How to make gtag and gtm work with partytown in a react app - google-analytics

I need some help with google analytics, gtag and gtm scripts
This is how I am generating the partytown snippet in webpack ->
const snippetText = partytownSnippet({
debug: config.env !== 'production',
forward: [
'dataLayer.push',
'GoogleAnalyticsObject',
'ga',
'gtag'
],
resolveUrl: (url, _location, type) => {
const proxyScriptHosts = [
'www.google-analytics.com',
'www.googletagmanager.com',
];
if (type === 'script' && !!url && proxyScriptHosts.find((proxyScriptHost) => url.host.includes(proxyScriptHost))) {
const proxyUrl = new URL('https://my-proxy.com/api/proxy');
proxyUrl.searchParams.append('url', url.href);
return proxyUrl;
}
return url;
}
});
Then I insert this snippet in my index.html file like this -
<script type="text/javascript">{snippetText}</script>
Now I need to load 3 scripts for my app, this is where I need help to understand what I am doing wrong -
this is how I am loading the google analytics script -
<script type="text/partytown">
window.dataLayer = window.dataLayer || [];
window.gtag = function () {
window.dataLayer.push(arguments);
}
window.GoogleAnalyticsObject = 'ga';
window.ga = function () {
window.ga.q = window.ga.q || [];
window.ga.q.push(arguments);
};
window.ga.l = 1 * new Date();
</script>
<script type="text/partytown" async defer fetchpriority="low" src="https://www.google-analytics.com/analytics.js"></script>
in one of my react components, i am initializing google analytics like this ->
window.gtag('js', new Date());
window.gtag('config', gaId, options);
This works perfectly fine, i can see google analytics UA4 requests in the network tab working as expected!!
2. this is how i am loading the gtag script -
<script type="text/partytown" async defer fetchpriority="low" src="https://www.googletagmanager.com/gtag/js?id=${gtagId}"></script>
this is how i am loading the gtm script -
<script type="text/partytown" async defer fetchpriority="low" src="https://www.googletagmanager.com/gtm.js?id=${gtmId}"></script>
<script type="text/partytown">
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({ 'gtm.start': new Date().getTime(), event: 'gtm.js' });
</script>
Gtag and GTM scripts aren't working, none of the events are being sent for them, please help me understand. I need ga, gtag, and gtm all 3 in my case working together
Please help me with this, i've been stuck on it for quite some time, need to understand the right way to do this
I have searched in the official partytown docs and read various blogs online, this is as far as I've gotten

oo very interesting, this is my advice: To get gtag and GTM working, you need to make sure the following things:
You have created a gtag tracking code in your Google Analytics account and have added the tracking ID in your code: (reCheck)
'<'script type="text/partytown" async defer fetchpriority="low"
src="https://www.googletagmanager.com/gtag/js?id=${gtagId}"></script'>'
You have created a GTM container in your Google Tag Manager account and have added the container ID in your code: (reCheck)
<script type="text/partytown" async defer fetchpriority="low" src="https://www.googletagmanager.com/gtm.js?id=${gtmId}"></script>
Make sure the data layer is correctly initialized before the GTM script is executed:
<script type="text/partytown">
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({ 'gtm.start': new Date().getTime(), event: 'gtm.js' });
</script>
Verify that your GTM container is configured to fire the gtag tracking code, which will send the data to Google Analytics.
If you have done all the above steps correctly, and still the events are not being sent, try checking the network tab in the browser's developer tools to see if there are any errors being returned for the gtag or GTM requests. If there are errors, fix them, and try again.

Related

Google Signals/Remarketing - check if it's enabled?

