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
Related
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.
My goal is to have user email address in google analytics reports so I can build custom reports for client.
For this, I added a custom dimensions "cdUserEmailAddress" under admin --> custom definitions --> custom dimensions
I set it's scope to session.
I set the following code in my page to add a test value but it does not add the value anywhere that I can find on google analytics dashboard.
<script>
window.dataLayer = window.dataLayer || [];
function gtag() { dataLayer.push(arguments); }
gtag('js', new Date());
dataLayer.push({
'cdUserEmailAddress': 'test#test.com',
'event': 'sessionUserLoggedIn'
});
gtag('config', 'UA-xxxx-x');
ga('create', 'UA-xxxx-x', 'auto');
ga('set', 'cdUserEmailAddress', 'test#test.com');
ga('send', 'pageview');
console.log('working 2');
</script>
This did not work.
I then went to Google TagManager (since one of the articles I read suggested I need to set that up too) and added a user data element there.
Pasted the auto generated code shown below but that did not help either.
<!-- Google Tag Manager -->
<script>(function (w, d, s, l, i) {
w[l] = w[l] || []; w[l].push({
'gtm.start':
new Date().getTime(), event: 'gtm.js'
}); var f = d.getElementsByTagName(s)[0],
j = d.createElement(s), dl = l != 'dataLayer' ? '&l=' + l : '';
j.async = true; j.src =
'https://www.googletagmanager.com/gtm.js?id=' + i + dl;
f.parentNode.insertBefore(j, f);
})(window, document, 'script', 'dataLayer', 'GTM-xxx');</script>
<!-- End Google Tag Manager -->
What am I missing?
I looked everywhere for this test#test.com value ...by creating a custom report, under Behavior -> Site content --> AllPages...
I find it no where.
Please help
Thanks
First of all, you should be aware, that sending email (or any other personally identifiable information) to Google Analytics is violating the terms of service.
Newertheless, if you decide to go for any other custom data, the following should be changed in your code. Custom dimensions and metrics are not referenced by their names, but by their ID, which can be looked up in the administration panel of Google Analytics. For further details please check this detailed guide.
So this part:
ga('set', 'cdUserEmailAddress', 'test#test.com');
Becomes:
ga('set', 'dimension1', 'your non-pii data'); //update the number according to your settings
Also, please note, that generally it's not suggested to send data to the same Analytics property directly from ga() calls and GTM, as you need to maintain your tracking settings in parallel, and you can easily send pageview twice, which is usually not desired.
If you decide to go for GTM, then you need to set up a dataLayer variable, that references your key used in the dataLayer (cdUserEmailAddress in your present case), and you need to use this variable in the Universal Analytics settings, where you can set up custom dimension values, also by referring their IDs.
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
Our goal here is to keep Google Analytics conversion goal tracking data when we load an iframe from another domain name that we own.
We have a domain rentalbookingsoftware.com that on the free trial page: https://rentalbookingsoftware.com/free-trial-signup/ loads an iframe for our free trial installer from sidev2.info like this:
<iframe width="80%" height="500" src="https://sidev2.info/freetrialform.php"></iframe>
We are trying to keep the gtag.js clientid the same but a new session is started on the sidev2.info gtag tracking which causes the correct referrer information to be lost as you can see from the gtag recording: https://www.screencast.com/t/l5wsqUF6oAg . Both sites are using this tracking code as suggested from: https://developers.google.com/analytics/devguides/collection/gtagjs/cross-domain and I have added sidev2.info to the referrer exclusion list: https://support.google.com/analytics/answer/2795830
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-45446232-1"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-45446232-1', {
'linker': {
'domains': ['rentalbookingsoftware.com', 'sidev2.info']
}
});
</script>
How can we keep the clientid using gtags? I found this: https://developers.google.com/analytics/devguides/collection/analyticsjs/cross-domain but it is for analytics.js tracking, should we switch to that tracking method?
From what I can see Google seem to be phasing out analytics.js now in favor of their tag manager.
How do I fire google analytics new gtag tracking code for multiple analytics accounts?
Something like this:
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-108285779-2"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-108285779-2');
gtag('config', 'ANOTHER CODE');
gtag('config', 'ANOTHER CODE');
</script>
In short:
Yes, you can add the same type of product multiple times by calling gtag('config', ...) for each respective Google account + property ID you have.
Details:
It's 2021 and I had the same question but was too paranoid to trust this thread's top voted answer because it described a different experience than my own when testing how this works. Firstly, though, in order to answer OP's question in 2021 we have to look at two entries in Google's official docs, because neither entry fully answers the question but when brought together they can give us a bit more confidence in how to solve this:
Can I add more than one type of product using gtag('config', ...)? (Answer: yes.) (Docs)
Can I add more than one of the same type of product using gtag('config', ...)? (Answer: yes.) (Docs)
Here's an example snippet of how I accomplished OP's scenario using JavaScript. If you try this in your browser's console you should see a unique script get added for each ID you set in the below snippet's googleIds array.
Notes:
Notice that the snippet's googleIds array contains five IDs.
Notice that, after running the snippet in your browser console, five <script> tags get set to the page, but that the snippet itself only explicitly built and set one of the tags to the .
The rest of the tags get added after their respective IDs are pushed into the dataLayer, and after the first script is initialized (i.e. the element is constructed + set to the ). The order of these two steps doesn't matter (i.e. You can initialize first and then push your IDs to the dataLayer, or push your IDs to the dataLayer and then initialize).
// An array of IDs I want to load on the same page(s) at the same time
var googleIds = ["AW-00000000", "AW-00000001", "AW-00000002", "DC-00000000", "UA-00000000-1"];
// Setting dataLayer & gtag to window because I'm using a custom code text field in a tag management system
window.dataLayer = window.dataLayer || [];
window.gtag =
window.gtag ||
function() {
window.dataLayer.push(arguments);
};
window.gtag("js", new Date());
// Flag used to ensure script only set with first ID, and rest of IDs are pushed to dataLayer
var gtagScriptExists = false;
// ID validation regex. Only tested with AW-*, but DC & UA are also valid prefixes
var validIdStructure = new RegExp(/(AW|DC|UA)-[0-9]{8,}(-[0-9]{1,})?/);
// Push IDs into dataLayer and set initial gtag/js?id= script to page using first ID in googleIds array
for (var i = 0; i < googleIds.length; i++) {
var gtagId = googleIds[i];
// Validate that the ID being passed isn't a big weirdo
var idIsValid =
typeof gtagId === "string" && gtagId.match(validIdStructure);
if (idIsValid) {
window.gtag("config", gtagId);
// NOTE: gtag script only needs to be set to page once, but each gtag('config', <ID>) that's pushed to the dataLayer will add subsequent gtag/js?id=<ID> scripts to the page
if (!gtagScriptExists) {
// Set initial gtag/js?id=<first ID> script to <head>
var script = document.createElement("script");
script.type = "text/javascript";
script.async = true;
script.src = "//www.googletagmanager.com/gtag/js?id=" + gtagId;
document.getElementsByTagName("head")[0].appendChild(script);
// Update gtag/js?id= script status flag so this initialization script is only set for the first ID, and not all the IDs in the array
gtagScriptExists = true;
}
}
}
Yes, that is correct according to documentation. But it generated no data for me on the subsequent codes until I added
<script async src="https://www.googletagmanager.com/gtag/js?id=ANOTHER_CODE"></script>
Immediately above the code block. Either I stumbled on a working kludge or Google needs to update their documentation.
Yes. You can add multiple accounts, and send to all of them or send individually.
Setup
<script async src="https://www.googletagmanager.com/gtag/js?id=G-XXXXXX-1">
</script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
// Global configs
gtag('config', 'G-XXXXXX-1');
gtag('config', 'AW-YYYYYY');
gtag('config', 'DC-ZZZZZZ');
</script>
And this is the place when firing events
<script>
// This is place firing event
// Send to all
// Send to one: Measure Google Ads conversions
gtag('event', 'conversion', {
'send_to': 'AW-YYYYYY/AbC-D_efG-h12_34-567',
'value': 1.0,
'currency': 'USD'
});
</script>
That seems to be the official way to do it according to the documentation.