Unable to send custom variable/value/event to GA4
Asked Answered
A

1

0

I want to submit an extra variable to GA4 upon page load. But I don't see my variable anywhere in Google Analytics and I don't see it mentioned in the Google Chrome>Developer Tools>Network>Post Request to https://www.google-analytics.com/g/collect.

This is the web page I created:

<!DOCTYPE html>
<html>
<head>
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-YOURIDHERE"></script>
<script>
  const gid = 'G-YOURIDHERE';
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());
  gtag("event", "athlete_type", {
    "event_category": "hockey",
    "event_label": "hockey",
    "value": "hockey"
  });
  gtag('config', gid );
</script>
</head>
<body>
</body>
</html>

Replace G-YOURIDHERE with your own google analytics ID. My goal is to use Google Analytics to keep track of each athlete_type and when they visited my website. Simply put, I want to use Google Analytics as a time-based key-value store. You can see that I tried using fields like event_category, event_label, value but none of those are being transmitted by my webpage to Google Analytics.

However things like gtag('config', gid, {page_path: 'anything-i-want'}); does properly send. I can see anything-i-want under Real Time Reports>Event Count by Event Name>page_view>page_path>anything-i-want. So I know my google analytics account is functional.

What am I doing wrong? How do I send a custom variable to GA4?

Autocephalous answered 1/1 at 16:47 Comment(0)
W
1

It can be checked, if it works, when this line of code:

gtag("event", "athlete_type", {
  "event_category": "hockey",
  "event_label": "hockey",
  "value": "hockey"
});

is used after this line of code:

gtag('config', gid );.

The updated code is as follows:

<!DOCTYPE html>
<html>
<head>
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-YOURIDHERE"></script>
<script>
  const gid = 'G-YOURIDHERE';
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());
  gtag('config', gid );
  gtag("event", "athlete_type", {
    "event_category": "hockey",
    "event_label": "hockey",
    "value": "hockey"
 });
</script>
</head>
<body>
</body>
</html>
Woodring answered 1/1 at 19:28 Comment(2)
Yes, that worked!!! How come the order matters? Is there a particular google documentation you can suggest I read to understand this?Autocephalous
Regarding documentation about Google Analytics 4 event setup, using gtag.js API, this link can be checked: developers.google.com/analytics/devguides/collection/ga4/….Woodring

© 2022 - 2024 — McMap. All rights reserved.