How to check if Google Analytics is loaded

. Posted in: Data Collection
Tags: Google Analytics, Troubleshooting

When you are setting up Google Analytics on your website, you’ll want to verify that it’s actually working and that data is being collected and processed. So even before you begin to configure events, ecommerce tracking etc., it’s crucial that you can trust your data collection. This involves checking if Google Analytics is being loaded at all and if your customisations such as event tracking is working as intended. In this post, I’ll provide some tips and tricks that can help you in the initial setup phase on your Google Analytics implementation.

#1 Is your tracking code installed on all pages?

The number one important step is to make sure that your Google Analytics tracking code is installed on all pages on your website. If you’re using a plugin or extension for your CMS (such as the excellent Google Analytics plugin by Yoast for Wordpress), then you can be pretty sure that the code is where it’s supposed to be. Even so, I’ll always recommend to go through your site to make sure the code is actually there. Some caching tools will not update all your pages immediately and as such, the code may be missing on those pages.

Crawl your site

One of the safest ways to make sure that your tracking code is implemented on all pages is to scan the generated HTML source code on each and every page. If you have more than a few pages, this can be very time consuming. So a great way to accomplish this, is to use a crawler. There are online tools that will do this for you - for free. Take a look at GA Checker, which will scan your entire website and provide you with a list of pages where your tracking code is missing. In addition, desktop software such as Screaming Frog (which is built for SEO auditing) can also be configured to crawl a website with specific search parameters. Using Screaming Frog you can use custom filters and rules to list pages that are missing the Google Analytics tracking code. It’s great for larger websites and it’s also possible to configure it to look for custom modifications to your tracking code (e.g. if you’re setting custom dimensions that you’d like to verify are being set on all pages).

Bonus: Use javascript to check if GA is loaded

In some cases, it’s useful to use javascript to check if Google Analytics is loaded. Maybe you want to trigger certain scripts or ajax calls if Analytics has been prevented from being loaded - for example, you may want to serve specific content to users who are actively blocking Google Analytics. Consider this script:

window.addEventListener("load", function(){
    if(window.ga && ga.create) {
        // Hooray! Analytics is present!
    } else {
        // Booh, no Analytics detected...
    }
}, false);

This script adds an event listener that attaches to the browser’s window object once all scripts and external assets (including css files and images) have been loaded. Once that has happened, it checks if the Google Analytics object and the Google Analytics ‘create’ method have been initialised. If that’s the case, then we can safely assume that Google Analytics has been loaded and we can choose to do anything from there. On the other hand, if neither the object nor the method can be found, then Analytics has been not been loaded. So based on that, you can do anything based on Analytics being loaded or not.

#2 Are events being tracked and collected in Analytics?

When you are setting up events (e.g. for tracking downloadable files, outgoing links etc.) you must verify that they’re working. You can verify that events are being tracked correctly in many ways - but the two easiest and most common approaches are to use debugging tools in your browser or by using Google Analytics' Real-Time reporting. For example, you may have set up event tracking on a PDF download link on your website. First, you need to click that link to trigger your event. And then you can verify that the event is being sent by:

Checking your Real-Time Events report in Google Analytics

If your Analytics property and your event has been set up correctly, you’ll be able to see your event instantly in the Real-Time Events report in Google Analytics. You may have to wait a few seconds, but that’s all. There, it’s possible to see the actual Event Category, Event Action and Event Label that’s being sent. But note, that Real-Time reports in Analytics will obey any filters you may have set up. So if you’re filtering out your own IP and if you’re testing via your IP, then the event will not appear in the report.

Using GA Debugger extension for Google Chrome

If you’re not using the GA Debugger extension for the Google Chrome browser, go and get it right now! When enabled, this extension will output - in text format - all the data that’s being sent to Google Analytics every time you trigger a pageview, event or any other Google Analytics hit. Not only on your own website, but on any website. To see this “data log”, enable the extension via the Google Chrome toolbar, and then open up the browser console (CTRL + SHIFT + J). Then trigger your event (e.g. by clicking a link that’s being tracked with events) and watch your console.

Check if Google Analytics events are loaded with GA Debugger

You’ll see a bunch of stuff, but what you want to notice is if the eventAction, eventCategory and eventLabel lines appear in your console. The screenshot above is the data log for one of my own events on this website that’s being triggered whenever a user reaches the end of a blog post within 60 seconds. The GA Debugger is actually great for any type of Google Analytics verification since it continually tracks and outputs what is sent to Analytics in real-time. So make it a habit to have the console open on a second screen if possible. It’s kind of like reading The Matrix, but you’ll get used to looking for inconsistencies.

Summing up: Checking if Google Analytics is loaded

There are two basic steps to checking if Google Analytics is being loaded. First of all, make sure that your tracking code is implemented on all pages. This can be done manually (by viewing source on each of your pages) or automatically by using online tools or desktop software. It’s also possible to use javascript to detect if Analytics has been loaded or not - and even fire scripts based on any of those two states. Secondly, it’s important that you verify your customisations. For the most part, this concerns event tracking. You can verify event tracking by triggering your events and then by checking either your Real-Time reports in Google Analytics or by using a browser extension such as GA Debugger for Chrome. I haven’t covered other interactions such as ecommerce transactions or Custom Dimensions and Metrics. But those too can easily be verified by using the GA Debugger. If you found this post useful, please comment or share :-)