Google Analytics Measurement Protocol API

Sending Events via Google Analytics Measurement Protocol API

The rise in data protection and privacy regulations has created new challenges that have serious implications on the entire digital industry. Web browsers like Safari and Mozilla have released a range of updates, like Intelligent Tracking Prevention (ITP), that work against tracking technologies by blocking third-party cookies and applying restrictions on the first-party cookies as well.

In order to prepare for this, companies like Google and Facebook have developed a new tracking method called server to server. It allows businesses to send data and events from their servers back to the ads’ servers.

Google Analytics Measurement Protocol is one of the solutions that allow advertisers to overcome those restrictions and collect the data in the backend and send it directly from the server to Google Analytics Servers via the API. Today, I will walk you through the implementation steps to set it up.

1- Structure your Payload

To send event data via the measurement protocol we need to make POST requests to this API end point

https://www.google-analytics.com/collect


The following parameters are required for any payload:

v=1              // Version.
&tid=UA-XXXXX-Y  // Tracking ID / Property ID.
&cid=555         // Anonymous Client ID.
&t=event             // Hit Type. In our case it should be 'event'

To send event data specifically, we need to post those parameters:

&t=event         // Event hit type. Required.
&ec=server        // Event Category. Required.
&ea=signup         // Event Action. Required.
&el=popup      // Event label. Optional.
&ev=300          // Event value.Optional

Your final post URL should be something like this:

https://www.google-analytics.com/collect?v=1&tid=UA-XXXXX-Y&cid=555&t=event&ec=server&ea=signup&el=popup&ev=300

Once you’re done with structuring your payload, you can go ahead and ask your developer to make an HTTP POST request to the endpoint above. If you are looking to set up this yourself, you may continue to the next step.

2- Send Events through the API

Most programming languages can do the job here and make a POST request to Google Analytics servers. I recommend using the language you’re familiar with or your website is built upon. In this example, I will be using Python to make the call via the request library.

import requests

url = 'https://www.google-analytics.com/collect'
ga_id = 'UA-XXXXX-Y'
ga_cookie = request.COOKIES.get('_ga')
client_id = ga_cookie.split('.',2)[2]   

payload = {
            'v': 1,
            'tid': ga_id,
            'cid':client_id,

            't': 'event',
            'ec': 'server',
            'ea': 'signup'
        }
r = requests.post(url, data=payload)

The code example is very simple. As for the client id, I am capturing it from the Google Analytics first-party cookies named _ga. This line of code is more relevant to the Django framework, so you may need to change it


What I do next is to split it and take the number after GA1.2. , this should be the client id that Google Analytics uses to match the event data to the user’s data.

If you’d like to send more data like e-commerce tracking, I’d recommend going through the Google Analytics developer guide, it has all details about the parameters you need to send in each payload. If you have any questions, please leave a comment.

Leave a Comment