In the next few lines, I will go over 4 advanced concepts that I think are a great example of what Google Analytics in a combination with Google Tag Manager can do. Additionally, by understanding these concepts I was able to better utilise these technologies and generally elevate my knowledge in the area.
Callback as a concept is popular and applicable across any programming language in general. It’s basically a piece of code that is pushed as an argument to a function. When the function has completed, that piece of code (the callback) is executed.
Some use-cases where I used custom orchestration:
– Custom tag sequencing. I’ve written about the specifics around Custom HTML Tags in this post
Here are some additional resources on this topic:
Callback Function Definition
Google Analytics – hitCallback
Google Tag Manager – Fields to set (hitCallback) – Advanced Section
2 – Working with Cookies.
However, it is worth mentioning that with each new cookie added, the terms & privacy policies on the website you are adding them to needs to be updated, also make sure the data stored is aligned with regulations such as GDPR.
Here are some occasions where I’ve dropped some cookies:
– Prevent duplicate transactions on an e-commerce store, when doing that server-side is not an option.
– Surface information that is used for website personalisation
– Surface information to score user sessions, based on specific on-site interactions and behaviour
3 – Utilisation of custom data transport mechanisms.
The analytics.js library allows the use of the navigator.sendBeacon() method, which means that you can asynchronously transfer small beacons of data from the user’s browser to the
/collect endpoint using a
POST request. When you are passing an event data, for example, using this advanced transport mechanism will allow the data to be send too Google Analytics even if the user left the page to early, or even closed the browser window.
This feature, however, lacked some browser support, but recently things started to pick up and it looks promising. I’ve written a few lines on the topic in this post
The main use-case I found applying this concept was the given example above:
– transporting event data based on the completion of a form, where the page was re-directing too quickly for the hit to be sent to GA.
4 – Using customTask to customize how analytics.js validates, constructs, and sends measurement protocol requests.
send command is called, either via a hard-coded GA implementation or via Google Tag Manager templated GA tag, what happens behind the curtains is a HTTP request that analytics.js compiles and sends to the Google Analytics servers. Each request is constructed via the measurement protocol mechanism and the whole process consists of a number of tasks where the
customTask is the first one that is executed.
A full list of those tasks and further documentation on the library can be found here
model parameter as input. The
model is a simple object that provides access to any of the fields defined in the library. The fact that
customTask is executed first means that you can replace or change completely the behaviour of the other functions, using the
set methods provided.
Here is some further reading on the Model Object
I’ve used customTasks for:
– Setting a Client-ID as a Custom Dimension
– Removing PII mid-hit
Using these concepts, sometimes even in a combination for specific solutions, extends what is available out of the box from Google Analytics and Google Tag Manager and really shows that anything is achievable when it comes to data collection and on-page tag management. I am still exploring the technologies with every project I work on and I am still on the look for other edge cases where an unusual solution is required! Pushing to the limits of any technology stack is the best way to learn.
Share this Post