Event tracking through CSS selectors in Google Tag Manager

This article is in conjunction with the article Introduction to DOM, Nodes & CSS Selectors for Google Tag Manager where I introduced the concept of DOM and CSS selectors. Make sure you read it, in order to get the best out of the present article.

Today I am going to show you, how to use CSS selectors for tracking events in Google Tag Manager.

Tracking clicks on a button via CSS selectors in Google Tag Manager

Here is how the whole process would look like:

  1. Find the CSS selector of the button you want to track.
  2. Create a new tag of type Universal Analytics that sends event data to GA on button click.
  3. Test the tag and then publish the container.

Let us suppose you want to track clicks on the ‘Start Assessment’ button on the page: https://www.optimizesmart.com/web-analytics-training-course-promo/

CSS selectors in Google Tag Manager

Follow the steps below:

Step-1: Install the chrome extension called ‘SelectorGadget‘ for finding the CSS selector of a page element.

Step-2: Navigate to the page: https://www.optimizesmart.com/web-analytics-training-course-promo/

Step-3: Click on the ‘SelectorGadget’ icon (at the top right-hand side of the browser window) in order to turn on the extension:

selector gadget

Step-4: Now select the ‘Start Assessment’ button and note down the corresponding CSS Selector, displayed in the ‘selectorGadget’ tool:

css selector

In my case the CSS selector is: .wpProQuiz_button

Step-5: Turn off the ‘SelectorGadget’ extension by clicking on its icon again (at the top right-hand side of the browser window).

Step-6: Navigate to your GTM account and create a new tag of type ‘Universal Analytics’. Name it something like ‘Send click on the Start Assessment Button’:

send click tag

Step-7: Create the tag with the following configuration:

tag configuration
tag configuration2 2

Step-6: Create a new trigger with the following configuration:

CSS selectors in Google Tag Manager

Step-7: Save the tag and then preview the container.

Step-8: Navigate to the page: https://www.optimizesmart.com/web-analytics-training-course-promo/

Step-9: Click on the ‘Start Assessment’ button and then scroll down the page, where you can see the ‘GTM Debug Console window‘.

Step-10: Click on the ‘gtm.click’ event under ‘Summary’ column in the GTM debug console window:

gtm click

If you see your tag (in my case the tag is ‘Send click on the Start Assessment Button’) being displayed under the ‘Tags fired on this event’ section, then it means that your tag fires on button click.

Step-11: Click on the tag to see what values are being passed:

click on tag

Step-12: Scroll down to the ‘Firing Triggers‘ section to check the various filters for your trigger:

firing triggers

You should see a green checkmark next to each filter.

If your tag is not firing then you would see red cross mark next to one or all of your filters.

Thus ‘Firing triggers’ section is a good way to determine, why a tag is not firing.

Step-13: Navigate back to your GTM account and then click on the ‘Leave Preview Mode’ link.

Step-14: Publish the container.

Step-15: Navigate to your Google Analytics reporting view and then click on the ‘Top Events‘ link (under ‘Behavior‘ > ‘Events’) to see the data for the tracked events:

start assessment event

That’s how you can use CSS selectors to track events in Google Tag Manager.

Related Articles

Event Tracking in Google Tag Manager

Event Tracking in Google Analytics

My best selling books on Digital Analytics and Conversion Optimization

Maths and Stats for Web Analytics and Conversion Optimization
This expert guide will teach you how to leverage the knowledge of maths and statistics in order to accurately interpret data and take actions, which can quickly improve the bottom-line of your online business.

Master the Essentials of Email Marketing Analytics
This book focuses solely on the ‘analytics’ that power your email marketing optimization program and will help you dramatically reduce your cost per acquisition and increase marketing ROI by tracking the performance of the various KPIs and metrics used for email marketing.

Attribution Modelling in Google Analytics and BeyondSECOND EDITION OUT NOW!
Attribution modelling is the process of determining the most effective marketing channels for investment. This book has been written to help you implement attribution modelling. It will teach you how to leverage the knowledge of attribution modelling in order to allocate marketing budget and understand buying behaviour.

Attribution Modelling in Google Ads and Facebook
This book has been written to help you implement attribution modelling in Google Ads (Google AdWords) and Facebook. It will teach you, how to leverage the knowledge of attribution modelling in order to understand the customer purchasing journey and determine the most effective marketing channels for investment.

About the Author

Himanshu Sharma

  • Founder, OptimizeSmart.com
  • Over 15 years of experience in digital analytics and marketing
  • Author of four best-selling books on digital analytics and conversion optimization
  • Nominated for Digital Analytics Association Awards for Excellence
  • Runs one of the most popular blogs in the world on digital analytics
  • Consultant to countless small and big businesses over the decade