Jason Biondo
Jason Biondo

Reputation: 834

How Do I Call Google Universal Analytics and GA4 Events Simultaneously?

I have universal analytics configured and send events using the gtag function but how can I also add in GA4? Is there a different function for GA4? If I call, the same gtag functions will it cause conflicts?

The documentation says to just add a new gtag config, but it doesn't say anything about referencing the events. Do I just duplicate the events and send them in different formats? If so, won't that count as duplicated events in both platforms?

https://developers.google.com/analytics/devguides/collection/ga4/basic-tag?technology=gtagjs

How can this be done cleanly with only sending events to each platform separately?

I am using this for UA. What is the GA4 version aside from syntax of the object being different?

window.gtag("event", "view_item", {
  items: [
    {
      id: productID,
      name: product.title,
      category: product.productType,
      price: product.priceRange.minVariantPrice.amount
    }
  ]
})

Upvotes: 2

Views: 1278

Answers (1)

Michele Pisani
Michele Pisani

Reputation: 14179

Here is the complete (example) gtag.js snippet you would use to collect data for a UA property with an ID of 'UA-XXXXXX-13' and a GA4 data stream ID of 'G-XXXXXXXXXX'.

<script async src="https://www.googletagmanager.com/gtag/js?id=UA-XXXXXX-13"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'UA-XXXXXX-13');
  gtag('config', 'G-XXXXXXXXXX');
</script>

The platforms are completely different and have nothing in common.

Here info to manage events: https://developers.google.com/analytics/devguides/collection/ga4/translate-events

If you currently use gtag.js

gtag('event', 'myCustomEvent', {
  "TheEventAction": "myEventAction1",
  "TheEventLabel" : "myEventLabel1"
});

If you currently tag for a Universal Analytics (UA) property using gtag.js and trigger an event as shown in the code sample above, the event will be automatically be sent to your Google Analytics 4 (GA4) property as well, provided that:

  1. You have added your GA4 measurement ID to the basic gtag.js snippet
  2. The event doesn't have an explicit send_to parameter assigned

Upvotes: 3

Related Questions