Dev.to WebDev πŸ›  Dev πŸ‘ 0 πŸ“– 2 min read

The Copy button was clicked. Did anything reach the clipboard?

Click Copy on the Bold card in the PasteCharm font generator, then paste the result somewhere you can see it. That's the whole interaction when the browser lets the clipboard write go through. What should the same button

Click Copy on the Bold card in the PasteCharm font generator, then paste the result somewhere you can see it. That's the whole interaction when the browser lets the clipboard write go through. What should the same button do if the write fails?

PasteCharm opens a dialog with the output already selected. You can still copy it yourself. The analytics event for a successful copy stays quiet.

Put the event after the write

A click handler knows someone tried to copy. The promise returned by clipboard.writeText() tells you when the write completes. Here's the modern clipboard branch on its own, with the fallback details left out:

async function copyStyledText(text) {
  try {
    await navigator.clipboard.writeText(text);
  } catch {
    openManualCopyDialog(text);
    return;
  }

  trackCopy("font");
  showStatus("Copied!");
}

The live tool also tries an older browser copy command. If that command reports success, it takes the same success path. If both attempts fail, the manual dialog is the way out. Opening that dialog doesn't count as a copy because the app hasn't observed a successful write.

Test the failure on purpose

The PasteCharm Playwright test makes both automatic copy paths fail before the page loads:

await page.addInitScript(() => {
  Object.defineProperty(navigator, "clipboard", {
    value: { writeText: () => Promise.reject(new Error("blocked")) }
  });
  document.execCommand = () => false;
});

Then it opts into analytics, clicks the Bold card's Copy button, and checks for two results. The manual dialog should be visible, and the analytics queue should contain zero copy_success events. The test intercepts the Google tag request and inspects the local queue, so it can run without sending test events to GA4.

Count the action without collecting the text

With analytics enabled, a successful copy from the Bold card queues copy_success with tool_type: "fonts" and copy_type: "font" alongside the analytics routing ID. The text someone typed and the styled output never go into that event. A separate test checks the queued payload and looks for a sample input that should stay out of it.

That gives the event a useful meaning. It counts observed automatic copies. It can't tell me whether a person later pasted the result or whether they copied it by hand. I'd keep the manual path even if it makes the counter less complete. The person who came for a line of text still gets a way to take it with them.

This article was drafted with AI from PasteCharm’s implementation and test cases.

πŸ“° Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes β€” full credit and traffic to the original publisher.