Tracking with the new Google Analytics 4 (GA4) server-side
Asked Answered
J

2

13

Google Analytics 4 (GA4) was recently released and made the default for new properties on the Google Analytics dashboard. I was checking it out and it looks great. We report a lot of our data to Google Analytics via our Node.js server as opposed to client-side using a library called universal-analytics (https://www.npmjs.com/package/universal-analytics), which works very well.

We want to start using GA4 asap but we cannot find any documentation on how to send events to a GA4 property server-side. There are only client-side examples and those don't seem to work at all on a server.

Simply put, what is the serverside equivalent for the following?

<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-ABC123ABC123"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'G-ABC123ABC123');
</script>

Anyone had success with this?

Jasminjasmina answered 6/11, 2020 at 18:47 Comment(0)
O
10

You can try to use measurement protocol v2: https://developers.google.com/analytics/devguides/collection/protocol/ga4/sending-events?client_type=gtag#send_an_event

Oldcastle answered 6/11, 2020 at 20:4 Comment(3)
Thank you this is helpful! I don't see anywhere to add custom parameters to pageviews like IP, userAgent, etc. Maybe that will come later?Jasminjasmina
The provided link seems to be broken. This one works: developers.google.com/analytics/devguides/collection/protocol/…Rousseau
Where to get clientId? I have JSP application and if I want to use server-side measurement protocol and send the measurement data from my server side java code then from where I get the clientId?Onerous
J
2

It's working perfectly

const measurementId = `G-XXXXXXXXXX`;
const apiSecret = `<secret_value>`;

fetch(`https://www.google-analytics.com/mp/collect?measurement_id=${measurementId}&api_secret=${apiSecret}`, {
  method: "POST",
  body: JSON.stringify({
    "client_id": "client_id",
    "events": [{
      "name": "add_payment_info",
      "params": {
        "currency": "USD",
        "value": 7.77,
        "coupon": "SUMMER_FUN",
        "payment_type": "Credit Card",
        "items": [
          {
            "item_id": "SKU_12345",
            "item_name": "Stan and Friends Tee",
            "affiliation": "Google Merchandise Store",
            "coupon": "SUMMER_FUN",
            "currency": "USD",
            "discount": 2.22,
            "index": 0,
            "item_brand": "Google",
            "item_category": "Apparel",
            "item_list_id": "related_products",
            "item_list_name": "Related Products",
            "item_variant": "green",
            "location_id": "L_12345",
            "price": 9.99,
            "quantity": 1
          }
        ]
      }
    }]
  })
});
Jorry answered 19/5, 2022 at 6:54 Comment(4)
thank your answer but where is client_id , how to get it?Ferrotype
developers.google.com/analytics/devguides/collection/protocol/…Jorry
This link just said uniquely identifies a user on browser but question is how to generate this id?Onerous
analyticsmania.com/post/google-analytics-client-idJorry

© 2022 - 2024 — McMap. All rights reserved.