Send Web events to GA4 using Measurement Protocol

1. Introduction

Last Updated: 2019-04-30

What you'll build

  • In this codelab, You will learn how to send external events to GA4 using Measurement Protocol.
  • This codelab assumes that you already have a Webpage/site with Google Analytics 4 implemented.

What you'll learn

  • Steps to make your first MP call
  • Understand parameters required for the call
  • Send and validate your test call
  • Build a sample script in Python to make the call

What you'll need

  • A Website/Webpage
  • Any IDE to make changes
  • GA4 Account
  • Optional - Python development environment (or Colab)

2. Collect the mandatory fields

Create API Secret in GA4

Navigate to GA4 and create your new API secret by navigating to Admin > Data Streams > choose your stream > Measurement Protocol > Create

8dee89e850ae90bb.png

d0be19d8112435a2.png

de5a80a1445d3a14.png

You can provide any nickname, and the secret value will be displayed, which you can use in your call

Collect client_id

You can use any of the below methods to collect your client_id.

  1. Collecting via gTag

Both of these are explained in detail below

Collecting via gTag

You can retrieve you client_id via your website using :

gtag('get', 'UA-XXXXXXXX-Y', 'client_id', (clientID) => {
  Do something with clientID)
}

Below are more links depending on your infrastructure

3. Constructing the Call

You can build a sample call using the Event Builder in GA4. (This requires you to login and have cookies enabled). Make sure the toggle is set to "gtag.js"

b904d2a053eb7817.png

You shall need to fill in the following fields

  • api_secret - Already created earlier on GA4
  • measurement_id- To get this, you can navigate to Admin > Data Streams > choose your stream . It should be displayed as below

d362992ff592c675.png

  • client_id - You have already retrieved this value
  • user_id is not mandatory. You can leave it blank for now
  • Category - change this to "Custom" from the dropdown, and put in any event name of your choice (don't use any auto collected event). Here we are using "test_from_codelab"

cf580a708bb0a8a2.png

Optionally, you can also choose to provide event parameters and/or user properties by clicking on the buttons below

da59bb982f6f906a.png

Once you have filled in everything, you should see something like this, with a button to "Validate Event"

31f720d550906298.png

Once you reach this, click on "VALIDATE EVENT" , the button highlighted in orange. It should display the message below, specifying that the event is valid, and you shall see a button to now "SEND TO GA". At this point, if the event comes up as invalid, the tool will tell you the exact field where there is an issue, and you can fix that and retry

1d63d1e067c46294.png

You can now click on the button, and it should send a test event to GA4

4. Validating events in GA4

Once you have sent the event, you can navigate to your GA4 account and check Realtime. You should see the event come through

7b9e9a0f74b64757.png

It could take around 24 hours for the events to propagate from realtime view to the actual events reporting tab, so no need to worry if you don't see this in the regular event reporting immediately!

5. Building a Python Script

Now that you have tested it out, you can examine the API call, and the event payload to build a similar architecture in Python (or in any language of your choice) that can make this call. You can then schedule this at your desired frequency and operationalize it. For this part, you can either use any IDE of your choice that supports Python or just use a Google Colab notebook which does not require any installation on your device

Referring back to the GA4 Event Builder, you will see that the endpoint is as below

POST /mp/collect?measurement_id=XXXX&api_secret=XXXX 
HTTP/1.1
Host: www.google-analytics.com

The event payload was as below

{
  "client_id": XXXX,
  "non_personalized_ads": false,
  "events": [
    {
      "name": "test_from_codelab",
      "params": {
        "test_param": "test_123"
      }
    }
  ]
}

You can translate this to python by using something like this

import requests
import json
url = "https://www.google-analytics.com/mp/collect?measurement_id=XXXX&api_secret=XXXX"
payload = {
  "client_id": XXXX,
  "non_personalized_ads": false,
  "events": [
    {
      "name": "test_from_codelab",
      "params": {
        "test_param": "test_123"
      }
    }
  ]
}
r = requests.post(url,data=json.dumps(payload),verify=True)
print(r.status_code)

Once you execute this with the right values, you should similarly see the event reflect in realtime in GA4.

6. Congratulations

Congratulations, you've successfully used Measurement Protocol in GA4. Now you can build powerful solution architectures to send more meaningful data to Google Analytics and improve your marketing and business analytics. In order to make the most of this, we also suggest connecting to Google Ads, and importing these events as conversions.

You've learned

  • How to collect the right variables to make MP calls
  • How to send and validate test events
  • How to build a script to send MP calls

Reference docs