Automating Testing with Python & Google Analytics

It happens, more than it should and it’s annoying…..web developers inadvertently break tracking while developing websites.

The problem is – the earliest sign you have that things have gone south is 24 hours later the revenue or conversions look unnaturally out of trend. By this point, you’ve lost a bunch of data, Year on Year trend is screwed and you’re adding annotations to Google Analytics to explain what happened in the hope that someone looks at the annotation next year when doing their Year on Year comparisons.

If you’re working on a site that has ongoing development, this type of problem can seem to happen almost once a month.

So what can be done?

Continue reading “Automating Testing with Python & Google Analytics”

Google Tag Manager: How to create a cookie

Creating a cookie in Google Tag Manager is very easy.

  1. Create a custom HTML tag
  2. Insert the cookie script below and update the following values to suit your needs:
    1. Name (name of the cookie, which can be anything you want)
    2. Value (the value the cookie should hold)
    3. Days (how many days until the cookie expires)
  3. Create the trigger i.e.  when should the cookie be created
  4. Publish and you’re done. Super Easy!

Code to create a cookie

Continue reading “Google Tag Manager: How to create a cookie”

Using Python to enhance & automate measurement

Recently I’ve started to find more and more use cases for Python in digital marketing measurement.

Below is a list of example applications:

  1. Scraping data from the web and storing the results in a google doc with the Python script set to run nightly on a schedule. Sometimes there isn’t a datastudio connector or an API. Scraping the data into a google spreadsheet gives you a regularly updated data source that plugs straight into Datastudio.


  1. Scraping data from Trustpilot reviews and building personas based on the language used in customer reviews.
  2. Accessing Google Search console data through the API and storing this data in a database. Google Search console only shows 90 days of data making it difficult to see the trend or do year on year comparisons.
  3. Automating the testing of an ecommerce site i.e. testing a channel is recorded correctly, the transaction tag fired and the transaction appeared in Google Analytics with the correct data.
  4. Scraping price data from a competitors website to keep an eye on how they compare.
  5. Cleaning data i.e. backend database
  6. Scraping news data to make PPC ad copy dynamic and highland relevant

Should you set Google Analytics up on the staging site?

A common scenario is for a new website to be built on a test staging server that requires tracking to be implemented. Ideally, you’d want to set all the tracking up on the test site but you don’t want this data going into the main Google Analytics Property since it’s not real data but you do want to be sure all of the tracking is in place and works correctly.

The best way I’ve found to do this is to:

Continue reading “Should you set Google Analytics up on the staging site?”

Why is Facebook data different to Google Analytics?

In short Facebook & Google Analytics use different attribution models.

Facebook Attribution

Provided you have the Facebook tracking pixel installed on your pages and you are pushing in a conversion(s) as an event:

Facebook will take credit for a conversion if the conversion happens with 28 days of clicking a Facebook ad or 1 days within viewing a Facebook ad i.e. the ad appeared in your Facebook feed.

Continue reading “Why is Facebook data different to Google Analytics?”

How to fix AdWords clicks discrepancy error in Analytics

Most of the time I see this error in Google Analytics two things are happening:

The Adwords Ad is targeting a page that has some sort of redirect
The redirect strips out the gclid parameter and Google Analytics records the session against the Organic channel.

If the site has recently moved from http to https check that the Ad destinations have been updated to the new URL.

In short you need to avoid sending traffic to any page that redirects. Strictly speaking redirects should not mess up tracking but I’ve lost count of how many time redirects are a part of a tracking problem.

There’s a couple of tools that can help here:

Check for redirects with: Redirect Path

Crawl the site with Screaming Frog and match the data up with the Ad destination URLS. If any of the Ad destination URLs redirect then update the destination URL in Adwords.

If you’re working on a site with thousands of pages then switch Screaming Frog for Deep Crawl which is web-based and handles large volumes better in my experience.

More info from Google Analytics help: