google analytics python measurement

Track & share your Python scripts use through GA’s measurement protocol

I was recently working on a small side project and I wanted a quick and easy way to track the most basic uI was recently working on a small side project and I wanted a quick and easy way to track the most basic usage statistics of a couple of python scripts. I wanted this measured in a way that I could share the statistic in a real-time dashboard without having to spend time creating and publishing that actual dashboard.

I decided to go with a little work-around which proved easier and a lot more convenient for sharing those statistics than I expected. This should also work for non-python applications or scripts al long as you can send post request.

Also, this way it is completely free and should work for any jupyter notebook, flask, streamlit or python application.

All that is required is a working Google Analytics account, and the python http requests library.

Why use Google Analytics?

Because Google Analytics does the dashboarding for you. It lets you create a separate view, and easily share that view while allowing you to allocate rights as you see fit.

You can use all the GA functions as emailing a daily report or setting usage alerts. All while having a real time dashboard for free.

But perhaps the greatest advantage of using Google Analytics is just that its user-friendliness and familiarity outside of the analyst and marketing circles that just makes it more likely you will circumvent the common reluctance of ‘yet another tool to have to learn’ when sharing.

How to set it up

All you have to do is make your script send an http post request using the Google Analytics Measurement Protocol.

This can be done in just a few lines of python code.

'''
import requests
def track_ScriptRun(event_category, event_action, event_label):
    tracking_ID = 'UA-XXXXXX-X'
    client_ID = '12345678'
    tracking_URL = 'https://www.google-analytics.com/collect?v=1&t=event&tid=' + tracking_ID + '&cid=' + client_ID + '&ec=' + event_category + '&ea=' + event_action + '&el=' + event_label + '&aip=1'
    requests.post(tracking_URL) 
'''

(All this function does (when called) is concatenate the google-analytics.com/collect URL with all the needed parameters (key value pairs) and then sends it as post request)

Here is a description of the 3 ‘payload parameters’ that need to be present in the post request:

track python script use with google analytics

On top of this we have the ‘event parameters’. These are the parameters that we can actively use to differentiate usage between projects, scripts or functions.

These event parameters are so useful because they are hierarchical. This enables you to very easily use this piece of code for several different function or subsets of your projects (or different projects altogether) by just changing the corresponding one of the three event parameters.

The event parameters are the ‘category’, ‘Action’ and ‘Label’ key-value pairs as depicted below.

Let’s say we have a project called ‘Project_X’ that consists of two scripts (creatively called ‘Script_1’ and ‘Script_2’) and within each of these two scripts we have user option A and user option B. We can now easily monitor and share the usage for the whole project down to the user option level with only the code and parameters from before.

All we do now is call the function we already created above within each to be measured part of the script and fill in the correct event parameters for that part.

'''
track_ScriptRun('Project_X', 'Script_1', 'optionA');
'''

And that is it.

You can now monitor the usage in real time in google analytics. I suggest creating a separate view for this so that when sharing it doesn’t share other statistics from the GA account you do not wish to share.

Google Analytics screenshot for python script measurement

(You can switch from the realtime view to the usage statistics over a certain period by using the menu on the left to move to ‘behavior’ > ‘events’. Here you will also have the event label statistics.)

I hope this helps someone looking to get more insights into the usage of their python scripts or applications!

To install the request library (documentation can be found here): https://requests.readthedocs.io/en/latest/

'''
$ pip install requests
'''

Setting usage alerts in google analytics: 
https://support.google.com/analytics/answer/1033021?hl=en

Google Analytics Measurement Protocol: 
https://developers.google.com/analytics/devguides/collection/protocol/v1/reference

Measurement Protocol Parameter Reference: 
https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters 
(&aip=1 anonymizes the IP address)

Example code:

'''
import requests

def track_ScriptRun(event_category, event_action, event_label):
    tracking_ID = 'UA-XXXXXX-X'
    client_ID = '12345678'
    tracking_URL = 'https://www.google-analytics.com/collect?v=1&t=event&tid=' + tracking_ID + '&cid=' + client_ID + '&ec=' + event_category + '&ea=' + event_action + '&el=' + event_label + '&aip=1'
    requests.post(tracking_URL) 

track_ScriptRun('Project_X', 'Script_1', 'optionA');
'''