Advanced GTM tracking: scrolling, clicks, visibility and the data layer

30 İyul 2026·Updated: ·⏱ ≈11 min·👁 130 views

Advanced GTM tracking: scrolling, clicks, visibility and the data layer
Contents

Once the basic setup is complete, you have page views and conversions. That’s useful, but it only tells part of the story. A page view tells you that a user has arrived while a conversion tells you they’ve completed the intended action. What happened in between is still unclear. With GTM, you can track user behaviour through scrolls, clicks, video views and the data layer.

Yet optimisation depends on what happens in that gap. Do people read to the end of the article? Do they even see your campaign banner? Which buttons do they use, and which go unnoticed? Are there JavaScript errors on your site that you haven’t spotted?

In this article, we’ll set up tracking for these behaviours, then move on to GTM’s most powerful, but least understood concept: data layer.

👉 GA4 and Google Ads setup

Scroll tracking in GTM: are people really reading your content?

One of the most useful and easiest tracking methods to set up, especially for blogs, news sites and long landing pages.

Setup steps

  1. Variables → Configure → Enable the `Scroll Depth Threshold`, `Scroll Depth Units` and `Scroll Direction` variables.
  2. Triggers → New → Scroll Depth. Select vertical scroll and enter the percentage thresholds: `25, 50, 75, 90`.
  3. Narrow the trigger’s scope. If you only need it on content pages, not across the whole site, use: `Page Path contains /blog`.
  4. Tags → New → Google Analytics: GA4 Event. Select your `constant` variable for the Measurement ID and set the event name to `scroll_depth`.
  5. Add a parameter: name `percent`, value: the `Scroll Depth Threshold` variable.
  6. Check in Preview: the tag should fire once at each percentage threshold. Thresholds already visible on screen may also be recorded when the page loads.

An important improvement: add a time condition

Looking at scroll depth alone can be misleading. If someone scrolls to the bottom of a page in three seconds, that doesn’t mean they’ve read the content. They may simply have skimmed the page and left. It wouldn’t be right to count that as “read”.

One solution is to combine a 90% Scroll Depth trigger with a 10-second Timer trigger in a Trigger Group. The tag fires once both triggers have fired. This checks that the user has reached the bottom of the page and met a minimum time condition.

The difference in results is significant. For example, “60% of views reached the bottom of the page” and “22% of views met both the scroll and time conditions” tell different stories. Even the second metric doesn’t prove that the text was actually read.

Timer triggers: time spent on site

A Timer trigger fires after a set amount of time on a page. There are two common ways to use it.

Use case 1: send a time event to GA4

Create a trigger: type Timer, interval `20000` (20 seconds), limit `1`. To activate it across the whole site, add the condition `Page URL matches RegEx .*`.

Then set up a GA4 Event tag with the event name `time_on_site`. You don’t need a separate template for the elapsed time: Data Layer Variable create a variable named `gtm.timerElapsedTime` and add it as a parameter. It returns a value in milliseconds. This is the time measured by the timer on that page, not the total active time on the site.

Use case 2: a time-based remarketing audience

This is a more interesting scenario. You can create a separate remarketing audience of users who spend at least 30 seconds on a page. This may indicate interest, but it doesn’t confirm it on its own.

Trigger: Timer, `30000` milliseconds, limit `1`. Tag: for Meta Custom HTML Use it to send a custom event with GTM. If your Meta Pixel is already set up, use `fbq('trackCustom', 'Stayed30Seconds');`. This event name indicates that someone has spent at least 30 seconds on the page.

You can then create an audience in Meta based on this event and target those users. Campaign results will show whether this is more effective than targeting all visitors.

Click tracking: start broad, then narrow down

The basic principle of click tracking is: start broad, then narrow down.

All clicks

Trigger: Click - All Elements, with no conditions. Tag: GA4 Event, event name `all_clicks`. This sends an event for every click recorded on the page.

Don’t leave this running permanently. Collect data for a day or two, see which elements people click, then switch it off. If it runs continuously, it can clutter your GA4 reports with unnecessary events.

Buttons only

Now narrow the scope. Right-click a button on your site, select “Inspect” and check the element’s CSS class, for example, `btn-primary`. `button` could be either a class name or the name of an HTML tag.

Trigger: Click - All Elements, with the condition `Click Classes contains btn-primary`. Tag: GA4 Event, event name `button_click`, parameter name `button_text` and value `Click Text`. This lets you see which button was clicked.

Preview makes the difference clear: clicking a button with the matching class fires `button_click`. Clicking a blank area of the page only records an event if the general click tag is active.

External links

It’s useful to know which links people use to leave your site, especially when you want to track visits to partner sites or social media profiles.

