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

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:

  1. Right-click the element you want to track and choose Inspect. Your browser's DevTools open with that element highlighted in the HTML panel.
  2. Read its tag, id, and classes, then build a selector from the most stable ones: #id targets an id, .class targets a class, and a space means "inside". So .card.featured .btn reads as "a .btn anywhere inside an element that has both card and featured".
  3. 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-child chains that break when the page changes), so trim it back to the classes that actually mean something.
  4. 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 against Click 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

  1. Enable Click Element

    1. Go to Variables in the left sidebar and click Configure at the top of the Built-In Variables section.
    2. Switch on Click Element. The CSS selector runs against this.
  2. Create the trigger

    1. Go to Triggers in the left sidebar and click New.
    2. Click the trigger-type box and choose Click → All Elements.
    3. Set it to Some Clicks so a condition appears.
    4. Set the condition to Click Element matches CSS selector .card.featured .btn.
    5. Name it Click - Featured plan and Save.
  3. Tag: Custom HTML - Test

    1. Go to Tags → New and choose Custom HTML.
    2. Paste <script></script> (fires but does nothing) and name it Custom HTML - Test.
    3. 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.

  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

The featured card's button fires your tag (Tags Fired); the standard card's button leaves it under Tags Not Fired, because only the first matches your selector.

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