The Marketing Technologist.

We talk about analytics, code, data science and everything related to marketing technology. Backed by the tech geeks of Greenhouse.

Measure your Python projects with Google Analytics

It is easier than ever to automate your processes. Vendors provide well-documented APIs. Cloud solutions like Amazon Web Services and the Google Cloud Platform make it a breeze to upload a project to the cloud and run it at an interval of choice. But what I have often seen happening with new technologies also applies here: making it work is more important than the measurement of the new technology.

In this post, I'll share a basic example of how to measure Python projects with Google Analytics. If you don't use Python, it's pretty easy to set up something similar in a different programming language.

The code

You can easily measure something in Python by sending an http request using the Google Analytics Measurement Protocol. All you have to do is ping a URL in the correct format. I've created a basic function that does this for a Google Analytics event:

import requests

def track_google_analytics_event(event_category, event_action, event_label):			tracking_id = 'UA-XXXXX-X'	
	clientid_str = str(datetime.now())	
	tracking_url = 'https://www.google-analytics.com/collect?v=1&t=event&tid='+tracking_id+'&cid='+clientid_str+'&ec='+event_category+'&ea='+event_action+'&el='+event_label+'&aip=1'	
	requests.post(tracking_url)

Why would you want to measure your projects with a Google Analytics event? They work really well for tracking automation projects. I'll share some examples later. First, I'll explain what happens in this neat little function.

  • tracking_id: this is the Google Analytics Property ID you want to send the data to. You get the ID of your property when you set up a Google Analytics property.
  • clientid_str: the Measurement Protocol has a few required parameters. One of them is the client id. This is the value Google uses to stitch sessions and users together. As we don't need to track sessions and users in our projects, we just use a timestamp.
  • track_url: this is the http request we'll send. It includes the tracking_id and clientid_str. Besides that, it includes some other parameters:
  • event_category: the category of the event, filled out with the event_category function parameter.
  • event_action: the action of the event, filled out with the event_action function parameter.
  • event_label: the label of the event, filled out with the event_label function parameter.
  • aip: this is short for Anonymize IP. It sets the last octet of the IP to 0. This makes sure Google Analytics doesn't store a full IP address.

On to the next part: why event tracking works really well for measurement of automation projects.

Event tracking for automation projects

Google Analytics events have a hierarchical structure. The hierarchy is Category > Action > Label. This translates really well into our automation projects. We generally set up a Google Analytics property per automated process and structure the measurements like this:

track_google_analytics_event('project source', 'client name', 'function detail')

Here are two examples:

Example 1: Analytics Notification System

For our clients, we run an automated check on measurements that are used to optimise campaigns. We format our events in this structure:

track_google_analytics_event('project source', 'client name', 'function detail');

The possible values for the three parameters are:

  • event category / project source: the location where we've run the project from. We currently use AWS and have three possible sources:
  1. 'local' the source when we test the project locally,
  2. 'lambda' the source when we test the project in AWS,
  3. 'aws.events' the source when the project was triggered by an AWS CloudWatch event. This last source is what we call the live environment. By filtering on the source, we can choose to report only on the live activity of the project and ignore the test data.
  • event action / client name: the name of the client we run a test for, e.g. 'The Marketing Technologist'.
  • event label / function detail: the type of check that ran. In our case, there are four possible values: 'status report', 'tracking issue', 'funnel logic' and 'cro data'.

A full example looks like this:

track_google_analytics_event('aws.events', 'The Marketing Technologist', 'status report');

Here's what the data looks like in Google Analytics:

Screenshot of automation project event data in a Google Analytics report.

Example 2: Automated tag placement

Our automated tag placement system picks up tasks (that contain a tag request) from our ticketing system (Wrike) and places them as a tag in Google Tag Manager. For this project, we use a similar approach:

  • event category: project source.
  • event action: client name.
  • event label: ¬†empty, as we only have a single functionality currently.

A full example of this project looks like this:

track_google_analytics_event('aws.events', 'The Marketing Technologist', '');

Remember that we set up a different Google Analytics property per project. So there is no need to include the project name in one of the event parameters.

Start measuring your projects

With the help of the little function described in this post, we are able to measure how many automated data checks we run and how many tags we place with our automated system. It doesn't improve any functionality, but it does tell us how many tasks that we planned to automate we actually automated.

What automation project will you start measuring?