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

Issue

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?

Solution

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

However, it is currently in alpha version.

enter image description here

Answered By – Michele Pisani

Answer Checked By – Clifford M. (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.