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
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.
- Go to Tags → New and choose Custom HTML.
- Paste a script that listens for the
copyevent and pushes{ event: 'text_copy', copied_text: window.getSelection().toString() }. - Set its firing trigger to All Pages, name it
Listener - Copy, and Save.
Create the Custom Event trigger
- Go to Triggers → New and choose Custom Event.
- Set Event name to
text_copy(it must match theeventyou pushed). - Name it
Event - text_copyand Save.
Capture the selection in a variable
- Go to Variables → New (under User-Defined Variables) and choose Data Layer Variable.
- Set Data Layer Variable Name to
copied_text. - Name it
DLV - copied_textand Save.
Tag: Custom HTML - Test
- Go to Tags → New and choose Custom HTML with
<script></script>(fires but does nothing). Name itCustom HTML - Test, this is separate from the listener tag. - Under Triggering, add
Event - text_copy. If it's a GA4 tag, sendDLV - copied_textas a parameter. Then Save.
- Go to Tags → New and choose Custom HTML with
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.
- In your GTM, click Preview.
- Paste the live URL above and click Connect.
- Interact with the live page and watch your tag fire in Tag Assistant.
What you should expect to see
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