To do this, Auto-Event Variable create a variable: choose `Element URL` as the Variable Type and `Is Outbound` as the Component Type. Trigger: Click - Just Links, with the condition that the variable returns `true`. The tag fires when a link leads to an external site.

Element visibility: are people actually seeing your banner?

This type of tracking is often overlooked, but it’s very useful. You have an important section on a page, such as a contact form, campaign banner or pricing table, and want to know whether people reach it.

Scroll tracking doesn’t fully answer this question because the positions corresponding to percentage thresholds change when the page length changes. An Element Visibility trigger tracks a specific element instead.

Setup

  1. Variables → Configure → Enable the `Percent Visible` and `On-Screen Duration` variables.
  2. Right-click the target section on your site, select “Inspect” and find the element’s `id` attribute.
  3. Triggers → New → Element Visibility. Selection method: ID. Enter that ID.
  4. Set “When to fire this trigger” to Once per page .
  5. In the Advanced section, set the minimum time in view, for example, `10000` milliseconds. The tag fires when the element has been visible at the required percentage for 10 seconds in total. That time doesn’t have to be continuous.
  6. Set up a GA4 Event tag with the event name `section_view` and the parameter names `percent_visible` and `on_screen_duration`, using `Percent Visible` and `On-Screen Duration` as their values.

This tells you whether the section appeared on screen for the selected visibility percentage and duration. It doesn’t measure attention directly. If your contact form is at the bottom of the page and only 8% of visitors meet the conditions, it may be worth reviewing the form’s position too.

Capture JavaScript errors

This isn’t marketing tracking, but it directly affects marketing results. A JavaScript error on your site could stop a form from submitting, a button from working or a basket from opening. The user may simply leave, leaving the problem unnoticed.

GTM can help you track these errors:

  1. Variables → Configure → Enable the `Error Message`, `Error URL` and `Error Line` variables.
  2. Triggers → New → JavaScript Error, for all JavaScript Error events.
  3. GA4 Event tag: event name `js_error`. Add three parameters: `error_message`, `error_url` and `error_line`. Set each one to its corresponding variable.

You may need to create a controlled test error to check this tag in Preview mode. After publishing, check the GA4 Realtime report to see whether the `js_error` event is coming through.

Share the collected error messages with your development team. The source URL and line of code can help them diagnose the issue. It’s also useful to send the URL of the page where the error occurred.

Video view tracking

If your site has embedded YouTube videos, GTM can track them with a built-in trigger. The player must support the JavaScript API.

  1. Variables → Configure → Enable the `Video Title`, `Video Percent`, `Video Provider` and `Video Status` variables.
  2. Triggers → New → YouTube Video. Choose which milestones to measure: start, completion, pause and percentage thresholds (10, 25, 50, 75).
  3. Enable “Add JavaScript API support to all YouTube videos”. This adds `enablejsapi=1` to player URLs. A video that’s playing at the time may reload.
  4. Set up a GA4 Event tag with the event name `video_interaction`. Send the video title, percentage and `Video Status` value as parameters.

You can see which videos people watch to the end and which they stop early. To measure the first ten seconds separately, you’ll also need to set a time threshold. For product videos, this data can inform content decisions.

The data layer: where GTM’s real power lies

Now we’ve reached the most important concept. The tracking methods above rely on data that GTM can collect directly from the page and browser events: clicks, scrolls, URLs and elements. But some information can’t be read this way, or can’t be read reliably.

The problem

Imagine you have an e-commerce site. A product page shows the product name, price, colour, size and category. GTM can read this information from the page’s HTML structure, for example, by looking for the price in an element with the `.price` class.

But then a designer updates the page, moves the price block and changes its class name. GTM can no longer find the information, and tracking breaks without you noticing. You may not see an error message; the data simply stops coming through.

The solution

The data layer is a JavaScript array that passes information to tags. It provides a structured way to pass on important information, independently of how the page looks.

Even if you change the site’s appearance, tracking will keep working as long as the agreed data layer structure stays the same. GTM reads information from the data layer, not from page elements. Other tag managers need a suitable integration set up separately.

Structure

The data layer is declared in the site’s code like this:

<script> window.dataLayer = window.dataLayer || []; dataLayer.push({ 'event': 'view_item', 'product_name': 'Classic shirt', 'product_price': 49.90, 'product_color': 'white', 'product_size': 'M', 'product_category': 'menswear' }); </script>

Note: `dataLayer` is case-sensitive distinguishing uppercase and lowercase letters. The standard name won’t work as `datalayer` or `DataLayer`. This is an example of a custom structure; for GA4 e-commerce reports, the data must be mapped to the appropriate `items` structure.

How to view the data layer on your site

