I don't seem to be able to get Google analytics event tracking to work with the "new" style tracking code.
I cobbled together this code, but it might be wrong:
<script>
(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-42805759-5', 'my-domain.com');
ga('send', 'pageview');
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-42805759-5']);
_gaq.push(['_trackPageview']);
</script>
And an example link is:
View this page
Should this work?
And also, should tracked events be visible in Real-time > Events area of the dashboard?
Thanks.
You have two problems:
The classic style with _gaq is not interchangeable with the new universal analytics, you will be using ga('send','event', param1, param2...).
If you track an event on click with the classic style code, its unlikely the event will be reported before the page is unloaded. If you set this up correctly with the universal code, it will work fine.
Related
According to this on Single Page Applications you load the google tag script when the user first loads the app, then on subsequent page views you programmatically update the page path like so:
gtag('config', 'UA-1234567-89', {'page_path': '/about-us'});
My question is, when you call this code does it automatically send a new page view too?
So for example let's say my SPA has a root URL of:
https://helloworld.com
And the user clicks on the /about-us link from the homepage. Does this mean there will be 2 page view events?
First https://helloworld.com/
Then https://helloworld.com/about-us
Or do I need to do something else after updating the page path to send the page view event to GA for the about us page?
gtag('config', 'UA-1234567-89', {'page_path': '/about-us'});
// Call something else here to trigger new page view using updated path here?
If you're using gtag.js then your code snippet for implementation will look something like this:
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-00000000-1"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-00000000-1'); //modify this line
</script>
To prevent it from sending a pageview on load you just make the adjustment like so:
gtag('config', 'GA_MEASUREMENT_ID', {'send_page_view': false}); //replace the id with your own id
For SPAs, I'd recommend Google Tag Manager instead and use datalayer events or the included history change trigger for tracking, there might be a slight learning curve to start, but I think it'll simplify your code and make it more "portable" should you switch analytics platforms later on.
More info on GTM: https://developers.google.com/tag-manager/devguide
I am not sure about Google Analytics behaviour in sending allowAdFeatures.
I added analytics.js the default way, setting only the tracker:
<script>
(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-XXXXXX-XX', 'example.com');
ga('send', 'pageview');
</script>
When the user lands on my site, I show him a popup asking him to approve saving cookies.
Upon his approval I send:
ga('set', 'anonymizeIp', false);
ga('set', 'allowAdFeatures', true);
My question is about the current page that he is viewing (before approving the consent) - will the action of page view be able to be re-targeted for that current user or I need to send another event of page view?
In your case you need to send another page or an event with the new settings in order to send the updated data to Analytics.
However, you don't need to send this with your second hit:
ga('set', 'anonymizeIp', false);
Rather this with the previous pageview (and nothing with the second):
ga('set', 'anonymizeIp', true);
anonymizeIp is not active by deafult, so you must declare it in the initial code.
The following datalayer push appears to be deployed correctly after an action is clicked on a website.
I can see that this code is loading onto the page after the click fires, this full datalayer push script is then visible within the source code.
But when using the GTM debugger, there is no sign of the event, or the datalyer information.
Is this an issue with the data layer code, or perhaps how or when it is fired?
<script>
window.dataLayer = window.dataLayer || [];
dataLayer.push({
'event': 'addressResult',
'postcode': '3415',
'availability': 'limited'
});
</script>
I am using the new gtag.js on a website with existing Google Tag Manager dataLayer. I am trying to send events via gtag but because GTM also uses the same window.dataLayer for its operation (and we have some custom operation going on the dataLayer, so I cannot change over there), events are not being sent.
Is there a way to rename window.dataLayer for gtag.js?
It would seem so. Since gtag uses infrastructure from GTM I look how GTM goes about renaming the dataLayer and tried the same with gtag:
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-123456789-1&l=foo"></script>
<script>
window.foo = window.foo || [];
function gtag(){foo.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-123456789-1');
</script>
Watching the console in a jsfiddle I could see that the pageview was sent. adding the "l" query parameter with your desired datalayer name to the script tag seems to do the trick.
Is it possible to set a global hitCallback in Google Universal Analytics:
So any hit, calls the same callback?
Without modifying each ga('send'..
You can't set the hit callback globally, but you can set it on the tracker object itself, which means it'll get called for every hit that tracker sends (which is probably just as good as globally, for your purposes).
Here's an example:
ga('create', 'UA-12345-1', 'auto');
ga('set', 'hitCallback', function() {
console.log('hit is done');
});
ga('send', 'pageview');
ga('send', 'event', 'Button', 'click');
If you run this code, you'll see "hit is done" logged to the console twice.