GTM & GA: 50 ways to build a trigger
The problem is all inside your head She said to me The answer is easy If you take it logically I'd like to help you In your struggle to be free There must be 50 ways to build a trigger
I want to track clicks on this button.
Right-click (or control-click on a Mac) the button, and choose Inspect Element. Check out the button (and its inner <span> and containing <div>) code. All of the following trigger definitions will track clicks (or link clicks) on this button, using just the built-in variables. Enjoy!
- Link Click Trigger: {{Click URL}} <equals> https://www.youtube.com/watch?v=xhasLyfuT4Y
- Link Click Trigger: {{Click URL}} <contains> www.youtube.com/watch?v=xhasLyfuT4Y
- Link Click Trigger: {{Click URL}} <matches regex (ignore case)> youtube.*xhasLyfuT4Y
- Link Click Trigger: {{Click Text}} <equals> Green Button
- Link Click Trigger: {{Click Target}} <equals> paul-simon-window
- Link Click Trigger: {{Click Target}} <starts with> paul-simon
- Link Click Trigger: {{Click Target}} <contains> simon
- Link Click Trigger: {{Click Target}} <matches regex> ^paul.*window$
- Link Click Trigger: {{Click ID}} <equals> one-and-only-green-button
- Link Click Trigger: {{Click Classes}} <equals > green-button white-text
- Link Click Trigger: {{Click Classes}} <contains > green-button
- Link Click Trigger: {{Click Element}} <matches css selector> a[href*="https://www.youtube.com/watch?v=xhasLyfuT4Y"]
- Link Click Trigger: {{Click Element}} <matches css selector> a[target*="paul-simon-window"]
- Link Click Trigger: {{Click Element}} <matches css selector> a#one-and-only-green-button
- Link Click Trigger: {{Click Element}} <matches css selector> #one-and-only-green-button
- Link Click Trigger: {{Click Element}} <matches css selector> a#one-and-only-green-button.green-button
- Link Click Trigger: {{Click Element}} <matches css selector> #one-and-only-green-button.green-button
- Link Click Trigger: {{Click Element}} <matches css selector> a.green-button
- Link Click Trigger: {{Click Element}} <matches css selector> .green-button
- Link Click Trigger: {{Click Element}} <matches css selector> .green-button.white-text
- Link Click Trigger: {{Click Element}} <matches css selector> [data-title="This is my Green Button"]
- Link Click Trigger: {{Click Element}} <matches css selector> [data-title^="Green"]
- Link Click Trigger: {{Click Element}} <matches css selector> [data-title|="This"]
- Link Click Trigger: {{Click Element}} <matches css selector> [data-title^="This"]
- Link Click Trigger: {{Click Element}} <matches css selector> [data-title*="my Green"]
- Link Click Trigger: {{Click Element}} <matches css selector> div#button-container-id *
- Link Click Trigger: {{Click Element}} <matches css selector> div#button-container-id > a
- Link Click Trigger: {{Click Element}} <matches css selector> div#button-container-id a
- Link Click Trigger: {{Click Element}} <matches css selector> #button-container-id > a
- Link Click Trigger: {{Click Element}} <matches css selector> #button-container-id a
- Link Click Trigger: {{Click Element}} <matches css selector> div a.green-button
- Link Click Trigger: {{Click Element}} <matches css selector> div > a#one-and-only-green-button
- Link Click Trigger: {{Click Element}} <matches css selector> div > a.green-button
- Link Click Trigger: {{Click Element}} <matches css selector> a#one-and-only-green-button.green-button
- Click Trigger: {{Click Text}} <equals> Green Button
- Click Trigger: {{Click Classes}} <equals> button-text
- Click Trigger: {{Click ID}} <equals> green-button-span
- Click Trigger: {{Click Element}} <matches css selector> #green-button-span
- Click Trigger: {{Click Element}} <matches css selector> span#green-button-span
- Click Trigger: {{Click Element}} <matches css selector> span.button-text
- Click Trigger: {{Click Element}} <matches css selector> #green-button-span.button-text
- Click Trigger: {{Click Element}} <matches css selector> span#green-button-span.button-text
- Click Trigger: {{Click Element}} <matches css selector> div#button-container-id span
- Click Trigger: {{Click Element}} <matches css selector> div#button-container-id #green-button-span
- Click Trigger: {{Click Element}} <matches css selector> div#button-container-id .button-text
- Click Trigger: {{Click Element}} <matches css selector> div.button-container-class #green-button-span
- Click Trigger: {{Click Element}} <matches css selector> .green-button *
- Click Trigger: {{Click Element}} <matches css selector> #button-container-id *
- Click Trigger: {{Click Element}} <matches css selector> #one-and-only-green-button > span
- Click Trigger: {{Click Element}} <matches css selector> .green-button > span
There are literally infinite ways to do this. Here are a few things that should help to explain and enlighten the 50 ways shown here:
- Click Triggers (as opposed to Link Click Triggers) will report the innermost element that was clicked. In this case, it's the <span>. The <span> doesn't have an href attribute, so if you use a Click Trigger, {{Click URL}} will be blank. {{Click ID}} and {{Click Classes}} will refer to the <span>'s id and classes.
- Link Click Triggers will only fire if an <a> tag is clicked, and will always refer to that element. That means that {{Click ID}}, {{Click Classes}}, etc. will always refer to the <a> tag's id and classes.
- Simo Ahava has an excellent post about GTM matches css triggers
- Here's a CSS selector overall reference, and a great reference for using CSS selectors on attributes
- This regular expression (regex) tutorial got me started with regex