Rule #1: Life is supposed to be fun!

August 23, 2019

How to use Timer in GTM to track real time on page (Google Tag Manager)

GA calculates time spent on a given page by taking the difference between the timestamp of that pageview and the timestamp of the subsequent hit. But if a pageview is the last (or only) hit in the session, GA will assign it a time of zero.

For example: If a person entered the website, then spent around 20 minutes reading your page, and gather all the information he needed, then he exit the page. He  is an potential customer, but in Google Analytics, he is count as a bounce, and session duration is 0.

How to use Google Tag Manager to track user’s time on page

Now, we can use Google Tag manager, timer trigger to fix that problem, by identify the real time-on-page for a particular page.

*** This trick should only be use in some specific pages, such as blog pages, or key pages, since it will send multiple hits to GA, and send much more events than normal.

See more: Track every user behaviors by sending client ID

1, Send a hit after user spend 30 seconds on page.

Create the Timer trigger as follows:

  1. Add a new trigger. Choose Timer as the event type.
  2. Enter the desired timer interval in milliseconds, e.g. For a 30 seconds timer, enter 30000, and set the timer limit to 1 (only fire once per page)
  3. At the conditions, you enter Page Url -> contains -> URL of the pages you want to track. Leave domain for all pages
  4. This trigger fire on all timers

Create-timer-trigger

Now, you create a universal analytics event, with category and label as you wish. This will not affect any reports. The correct set up should look like this

tag-manager-time-on-page

2. Multiple timer on a page

What if you want to record multiple timer events from the same timer? A single 30 second timer is better than nothing, but what if visitors are spending 5, 10, or as much as 20 minutes on a single page before leaving your site? With only a 30-sec timer, your session duration metric could still be hugely misleading.

The set up will be quite the same, as following instructions below:

1, Create a timer trigger. Set up the same as above, but you should leave limit blank. This will allow the timer listener to fire indefinitely until the user leaves the page or closes their browser.

unlimited-timer-google-analytics

2, Create two new variables to capture the timerEventNumber and timerInterval auto-event variables as follows. At least right now, GTM does not create these macros by default:

Create a new variable named timerEventNumber, set to the value of the gtm.timerEventNumber variable in the data layer. This will return the count of the number of times that the timer listener has fired on the page.

create-user-defined-variable

Data-layer-variable-configure-time-on-page

Create a new variable named timerInterval exactly as above, but with gtm.timerInterval as the data layer variable name. This will return the timer interval that you have specified in the listener tag.

3, Configure a variable , custom javascript

Then input the following code into the custom Javascript

function() {
var elapsed = {{gtm.timerEventNumber}} * {{gtm.timerInterval}} / 1000;
var min = elapsed/60;
return min + ‘m ‘;
}

 custom-javascript-variable-time-on-page

4, Configure your Universal Analytics event tag as follows.

GA-configure-time-on-page

Finally, in Google Analytics, it will look something like this

google-tag-manager-gtm-time-on-page

