Categories
Web analytics

GTM script for measuring scrolling in single page applications

 

Why did I program it myself?

  • I have a SPA (Single page application) around me, where the classic scroll just doesn’t work. It is loaded once and it goes out. You can’t control it.
  • I don’t want Jquery because it’s useless for me today and I like vanilla without addictions mess.
  • I don’t want any ballast like the codes for super old GA classic etc. just pure GTM.
  • I don’t want to send a scroll more than once for a given distance on the page, that is, if I don’t want to.
  • I send an event interactive only if the user scrolls. at least one whole page, for pages that are too small I won’t turn on scroll because the page is too small.
  • I want my page length data in the number of visible screens etc

The code consists of two parts… initial setting + reset of values ​​and the function itself by measuring the scroll. If I go to a new virtual page, all I have to do is send a reset after loading the page (“pagescrollinit ()” function) and I’m done, the script measures even if I have a single page application that doesn’t change the URL or reload the whole page. Of course, you can re-fire the entire script and reset it as well. The script measures 50% and 90% scroll and scroll through the entire first page, which cancels the bounce rate. The script is created for clarity and minimalism, yes it can be extended, for example, for the brand to 25% or 75%, just copy a few pieces of code. You can try it yourself.

Measuring script

Variable with data

As you can see in the following picture, everything is nicely accessible via javascript and you can play with it and what is the main thing to read from the value.

You can also access the variables in GTM, just use the classic GTM javascript variable. For the example “pagescroll.DocPages” and the one according to the picture in this case returns the value 5, which says that the length of the user’s page is 5 viewport (screens).

What will be the output?

event: “Scroll-2page” to display the second page.

and distance measurement in percent

event: “Scroll” and eventAction: “90%” for a distance of 90% of the page.

Do I have it verified?

Yes, I run it on several sites for over a year.

Other tricks

You can divide the script into several parts, see note “(bonus)” and run the first part before sending pageview and send with pageview to GA enriched with a flag (GA metric) if it is too small a page and how long the page is in number of screens.

And the second part you run up to GA pageview callback and thus guarantee that it will never scroll before GA pageview is sent.

The mimistic reset of the scroll is then:

I deliberately put the scroll depth flags into individual variables, so you can ask at any time if “pagescroll.sc90” is True and you don’t have to go through the event map to find and check the current status, which in combination with SPA is quite rough.

.