javascriptnode.jsgoogle-analyticsgoogle-analytics-apigoogle-analytics-4

How to setup custom event tracking with new Google Analytics (GA4) on the server?


I would like to send event data to Google analytics 4 to track conversions on my web app. I found this article: https://developers.google.com/analytics/devguides/collection/protocol/ga4/sending-events?client_type=gtag#send_an_event

First I made a custom event that looks like this: Custom event

I created an api secret and included my measurement_id.

I then tried sending data to GA4 with the example in the article:

  const measurement_id = `G-XXXXXXXXX`;
  const api_secret = `XXXXXXXXXXXXXXXXXXXXXX`;

  await fetch(
    `https://www.google-analytics.com/mp/collect?measurement_id=${measurement_id}&api_secret=${api_secret}`,
    {
      method: "POST",
      headers: {
        "User-Agent":
          "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36",
        "Content-Type": "application/json",
      },
      body: JSON.stringify({
        client_id: "123.123333",
        events: [
          {
            name: "conversion",
            params: { succes: true },
          },
        ],
      }),
    }
  );

I cannot get this to work. Does anyone have experience with this or done something similar that can shine some light on what is going wrong here?


Solution

  • It takes between 34 - 48 hours for data to be shown in the standard reports on google analytics web app. This is due to data processing latency.

    If its a new google analytics account it can take up to 72 hours for it to begin displaying data.

    The best way to test your requests its to look at the real-time report. it will show if its getting hits almost instantly.