Google Analytics (4) Measurement Protocol

If you have worked with Universal Analytics, you might have already stumbled upon the Measurement Protocol. This allows people to send data straight to Google Analytics using an HTTP endpoint. In other words, any device that can be connected to the internet can send data to Google Analytics. Another use case for the Measurement Protocol is sending offline data. In this article, I will give a bit more information about the Measurement Protocol, how you can use it to send data to Google Analytics, and how the new Google Analytics 4 Measurement Protocol is different from the original one.

If you haven’t upgrade to Google Analytics 4 yet, and you want to a lot of time on the migration, go check out my GA4 Tag Migrator tool.

Universal Analytics Measurement Protocol

As mention in my intro, you can use the measurement protocol to send data to Google Analytics using an HTTP endpoint. For those that are not that technical, each time you visit a website you are requesting files from a server using HTTP (GET request). However, you can also send data to a server using HTTP, for example when you submit a form with your name and email address. Your information is then sent to a server (POST request). If you want to understand better what these different kind of requests are, check out my article about HTTP Requests.

HTTP Request

The most important thing to know here is that you need three parts when sending information to an HTTP endpoint:

  • The Method (e.g. GET or POST)
  • The URL (e.g. google-analytics.com/collect)
  • The Payload

The method is always going to be a POST request when we are talking about Measurement Protocol. The URL specifies where the data is going, so for us this would be google-analytics.com. The payload specifies what needs to be sent to this URL. Within the Measurement Protocol, this will contain the information about the hit.

Payload

Below you can find some examples of a payload for Measurement Protocol. These consist of different URL parameters that each have specific information about the hit. Important: these URL parameters are added to the end of the URL and you can recognize them by the question mark sign (e.g. example.com/?url_parameter1=value1&url_parameter2=value2)

  • Hit Type
  • Client ID
  • Document Location (Page URL)

For a complete list for the Universal Analytics payload you can check Google’s Documentation

Measurement Protocol Hit Example

So to bring these two parts together I will show an example of sending information to Google using the Measurement Protocol. Let’s say we want to send a hit type (t) is Page View hit to the Google Analytics property (tid) UA-12345678-1 where user with Client Id (cid) GA1.2.2134208833.1619799528 visited the Page URL (dl) automation-help.com/about. The URL for Universal Analytics Measurement Protocol is www.google-analytics.com/collect. Then our HTTP request would look like this:

Method: POST
URL: www.google-analytics.com/collect
Payload:  dl=automation-help.com/about&v=1&tid=UA-12345678-1-Y&cid=GA1.2.2134208833.1619799528&t=pageview

You can use the Google Analytics Hit Builder to actually create this hit and send it to Google Analytics. Note that this only works for Universal Analytics

Google Analytics 4 Measurement Protocol

Please note that the Google Analytics 4 Measurement Protocol is still in alpha, which is the step before beta. In other words, Google will still make changes to the Measurement Protocol. The Measurement Protocol for Google Analytics 4 still consists of the three parts I discussed before (Method, URL, and Payload). The Method will stay the same (POST request). However, the URL and the payload are different for Google Analytics 4. The URL is www.google-analytics.com/mp/collect instead of www.google-analytics.com/collect.

Payload

In contrary to Universal Analytics, where the payload consisted of relatively easy URL parameters (e.g. v=1, tid=UA-12345678-1, t=pageview etc), Google Analytics 4 has a more complicated payload. We are still going to use URL parameters, but these will only specify the following:

  • api_secret
  • measurement ID

Once again, these URL parameters will be added to the end of the URL and can be recognized by the question mark sign (e.g. www.google-analytics.com/mp/collect?api_secret=12345&measurement_id=G-12345678). This will already specify exactly where our information needs to go. On top of that, we can add the information that needs to be sent to your Google Analytics 4 property using the HTTP request body. This request body is something different from your URL parameters and is normally written in JSON format.

How to get the API secret for Google Analytics 4 Measurement Protocol

Before you can start sending data to Google Analytics, you will need to obtain your API secret. In the steps below I will show you how you can obtain this

Time needed: 5 minutes.

How to get the API secret for Google Analytics 4 Measurement Protocol

  1. Navigate to your GA4 admin settings and click on your Data Stream

  2. Click on “Measurement Protocol API secrets”

  3. Click on “Create”

  4. Add a name for your Measurement Protocol

  5. Copy the value that is located under Secret value (highlighted in red)

Measurement Protocol Hit Example

In Google Analytics 4 you will not see page views anymore. Instead, everything is sent as an event. Page views are sent as events that are called screen_views. In the example below I am sending an event called “test_event” for the user with the client ID is GS1.1.1620221971.9.0.1620221971.0

{
    "client_id": "GS1.1.1620221971.9.0.1620221971.0",
    "events": [{
      "name": "test_event",
      "params": {
        "event_parameter1": "foo"
      }
    }]
}

You can test this event using Google’s GA4 Event Builder .

Normally whenever an HTTP request is sent correctly, you would receive a 200 response code and perhaps some text containing the word “success”. However, for the Google Analytics 4 Measurement Protocol, I have only received the response code 204. Don’t worry, the event is still sent to your Google Analytics 4 property. Go to your real-time report and look at your event counts to see the event you just sent.

I hope you have enjoyed learning how to use the Google Analytics Measurement Protocol for both Universal Analytics as Google Analytics 4. If you have any questions, do not hesitate to ask them below.

1 thought on “Google Analytics (4) Measurement Protocol”

  1. Pingback: Google Analytics 4 (GA4) - Tutorial [Extended Guide] - Automation Help

Leave a Comment

Your email address will not be published.