Posted in Google Analytics, Tag Manager
38 Comments
  • John

    It would be nicer to have time converted to seconds in steps of ten. How long is 0.166666666666666 minutes?

    11:15 am November 5, 2020 Reply
    • Hi John,

      Thank you for your comment. To convert it into minutes and seconds, you could change the timerEventNumber as follow:

      function() {
      var elapsed = {{gtm.timerEventNumber}} * {{gtm.timerInterval}} / 1000;
      var min = Math.floor(elapsed/60);
      var second = elapsed % 60
      return min + ‘m ‘ + second + ‘s’;
      }

      11:31 am November 5, 2020 Reply
    • It will look something like this

      11:33 am November 5, 2020 Reply
  • Jayden Greenwood

    Can I set it up without Google Tag Manager?

    9:11 am November 15, 2020 Reply
  • Jeffrey

    Hey, Thank you so much for this!
    This is truly useful!

    1 question here, after I manage to pass the event to GA, how can I create a custom report of the Average Time for that specific page?

    6:08 pm January 7, 2021 Reply
    • Yeah, I think it is totally possible

      7:22 pm January 27, 2021 Reply
  • Dash

    When using the timerEventNumber from the comment above you get this:

    Error at line 5, character 14: Parse error. Character ‘‘’ (U+2018) is not a valid identifier start char

    6:51 pm January 27, 2021 Reply
    • I am able to add the code without any problems. Maybe you should try to remove any character ” from the code (if there are any)

      7:27 pm January 27, 2021 Reply
    • successful code
      It should look like this

      7:29 pm January 27, 2021 Reply
      • Dominik

        I had the same problem. Switching the identifiers solved the problem.

        function() {
        var elapsed = {{gtm.timerEventNumber}} * {{gtm.timerInterval}} / 1000;
        var min = Math.floor(elapsed/60);
        var second = elapsed % 60
        return min + ‘m’ + second + ‘s’;
        }

        3:34 pm March 4, 2021 Reply
      • Himanshu Suri

        I am getting the same error on using the script,

        Error at line 4, character 14: Parse error. Character ‘‘’ (U+2018) is not a valid identifier start char

        Pleas help

        9:23 pm May 23, 2021 Reply
  • Kyle

    Copy/pasting directly from the website gave me curly typographer’s quotes in GTM instead of straight quotes. The curly quotes are causing the error.

    Make sure there are straight quotes on this line: return min + ‘m ‘ + second + ‘s’

    https://practicaltypography.com/straight-and-curly-quotes.html.

    (@Vu thanks for this tut — worked out great.)

    1:00 am March 11, 2021 Reply
  • KOZAGAHA

    Thank you so much for all these informations, it helped us ( people who are struggling in using firebase analytics and GA4) a lot.I got one question :

    What’s that trigger called unlimited Timer in the config of the True Time on page ?

    3:05 pm April 20, 2021 Reply
    • The trigger is still the same. There is only one timer trigger in Google Tag Manager

      1:18 am May 1, 2021 Reply
  • Pablo

    Hey, I keep getting these errors, for both gtm.timerInterval and gtm.timerEventNumber

    The timer event is getting pushed in the dataLayer, but keeps returning unknown variable when I preview. check below:

    Unknown variable “gtm.timerEventNumber” found in another variable. Edit the variable and remove the reference to the unknown variable.

    Unknown variable “gtm.timerInterval” found in another variable. Edit the variable and remove the reference to the unknown variable.

    any help is kindly appreciated in advance!

    I have checked my dly and they seem to match.

    Thank You!!!
    Pablo

    5:52 pm April 30, 2021 Reply
    • It seems that you have not created the custom variable. Could you check that please?

      1:17 am May 1, 2021 Reply
      • Pablo

        yes, you are right! thank you! (:

        8:13 pm May 7, 2021 Reply
  • Rodger

    How to use this in a GA4 report?

    Thanks

    7:26 pm April 30, 2021 Reply
  • Pablo

    Hey, does this script take into account tab visibility? If not, how can it be then “true” time on page? Also, it doesn’t track engagement on page. So is the tab just opened or is the user actually interacting with it (clicking, scrolling, etc.)

    Am I missing something about this solution?

    Thanks!

    11:16 am May 21, 2021 Reply
    • Pablo

      Hey, would love a response (: Thank you!

      11:54 pm May 27, 2021 Reply
      • Hi Pablo,

        That’s a great question. However, based on my knowledge, GTM and Google Analytics can not track if user switch tab (because it is user behavior on their own machine, not on the website), so this is not possible to take that into account.

        About engagement, you can send event about how long user scroll the website (for example 25%, 50%, 75%…), or you can track if user perform any click on website by sending events whenever customer click (try using Click trigger – All Element), but it is a totally different things.

        You can event send event when an important element visible on the screen using “Element Visibility” trigger.

        Let me know if it is still not clear

        3:26 am May 28, 2021 Reply
  • Pablo

    Thanks for your response! (:

    4:47 pm May 30, 2021 Reply
  • Matt

    Can a timer be used to track cumulative time on site instead of just time on a single page? For instance, if a user spends 1 minute on the homepage then spends 4 minutes on a subpage, is there a way to combine into an overall session time, and in this particular example, get a 5-minute timer to fire based on total time on site?

    9:02 pm June 11, 2021 Reply
    • Hi Matt, as far as I know, there is no way you can do that. However, if your purpose is tracking how long users actually spent on your website, then “Session Duration” metric in Google Analytics will be enough.

      Because we are sending events 10 seconds, and session is based on time of last event minus time of first event -> it will be accurate

      3:51 am June 12, 2021 Reply
      • Matt

        Thank you for your reply. We’re interested in tracking view through conversion activity, which is difficult to do in Google Analytics. We are measuring GA traffic currently, but we want to see how well our view-through conversions perform, thus the inquiry about a “cumulative time on site” timer. Do you have any other ideas for a workaround in GTM?

        1:34 pm June 15, 2021 Reply
        • I think I do not get your idea correctly. To me, a view-through conversion is only on Google Ads, and it is when you see the ads but you do not click the ads, then you visit the website and make a conversion. But I am not sure what you mean by “view-through conversion” on GA

          1:51 pm June 15, 2021 Reply
          • Matt

            Correct; however, we are interested in view-through conversions from non-Google sources. Our experience has been that GA does not do a good job of measuring Session Duration unless it is a Google product and as such, we are looking at accomplishing via GTM timer triggers.

            4:28 pm June 15, 2021
  • Matt

    Is it possible to have a timer track multiple pages or the entire website instead of resetting after each page?

    12:26 am June 12, 2021 Reply
  • DOM

    Can it be set with Google Ads Conversion Tracking?

    2:25 pm June 30, 2021 Reply
    • Google Ads conversion tracking can only be set to a specific event. You can try to set conversion when user stay on the page for more than a specific time (for example: 1 minute, 5 minutes…)

      2:42 pm July 4, 2021 Reply
  • George

    Very Helpful!
    The code examples on the page would have worked even better if right click (copy and paste) were not disabled. 🙂

    7:20 pm August 26, 2021 Reply
    • I have disabled it. Thank you for commenting

      12:03 pm September 10, 2021 Reply
  • Chenemi Abraham

    I’m getting this error ” Unknown variable “gt.timerInterval” found in another variable. Edit the variable and remove the reference to the unknown variable.”

    Plus I do not know how we got the “Unlimited Timer Trigger

    2:05 pm August 31, 2021 Reply
  • sp

    How to interpret the data under that event label, seeing that a session of user on the page is generating multiple “track” with “event label”, we need to add all the minutes and divide by the number of event?

    2:51 am September 10, 2021 Reply
    • Hi, you only need the last event. You don’t need to add all previous events

      12:02 pm September 10, 2021 Reply
  • […] How to use Timer in GTM to track real time on page (Google Tag Manager) […]

    3:02 am September 23, 2021 Reply
Write a comment