Don't have one? Create one ↗
← All courses

Lesson 11 of 15

Copy-To-Clipboard

① Connect your GTM container

Paste your container ID to load it into this page. It only ever runs here.

Advanced: use a specific environment

GTM has no built-in "copy" trigger, so this is your first taste of a common pattern: a small Custom HTML listener watches a browser event and pushes its own dataLayer event, which a Custom Event trigger then fires on.

You'll listen for the copy event, push text_copy with the selected text, and fire a tag on it, handy for spotting which content people share.

Goal

Fire a tag when someone copies text, using a Custom HTML listener and a Custom Event trigger.

Build it in GTM

  1. Add the copy listener

    GTM has no built-in copy trigger, so a small Custom HTML tag does the listening and pushes its own event.

    1. Go to Tags → New and choose Custom HTML.
    2. Paste a script that listens for the copy event and pushes { event: 'text_copy', copied_text: window.getSelection().toString() }.
    3. Set its firing trigger to All Pages, name it Listener - Copy, and Save.
  2. Create the Custom Event trigger

    1. Go to Triggers → New and choose Custom Event.
    2. Set Event name to text_copy (it must match the event you pushed).
    3. Name it Event - text_copy and Save.
  3. Capture the selection in a variable

    1. Go to Variables → New (under User-Defined Variables) and choose Data Layer Variable.
    2. Set Data Layer Variable Name to copied_text.
    3. Name it DLV - copied_text and Save.
  4. Tag: Custom HTML - Test

    1. Go to Tags → New and choose Custom HTML with <script></script> (fires but does nothing). Name it Custom HTML - Test, this is separate from the listener tag.
    2. Under Triggering, add Event - text_copy. If it's a GA4 tag, send DLV - copied_text as a parameter. Then Save.

Debug in Tag Assistant

Copy this lesson's live URL and paste it into GTM Preview, that is the page Tag Assistant connects to. It has the clickable elements, so this page stays clean for reading.

  1. In your GTM, click Preview.
  2. Paste the live URL above and click Connect.
  3. Interact with the live page and watch your tag fire in Tag Assistant.

What you should expect to see

Copying text adds a text_copy event in both the Tag Assistant timeline and the dataLayer panel. Your tag fires, and copied_text holds the selection.

Verify your container

Built it? Export your container, Admin → Export Container, choose your workspace, then drop the JSON here to check it against this lesson.

Drop your container .json here

or browse · checked in your browser, nothing is uploaded

0 of 15 lessons complete