You don’t need to know how to code:

  1. Right-click on the site and select “Inspect”.
  2. Console tab.
  3. Type `dataLayer` and press Enter.
  4. You’ll see an array with entries numbered 0, 1 and 2. Click each one to view its contents.

If you see an empty array or an error, that doesn’t prove the data layer hasn’t been set up. Check when it loads, consent conditions and whether it’s using a different name. If needed, check with a developer.

How to create data layer variables

To use data layer information in GTM, create a separate variable for each value you need.

  1. View the data layer in the console and find the exact name of the field you need (for example, `product_price`).
  2. GTM → Variables → User-Defined Variables → New → type Data Layer Variable.
  3. Enter that name exactly in the “Data Layer Variable Name” field. A single typo can cause the variable to return `undefined`.
  4. Name it `dl: product price` and save.

With Version 2 selected, use dot notation for nested structures: `ecommerce.items.0.price`.

After creating it, check in Preview mode: Variables In the section, you should see the variable return the correct value. If there’s no value, either the name is wrong or the information hasn’t been sent at the time of the event you selected.

Together with a Custom Event trigger

This is where the data layer comes into its own: the site’s code can send an event to GTM at any point. The `'event': 'view_item'` line in the example above does exactly that.

To capture it in GTM: Triggers → New → Custom Event, event name `view_item`. This trigger fires only when the site’s code sends that event.

This approach is far more reliablethan triggers based on clicks or URLs because it won’t break just because the design changes. It’s widely used for professional e-commerce tracking, including product views, add-to-basket actions and purchases.

Custom HTML tags: for everything else

You can use a Custom HTML tag to add code without a suitable built-in tag template: advertising pixels, chat widgets, heatmap tools and A/B testing scripts. If the platform offers a built-in integration or suitable template, check that first.

Tags → New → Custom HTML, paste in the code and choose a trigger. Then check it in Preview.

A few important notes:

  • Always add a description to your Custom HTML tag: what it does and which platform it’s for. That note will save you time six months from now.
  • Read third-party code carefully. A Custom HTML tag on your site can run JavaScript code. This gives you flexibility, but also carries risk.
  • Consider attaching scripts that aren’t essential at initial load to the Window Loaded trigger. Check how delaying them affects measurement and the tool’s performance.

Conclusion

In this five-part series, we set up GTM from scratch: from the core concepts and interface to tracking logic and key platforms, then on to behaviour tracking and the data layer.

Here’s the overall picture. Page views and conversions show you the outcome . Scroll, click and visibility tracking help you investigate the reasons . The data layer helps tracking keep working when the site’s appearance changes, as long as its structure is maintained.

One final recommendation: don’t try to set everything up at once. Start with the basic setup, monitor the data for a week or two, then see which questions remain unanswered and add tracking to address them. Five tags chosen with a clear purpose are more useful than forty tags set up without a strategy.

To put this approach into practice with a realistic budget, visit the Google Ads campaign management page, where I explain the process.

Frequently asked questions

Do I need a developer to set up the data layer?

Usually, yes. A custom data layer requires changes to the site’s code. Built-in integrations and plugins can help with Shopify, WooCommerce and other CMS platforms, but a suitable GTM data layer isn’t guaranteed to be available automatically. Check the setup and console first.

What scroll depth thresholds should I set?

25/50/75/90 is a good starting set. Whether more thresholds are useful depends on what you want to measure, and each additional threshold can increase the number of events in GA4. For short pages, 50/90 may be enough.

I can’t find an ID for the Element Visibility trigger. What should I do?

The Element Visibility trigger offers two selection methods: ID and CSS selector. If the element has no `id` attribute, choose the CSS selector option and use its class name, for example, `.contact-section`. For a more reliable solution, ask a developer to add a stable ID to the element.

Does a Custom HTML tag slow down the site?

The code inside the tag and the resources it loads can slow down your site. Chat widgets and heatmap tools can add a particularly heavy load. If appropriate, attach them to a Window Loaded trigger and compare the results in PageSpeed Insights before and after.

How many data layer pushes can a page have?

There’s no fixed limit, but every push adds processing overhead. Initial information is sent when the page loads, followed by additional pushes in response to user actions, such as product views, add-to-basket actions and checkout steps. A Custom Event trigger captures only a matching push that includes an `event` key.

Topic: Google Tag Manager: posts in this topic →

Tural Rəhimov

About the author

Tural Rəhimov

Digital Marketing Manager: UM Azerbaijan

Digital marketing manager at Universal McCann (UM Azerbaijan). Experienced in Google Ads, Meta Ads, TikTok Ads and media planning. I help brands grow online.

More about me →

Need digital marketing support for your business?

Let's talk: I can help with everything from strategy to ad management.

Message me