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.
Related
I want to track outbound link clicks on my site. I have Google Analytics on my site in terms of having the global site tag (gtag) in the head.
I'm happy to add the onClick event to each link as there's only a few. Can I just add onclick="ga('send', 'event', 'Category', 'Action', 'Label', 'Value');" to each link?
Basically I just tried this and the events are not showing in the Realtime stats so I'm not sure if I'm missing something.
You can't mix Universal Analytics code and gtag code. If you have in head gtag snippet, you have to use its syntax for send an event:
gtag('event', <action>, {
'event_category': <category>,
'event_label': <label>,
'value': <value>
});
https://developers.google.com/analytics/devguides/collection/gtagjs/events
Measure outbound links with gtag.js: https://support.google.com/analytics/answer/7478520?hl=en
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.
This is the code I have
jQuery('iframe.language-menu-frame').contents().find(".text").click(function(){
if (ga) {
ga('send', 'event', 'Language Tool', 'click');
}
});
I placed this code inside my js file along with other event tracking code. All the other event tracking code works just fine, but not this one. Any idea why it is working in the console but not when installed into the js file?
In Universal Analytics
In case I need to set my custom dimension on page load with a single pageview, should I need to set it prior to send.
ga('set', 'dimension1', dimensionValue); ga('send', 'pageview');
Let me know if this works.
Thanks
Yes, that would work.
There is also an alternative syntax where you can set the dimension with the page view call:
ga('send', {
'hitType': 'pageview',
'dimension1' : 'dimensionValue'
});
The important thing is that there must be some kind of interaction event (pageview or event) to transmit the transaction value.
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.