Categories
Web analytics

Custom timer measuring the actual time spent on the page depending on the length of the content. GTM + GA

What did I want?

Measure the time on the content page so I can say that users have really read the content. Of course, this is a script in Google tag manager.

Required features:

  • Start measuring only on a loaded page (DOM ready) when the user really sees the page.
  • Measure the real time the user sees the page. This results in the use of document.visibilityState and the visibilitychange listener.
  • I wanted to change the length of time according to the length of the text. I wanted to use it to define a well-read article.

GTM trigger for HTML script.

Triggers only when the user has loaded the page content and sees it.

.*Check all events  + Use regex matchingSince I have enough GTM events in the page, all I need is this rule. For some sites, you will need to add a listener to change the visibility .
Rule:
gtm.dom equals true(The variable gtm.dom is a dataLayer variable named “gtm.dom”)
js.document.visibilityState equals visibleIt is a javascript variable document.visibilityStatesupport 96.16% ) with settings Convert undefined to notsupport . For other browsers, you can make a GTM triger Timer 30sec and name it anyway and set the condition in reverse, it will run at notsupport.

How does it work?

It launches only on the page that is loaded at the DOM level and at the same time the page is in the focus window (trigger). The window is not displayed in the next tab of the browser and the user really sees it.

At the beginning is the setting:

The following is the code for measuring the number of words and calculating the time required to read the content

This works if the content page is a Variable {{dl.pagePostType2}} contain "single-". I got this variable from the WordPress plugin on GTM which I really like DuracellTomi’s Google Tag Manager for WordPress and I recommend it. This condition can be done in any other way, or you can delete this content and set the time in the timer to fixed. Otherwise, it is calculated here that a fast reader can handle 228 words per minute, and I will want him to read at least a quarter of the content at this speed, adding to the minimum time spent on the page.

document.querySelector("#main") It is a general selector of the main content for wordpress. In general, this is a place for creativity.

Then the code follows the code for setting and starting the timer with the pause and resume functions.
Then the code monitoring changes is visibilityState.
Finally, registration of javascript functions waiting for browser events.

The entire timer code measuring the time on the visible page then looks like this:

This code is inserted into GTM tag type of HTML , it is set to 1 run per page, and set trigger.

And this is a compressed version of a previous script

Where do I use it?

If the user is on an article and spends time there defined enough to move within the noidle time and see the end of the page, then I declare that the user has read the content well enough and send the transaction with ecom article definition to Google analytics.

.