Creating a Tracking Pixel using Google Analytics v4 Measurement Protocol

2.7k Views Asked by At

I'm trying to create a tracking pixel using Google Analytics 4. From what I've read in the docs, you can only send a POST to create events. Here is an example:

curl --request POST \
  --url 'https://www.google-analytics.com/mp/collect?api_secret=XXXXXXXXXX&measurement_id=G-XXXXXXXX' \
  --header 'Content-Type: application/json' \
  --data '{
    "client_id": "555",
    "events": [{
        "name": "test_event",
        "params": {
            "session_id": "123",
            "engagement_time_msec": "100"
        }
    }]
}'

This of course works. However, I want to create a tracking pixel that I can use to track things like email opens via an HTTP GET (via the call from the <img />'s src). With universal google analytics, this is done via:

<img src="https://www.google-analytics.com/collect?v=1&tid=G-XXXXXX&cid=555&t=event&ec=email&ea=open&dp=%2Femail%2Ftest&dt=test" alt="" height="1" width="1">

Am I missing something here or is it no longer possible to create a tracking pixel like the one above, but for GA v4?

I even found a thread on google's support page for this but no responses and has been locked.

I did try just adding a pixel like so, but no luck:

<img src="https://www.google-analytics.com/collect?v=4&tid=G-XXXXXXXX&cid=555&t=event&ec=email&ea=open&dp=%2Femail%2Ftest&dt=test" alt="" height="1" width="1">

I also tried using https://www.google-analytics.com/mp/collect in the img src, but http get is not allowed for that endpoint so it just errors out.

2

There are 2 best solutions below

4
On

The Measurement Protocol is designed for use server side to send events. No pixel is used, just a POSTed message with a secret key. Note the key is secret, so should never be used on the client side.

If you are client-side on an HTML page, then you should use the normal gtag to send events.

4
On

I had the same issue. I resolved it by using the following code:

<img src="https://www.google-analytics.com/collect?v=2&tid=G-xxxxxxxxxx&cid=555&t=event&en=eventName">

Note it is v=2 and not v=1 or v=4.