SHARE

How to track time on page with Google Tag Manager

Our script for accurate tracking of time on page beats Google’s default measurement to give you an accurate picture of how long users are spending on your page open and in focus.

This post translates the approach into Google Tag Manager. The setup consists of two tags (one custom), one firing rule and two variables.

Step by step:

1. Add the timer script as a custom HTML tag

2. Add two variables to access the data layer variables

One for the formatted time, which will feed through the event label

And one for the number of seconds in focus since the last event, which will feed through the event value

3. Add the firing rule for the event

4. Add the tag that reports the timer event to Google Analytics

Options and further information

You can change the timer interval in the custom HTML tag – the reporting will adjust accordingly. Choosing the interval is a trade-off between the resolution of the reporting and the load on the client in sending events, as well as Google’s 500 hit per session quota.

We’ve chosen ten seconds because we think the users who are in ‘wrong place’ and don’t engage at all will leave in under ten seconds, anything more is some measure of success.

If you’d like assistance implementing this or something else to get an accurate picture of how users interact with your site, get in touch!

Subscribe to our blog

Thanks for signing up!

Related posts

There’s a flaw in the way Google Analytics measures ‘time on site’: the counter only starts from the second page visited, so all one-page visits are counted as zero time on site.
If a visitor comes to your page, stays for 10 minutes reading – and then closes the window… that’s counted as ZERO time.
With landing pages that have lots of interaction, or the call to action is a phone call rather than a click, this can be a real problem.
Pasting the Javascript below onto all the pages of your site will fix the problem.
The script logs an event to Google Analytics for every 10 seconds the visitor stays on the page, regardless of whether they bounced or not. But it won't affect your bounce rate or time on site for historical comparison *.
We suggest you look closely at how visitors drop off after 10, 20 and 30 seconds to see which of your web content could be improved.
Paste this into the source of your all your pages, after the Google Analytics script
<!-- Time on Site tracking (c) LittleData.co.uk 2014 --><script>(function(e){var t=true;var n=0;var r=true;var i=function(){t=false};var s=function(){t=true};if(window.addEventListener){window.addEventListener("blur",i,true);window.addEventListener("focus",s,true)}else if(window.attachEvent){window.attachEvent("onblur",i);window.attachEvent("onfocus",s)}var o=function(e){return Math.floor(e/60)+":"+(e%60==0?"00":e%60)};var u=window.setInterval(function(){e=e+10;if(t){n=n+10;if(typeof _gaq==="object"){_gaq.push(["_trackEvent","Time","Log",o(n),n,r])}else if(typeof ga==="function"){ga("send",{hitType:"event",eventCategory:"Time",eventAction:"Log",eventLabel:o(n),eventValue:10,nonInteraction:"true"})}}},1e4);window.setTimeout(function(){clearInterval(u)},601e3)})(0)</script>
What you'll see
In Google Analytics go to Behaviour .. Events .. Top Events and click on the event category 'Time'.
Searching for a particular time will find all the people who have stayed at least that length of time. e.g. 0:30 finds people who have stayed more than 30 seconds.
FAQs
Does this affect the way I compare bounce rate or time-on-site historically?
No. The script sends the timer events as 'non-interactive' meaning they won't be counted in your other metrics. Without this, you would see a sharp drop in bounce rate and an increase in time on site, as every visitor was counted as 'non-bounce' after 10 seconds. If you prefer this, see below about adapting the script.
Will this work for all browsers?
Yes, the functions have been tested on all major, modern browser: IE 9+, Chrome, Safari and Firefox.
What if I upgrade to Universal Analytics?
Don’t worry – our script already checks which of the two tracking scripts you have (ga.js or analytics.js) and sends the appropriate log.
Will this max out my Google Analytics limits?
The script cuts off reporting after 5 minutes, so not to violate Google’s quota of 200 – 500 events that can be sent in one session
Can I adapt this myself?
Sure. The full source file is here.
Need more help? Get in touch with our experts!

