This is a guest post byStephen Harris fromSeer Interactive. He was kind enough to share his awesome solution in this blog, so I’m very grateful indeed for his contribution.
If Google Tag Manager is loaded as the primary instrument for tracking on a webpage (as it should be), then all webpage tracking could and should be configurable via GTM. But we don’t always control the circumstances, and it’s not uncommon to face hardcoded Google Analytics tracking outside of GTM.
Perhaps GA tracking cannot be removed from the website source code quickly enough. Maybe the website is on a publishing schedule that doesn’t suit measurement needs, or maybe the folks responsible for the website don’t have sufficient resources. Whatever the reason, you got a “no” when you
requested insisted on removal. (You did insist on removal, didn’t you?)
Or what about when there are hardcoded GA commands in a platform or plugin? It could be screwing with your data. But chances are it’s not doing anything at all (because it’s tracking to the nonexistent default GA tracker) and there seems to be no way to get it to work through GTM.
And even when all tracking runs through Google Tag Manager, some things can be difficult (or impossible) to implement using the tag templates (such as addingGA plugins, or defining Universal Analytics tracker field defaults).
Here’s a solution.
1.2. A Good Spy Starts With Research
2. Reliability And Caveats
2.1. Uncommon Behavior For A Common Library
2.2. Cannot Intercept Hardcoded Pageviews From GA
3.3. Listening From GTM
4.2. Access Hardcoded GA Command Data Via GTM
1. Spying On GA
You can grab the code on GitHub . Copy-paste it either into a Custom HTML Tag in Google Tag Manager, or load it in the page template proper.
It’s called GA Spy because it can silently hijack and control all tracking that relies on Google’s Universal Analytics library ( analytics.js ). Effectively, this is a Google Analytics listener that does karate.
To put this in layman’s terms, the script listens for interactions with the ga() interface, returning the arguments passed to the function for you to process for whatever purpose you want. For example, you can automatically copy all calls to ga('send'...) to dataLayer.push() syntax, allowing you to fluently replicate hardcoded Universal Analytics tracking in Google Tag Manager.
This blog post is written assuming the hardcoded tracking is for Universal Analytics, but there is a version of the script for the async GA library (ga.js), to which the same core concepts apply.
Source code for ga-spy.js
Source code for ga-spy_async.js
1.1. Spy Code 101
Let’s walk through the script logic. Before implementing, it’s important to get a basic idea of what this script actually does. Once you understand the basics, you’ll also understand why this should be considered a temporary solution . Indeed, you should document the use of this solution, so that when the hardcoded Universal Analytics code is eventually removed from the site, you won’t be left hanging dry with methods that don’t really do anything.
This section also explains this script’s biggest caveat: GA Spy is not guaranteed to intercept (or even detect) tracking that fires while the page is loading (e.g. the standard GA snippet) if the hack is deployed via GTM. Capturing these requests requires that you deploy the script on the page template itself, which unfortunately might detract from the usefulness for those without developer resources handy.
For a more technical breakdown, check out the README .
1.2. A Good Spy Starts With Research
Let’s start with an overview of how the Universal Analytics tracker works on the page.