We are trying to use Google Signals (https://support.google.com/analytics/answer/7532985?hl=en) for remarketing purposes but we are unsure if it is correctly enabled. We are using the following code for our implementation:
​​<script async
src="https://www.googletagmanager.com/gtag/js?id=UA-XXXXXXXXX"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag() {
dataLayer.push(arguments);
}
if (!cookieManager.googleRemarketing) {
gtag('set', 'allow_google_signals', false);
}
gtag('js', new Date());
gtag('config', 'XXX', {
'anonymize_ip': true,
'optimize_id': 'XXX'
});
</script>
We are not using any additional google ads/remarketing code, but we have enabled "Tracking-Information" => "Data collection" => "Data collection for Google Signals" (the translations might vary, because we don't use analytics in English) in Google Analytics.
It seems like there is no cookie with the name "IDE" set, like it was before when we used the normal Google Remarketing Code. Did that cookie got removed/replaced in google signals?
If so, how can we check if google signals is working correctly?
The Tag Assistant Plugin for Chrome just shows the following:
Thank you!
In your snippet you have:
gtag('set', 'allow_google_signals', false );
so it disable all advertising features.
https://developers.google.com/analytics/devguides/collection/gtagjs/display-features
For this reason you can't see any IDE cookie.

How to not send data to GA when debugging locally?

As the documentation says:
There is a debugging version of Google Analytics that will print extra info to the console for debugging purpouses. However, this version will send data to GA even when it is only for debugging.
According to this documentation (that is a bit outdated), we need to add this code to our Google Analytics code to avoid sending hits to GA:
if (location.hostname == 'localhost') {
ga('set', 'sendHitTask', null);
}
However, I'm using a newer version of GA that uses gtag in the tracking code, So I've change the ga function to gtag:
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-134628373-1"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-134628373-1');
if (location.hostname == 'localhost') {
gtag('set', 'sendHitTask', null);
}
</script>
Just for clarification:
if (location.hostname == 'localhost') {
ga('set', 'sendHitTask', null);
}
to:
if (location.hostname == 'localhost') {
gtag('set', 'sendHitTask', null);
}
Is this the correct approache? I don't want to mess my data.
I'm using GTM to deploy the GA code. In order to make the changes to the GA tracking code, I've used a Custom HTML Tag.
There is a slightly different implementation for gtag. You can set the following window property to true in the conditional statement:
window['ga-disable-GA_MEASUREMENT_ID'] = true;
Replace GA_MEASUREMENT_ID with the Analytics ID of the property that you would like to disable.
This window property must be set before any calls to gtag() are made, and it must be set on each page for which you want to disable Analytics. If the property is not set or set to false, then Analytics will work as usual.
More info in link below. Hope it helps.
gtag ga-diasble setting

Analytics pageview/ event code is result in stats

I am trying to get statistics about sign ups on my website.
The analytics code is in the header:
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=...."></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag() { dataLayer.push(arguments); }
gtag('js', new Date());
gtag('config', '....');
</script>
Then I have a form and sending it (with a page reload, not ajax) and in the success snippet in razor I placed the ga code:
#if (success)
{
<script type="text/javascript">
ga('send', 'event', 'Supplier sign up', 'click', '/signup/success');
</script>
<p>success message</p>
}
The success message appears and I can see the script in the page inspect but I don't get any stats in Google Analytics.
Am I missing something? In the past (a few years ago) I was using a similar code and it worked. Has anything changed recently? Do I need to enable something in the Analytics or my code is wrong?
I am following the documentation:
https://developers.google.com/analytics/devguides/collection/analyticsjs/events
https://developers.google.com/analytics/devguides/collection/analyticsjs/pages
The implementation method for your GA is through gtag.s, not GTM or analytics.js.
Thus you need to modify your success to:
#if (success)
{
<script type="text/javascript">
gtag('event', 'click', {'event_category': 'Supplier sign up', 'event_label': '/signup/success'});
</script>
<p>success message</p>
}
You have mixed two GA libraries. You use gtag.js for page view, so you need to use gtag.js (not analytics.js) for events.
https://developers.google.com/analytics/devguides/collection/gtagjs/events

Why isn't gtag sending ad conversion tracking?

I'm using gtag.js to track Google Ads.
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=AW-abc123"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'AW-abc123');
</script>
This is the code to trigger my Google Ads Conversion Tracking pixel:
var callback = function () {
if (typeof(url) != 'undefined') {
window.location = url;
}
};
gtag('event', 'conversion', {
'send_to': 'AW-abc123/12345',
'event_callback': callback
});
When I load my page with a fresh cache, I can see my conversion pixel being fired:
However, after a page reload, the conversion no longer fires:
What is causing the conversion tag to stop firing?
Why do you think that tag doesn't fire? Blue color in Tag Assistant indicates non-standard implementation etc. Try to click on your tag in Tag Assistant and see details.

Global Site Tag (gtag) doesn't send anything

I've set up a new Analystics account, and copied over the tracking code. It's of the kind
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-XXXXXXXX-X"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-XXXXXXXX-X');
</script>
I then track an event using
gtag('event', 'click', {
event_category: 'acquisition',
event_action: 'click',
event_label: 'download',
event_value: 1
});
I've tried all kinds of gtag() syntax, however nothing ever gets sent to Analytics. When checking the Network tab in Chrome debug tools, there is no communication with Google at all. (the dataLayer array is properly populated)
I don't want to use Google Tag Manager, just looking for a simple "track event via Javascript".
Looks like it is working, might be a local computer/browser addon that's blocking the call to GA

Resources