google-analyticsgoogle-analytics-apigoogle-analytics-4universal-analytics

Migration from UA to GA4. Not able to receive event on GA4 dashboard


I have a existing UA dashboard with property ID (say UA-ABC). Now created a GA4 dashboard and have a measurement ID ( say GA-XYZ ). I was using a below python script to send the data to UA dashboard. Now replaced the UA tracking ID in the script with GA4 measurement ID. Not able to receive the events in the GA4 dashboard.

import requests

MEASUREMENT_ID = 'G-XYZ'
CLIENT_ID = '555'
EVENT_CATEGORY = 'video'
EVENT_ACTION = 'play'
EVENT_LABEL = 'holiday'


payload = {
    'v': '1',  
    'tid': MEASUREMENT_ID,  
    'cid': CLIENT_ID,  
    't': 'event',  
    'ec': EVENT_CATEGORY,  
    'ea': EVENT_ACTION,  
    'el': EVENT_LABEL,  
}


response = requests.post('https://www.google-analytics.com/collect', data=payload)


if response.status_code == 200:
    print('Event successfully sent to Google Analytics')
else:
    print('There was an error sending the event to Google Analytics')

Note: The events are going to the UA dashoard if I use UA tracking in the script.


Solution

  • You appear to be using the Measurement Protocol (Universal Analytics) which is designed to send hits to a universal analytics account.

    Endpoint

    POST /collect HTTP/1.1
    Host: www.google-analytics.com
    
    payload_data
    

    If you want to send hits to a GA4 account then you need to use Measurement Protocol (Google Analytics 4)

    The endpoint is also diffrent

    POST /mp/collect HTTP/1.1
    HOST: www.google-analytics.com
    Content-Type: application/json
    <payload_data>
    

    Remember ga4 is not the same as UA your going to have to redesign your hits and convert them to events.

    Event Payload example

    {
    "client_id": "x",
    "events": [
      {
        "name": "offline_purchase",
        "params": {
          "engagement_time_msec": "100",
          "session_id": "123"
        }
      }
    ]
    }
    

    That and the measurement protocol for ga4 is extremally limited. You will probably not be able to send all the hits you want to most of them are locked down for use by googles internal sdk's. Have a look at the event-builder you will be able to see what you can send.