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.
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 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
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.
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.