How to Set Up Measurement Protocol (MP)

Topics: , ,

In our previous article we introduced what the Measurement Protocol (MP) actually is, what its pros and cons are and how it can be used, including specific examples. Now let's look at how an MP setup is actually done.

Basic MP Setup Procedure

1. Creating a GA4 Property and a Data Stream

Before you start setting up the Measurement Protocol, you must have a GA4 property and a data stream created. These steps are necessary to obtain the measurement identifier (Measurement ID), which will also be used in the HTTP requests.

  1. Creating a GA4 property: Log in to your Google Analytics account and create a new GA4 property.
  2. Setting up a data stream: After creating the property, also add a new data stream (web, iOS or Android). This gives you the Measurement ID (e.g. G-XXXXXXX), which you will need for sending data.
2. Building the HTTP Request

The Measurement Protocol uses HTTP GET or POST requests to send data. The structure of the request is as follows:

  • Endpoint: https://www.google-analytics.com/mp/collect
  • Parameters:
    • measurement_id: the Measurement ID of your data stream
    • api_secret: the secret API key generated in the GA4 console
    • Other event parameters, such as client_id, user_id, timestamp_micros, non_personalized_ads, etc.

Example of a GET request:

https://www.google-analytics.com/mp/collect?measurement_id=G-XXXXXXX&api_secret=YOUR_API_SECRET&client_id=CLIENT_ID&events=[{event_name: 'purchase', params: {value: 23.07, currency: 'USD'}}]
3. Sending the Requests

Once you have built the HTTP request, you can send it from various places:

  • Server-side: From your server's backend using libraries such as requests in Python or axios in Node.js.
  • Client-side: Directly from the browser using JavaScript, but it is recommended to minimise this approach for security reasons.

Using Google Tag Manager (GTM)

Google Tag Manager (GTM) can be used as an intermediary for sending data to GA4 using the Measurement Protocol.

GTM Setup
  1. Creating a new tag: In the GTM console create a new tag of the type "Custom HTML Tag" or "Custom Image Tag".
  2. Configuring the tag: Insert the code for sending the HTTP request into the tag.
  3. Triggering the tag: Set up a trigger for the tag according to when you want to send events (e.g. on page load or on a click on a certain button).
Example of a Tag (Custom HTML Tag)
<script>
  var client_id = 'YOUR_CLIENT_ID';
  var measurement_id = 'G-XXXXXXX';
  var api_secret = 'YOUR_API_SECRET';

  var url = `https://www.google-analytics.com/mp/collect?measurement_id=${measurement_id}&api_secret=${api_secret}&client_id=${client_id}&events=[{event_name: 'page_view'}]`;

  fetch(url, { method: 'GET' });
</script>

Would you like to measure user interactions in detail and flexibly, beyond the standard web and mobile platforms? Then the Measurement Protocol is the perfect tool for it and our analytics team will set it up for you without any problems!
Improve the accuracy and scope of your analytics data. Contact us!