I'm using the following code in my page, in order to have Google Analytics and Tag Manager.
<script type="text/javascript">
(function(i, s, o, g, r, a, m) {i["GoogleAnalyticsObject"] = r;i[r] = i[r] || function() {(i[r].q = i[r].q || []).push(arguments)}, i[r].l = 1 * new Date();a = s.createElement(o),m = s.getElementsByTagName(o)[0];a.async = 1;
a.src = g;m.parentNode.insertBefore(a, m)})(window, document, "script", "//www.google-analytics.com/analytics.js", "ga");
ga('create', 'UA-xxxxx', 'auto');
ga("require", "displayfeatures");
ga('send', 'pageview');
</script>
and
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-xxxxxx"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-xxxxxx');
</script>
So I'm getting warning "Same web property ID is tracked twice" in Google Tag Assistant.
Is there anyway I can fix this warning?
Actually you have two Google Analytics codes on your page and both are sending 'pageview' hits to Google Analytics. The first one is somewhat older analytics.js snippet and the second is a little bit newer gtag.js snippet. None of them has anything to do with Google Tag Manager.
Consider using just one, either analytics.js or gtag.js. Note that if you'll also install GTM and set up pageview tracking in GTM you'll get the same issue again because GTM will duplicate the existing analytics snippet functionality.
Related
I am using gtag and event tracking. However, it doesn't count at all... I have no idea why it doesn't work even though I clicked the link. Always it shows 0. Please give me any advice. Thanks.
<script async src="https://www.googletagmanager.com/gtag/js?id=my id"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'my id');
</script>
test link
I am using the following construction:
Link
it works for me:
P.s. I didn't configure anything on GTM and GA.
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
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'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.
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