Search code examples
google-analyticsanalyticsmeasurement-protocolgoogle-analytics-4

Google Analytics 4 - Measurement Protocol API used without gtag.js or firebase


Is it possible to use GA4 Measurement Protocol to send events to Google Analytics and view and analyze them in the GA dashboard without using gtag.js or any other front-end script? The use case would be that some events are being sent to my server and I will just push these events to GA through the API.

One thing that makes me think is that the official Measurement Protocol API say:

In order for an event to be valid, it must have a client_id that has already been used to send an event from gtag.js. You will need to capture this ID client-side and include it in your call to the measurement protocol. In send an event to your property, we use "client_id" as the client_id. You will need to replace this with a real client_id that comes from gtag.js.

(https://developers.google.com/analytics/devguides/collection/protocol/ga4/verify-implementation?client_type=gtag#client_send)

That suggests that only events that have a valid client_id that originate from gtag.js will be counted. I did some experimenting with randomly generated client_ids and what I discovered was that I was able to see my events in the Realtime section of the GA4 console (the Event count by Event name section), but all the other sections would be empty and the Users in last 30 min section would always show 0.

Can someone please explain to me why it's zero and if such a use case is valid at all? Thanks


Solution

  • It is possible to use GA4 directly without gtag.ja or the Firebase SDK. Its not supported, so it takes some work. We have this working in a desktop app reasonably well. There a couple things that need to be done.

    1. As stated elsewhere the "engagement_time_msec" param must be set using the "_et" parameter. This is the number of milliseconds between now and the previous event.

    2. The client id "cid" has a specific format; it should be:

      "randomNumbers(10).unixTimeStamp()"

    3. The session id "sid" format is:

      "randomNumbers(10)"

    4. The "_z" parameter needs to be set. I think this is a cache buster. Looking deep into the gtag.js code it is a url safe base64 encoding of "CCD", which always results in the value "ccd.v9b"

    5. The page hash parameter "_p" can be set to this; not totally sure its correct but it works.

      "randomString(3).randomString(3)"

    6. Set the "User-Agent" HTTP request header in whatever framework/lib you are using. GA4 uses this to determine many things including Operating System. You will need to create a fake user agent based on the local device information. This is what we use for a Windows 11 x64:

      "myco.testapp/4.0.0 (Windows NT 10.0; Win64; x64)"

    7. The IP will be taken fromn the web request which is where the geolocation data comes from.

    Since a full working example is worth 1,000 words of documentation; here is a "test" event with a parameter "animal=dog":

    https://www.google-analytics.com/g/collect?cid=0078745494.1659679529&_et=364&_p=pfJ.Aev&seg=1&sid=2678664821&tid=G-???&ul=en&v=2&_z=ccd.v9b&en=test&ep.animal=dog