What is cross-domain tracking and why do you need to implement in your Google Analytics account?
Cross-domain tracking makes it possible for Analytics to see sessions on two related sites (such as an ecommerce site and a separate shopping cart site) as a single session. This is sometimes called site linking.
Cross-domain literally means that you are able to see a user in a single Google Analytics account in his journey across multiple domains that you control (e.g. mysite.com and myshoppingcart.com).
In the standard configuration of the Google Analytics script, every time a customer loads a page on a different domain a new session is generated, even if the branding looks seamless to the user and, unfortunately, the previous session has ended and this is even if the customer is still active and generates events and page views. Until you have implemented the cross-domain setting on your website you will not be able to have an accurate customer journey.
Why? Let’s take, for example, a standard website, www.siteA.com, and it's blog, www.blogB.com.
To track sessions, Analytics collects a client ID value in every hit. Client ID values are stored in 1st party cookies, and these cookies are only available to web pages on the same domain. When tracking sessions across multiple domains, the client ID value has to be transferred from one domain to the other. To do this, the Analytics tracking code has linking features that allow the source domain to place the client ID in the link URL, where the destination domain can access it.
Fortunately, with the release of Universal Analytics cross-domain tracking, it is easier to implement, and especially so with Google Tag Manager.
Setting up cross-domain tracking using Google Tag Manager
Add (or edit your existing) a basic page tracking tag (i.e. Tag Type = Universal Analytics; Track Type = Page View).
If you are using the same container for siteA.com and blogB.com, under More Settings → Fields to Set, enter the following:
Field Name: allowLinker
Value: true
Under More settings → Cross-Domain Tracking → Auto Link Domains enter "blogB.com" (without the quotes).
If you have multiple domains, separate them by commas: blogB.com, siteC.com
Leave the 'Use hash as delimiter' and 'Decorate forms' unless you have an unusual web setup.
Set the trigger to "All Pages".
Save a version of the container and publish it.
If you are using a separate container for blogB.com, repeat the steps above but in the Auto Link Domains field add: siteA.com
Add both domains to the Referral Exclusion List
When a user journey crosses from your first domain to your second domain, it will still appear as a new session in Google Analytics by default. If you want to be able to track a single session across multiple domains, you need to add your domains to the referral exclusion list.
Here’s an example Tag Assistant Recordings report that shows what it looks like when cross-domain tracking is not setup properly.
Setting up cross-domain tracking by directly modifying the tracking code
To set up cross-domain tracking for multiple top-level domains, you need to modify the Google Analytics tracking code on each domain. You should have basic knowledge of HTML and JavaScript or work with a developer to set up cross-domain tracking. The examples in this article use the Universal Analytics tracking code snippet (analytics.js).
Editing the tracking code for the primary domain
ga('create', 'UA-XXXXXXX-Y', 'auto', {'allowLinker': true});
ga('require', 'linker');
ga('linker:autoLink', ['siteB.com'] );
Remember to replace the example tracking ID (UA-XXXXXX-Y) with your own tracking ID, and replace the example autoLink domain (siteB.com) with your own secondary domain name.
Editing the tracking code on the secondary domain
ga('create', 'UA-XXXXXXX-Y', 'auto', {'allowLinker': true});
ga('require', 'linker');
ga('linker:autoLink', ['siteA.com'] );
Remember to replace the example tracking ID (UA-XXXXXX-Y) with your own tracking ID, and replace the example autoLink domain (siteA.com) with your own primary domain name.
Adding the domain to page URLs using filters
By default, Google Analytics only includes the page path and page title in page reports - not the domains name. For example, you might see one page appear in the Site Content report like this:
/contactUs.html
Because the domain names aren’t listed, it might be hard to tell whether this is www.siteA.com/contactUs.html or www.blogB.com/contactUs.html.
To get the domain names to appear in your reports you need to do two things:
Create a copy of your reporting view that includes data from all your domains in it
Add an advanced filter to that new view. The filter will tell Google Analytics to display domain names in your reports.
Follow this example to set up a view filter that displays domain names in your reports when you have cross-domain tracking set up. For some fields, you need to select an item from the dropdown menu. For others, you need to input the characters here:
Filter Type: Custom filter > Advanced
Field A: Hostname Extract A: (.*)
Field B: Request URI Extract: (.*)
Output To: Request URI Constructor: $A1$B1
Click Save to create the filter.
You can validate that filters are working as you expect using Google Tag Assistant Recordings. Tag Assistant Recordings can show you exactly how your filters change your traffic.
Get Social! Follow us on LinkedIn, Twitter, and Facebook and keep up-to-date with our Google Analytics insights.