Lesson 3 of 15
CSS-Selector Clicks
① Connect your GTM container
Paste your container ID to load it into this page. It only ever runs here.
Advanced: use a specific environment
When the element you care about has no handy ID, you can still target it by its place in the markup. GTM's matches CSS selector operator runs a real CSS query against Click Element, so one trigger can pinpoint a deeply-nested button.
Here two cards have identical "Choose plan" buttons; you'll fire a tag on the featured one only, using the selector .card.featured .btn.
You won't always be handed a selector like that one. Here's how to pull your own on any real page. The two cards live on this lesson's live page rather than here, so open that page (or any site) to follow along:
- Right-click the element you want to track and choose Inspect. Your browser's DevTools open with that element highlighted in the HTML panel.
- Read its tag,
id, and classes, then build a selector from the most stable ones:#idtargets an id,.classtargets a class, and a space means "inside". So.card.featured .btnreads as "a.btnanywhere inside an element that has bothcardandfeatured". - In a hurry? Right-click the highlighted node → Copy → Copy selector and DevTools writes a unique selector for you. It works, but it's often brittle (long
nth-childchains that break when the page changes), so trim it back to the classes that actually mean something. - Test it before you trust it. On a page that has the element, open the Console tab and run
document.querySelectorAll('.card.featured .btn'), then confirm the match count is exactly what you expect, no more and no less. That's the same query GTM runs againstClick Element.
Lean on stable hooks like a class or a data- attribute over the auto-generated copy, they survive redesigns far better than position-based chains. For the full menu of selector types, child, sibling, attribute, pseudo-class and more, the W3Schools CSS selectors reference is a solid place to keep reading.
Goal
Fire a tag on one button, scoped with a CSS selector instead of an ID.
Build it in GTM
Enable Click Element
- Go to Variables in the left sidebar and click Configure at the top of the Built-In Variables section.
- Switch on
Click Element. The CSS selector runs against this.
Create the trigger
- Go to Triggers in the left sidebar and click New.
- Click the trigger-type box and choose Click → All Elements.
- Set it to Some Clicks so a condition appears.
- Set the condition to
Click Elementmatches CSS selector.card.featured .btn. - Name it
Click - Featured planand Save.
Tag: Custom HTML - Test
- Go to Tags → New and choose Custom HTML.
- Paste
<script></script>(fires but does nothing) and name itCustom HTML - Test. - Under Triggering, add the trigger from the previous step, 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.
- 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
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