Is it possible to update the google Analytics Tracking object? - google-analytics

I create a google analytics tracking object like this:
ga('create', 'UA-10303525-4', 'auto'); // Replace with your property ID.
ga('send', 'pageview');
Later on, without loading the page, I want to update the tracking object with a userID:
ga('create', 'UA-10303525-4', { 'userId': response.data });
This doesn't seem to work. Is it possible to update or destroy/recreate the tracking object somehow?

You might try the set method.

Related

Google analytics userId configuration

currently want to track unique users in GA (remember their session, etc). Having the problem of where to place the userId code inside the tracking code?
Is it correct to place it in between create and send? or does it matter? will it have a negative effect if I place it before create or after send?
ga('create', 'UA-XXXXX-Y', 'auto');
ga('send', 'pageview');
Beginner level with GA.
You can put it in the create command via the optional fieldsObject, or you can set it on the tracker after it's created (but before the pageview is sent).
You cannot put it before the create command, and if you put it after the pageview is sent, it'll be too late and won't apply to that hit.
In other words, either of these two variations will work:
ga('create', 'UA-XXXXX-Y', 'auto', {userId: USER_ID});
ga('send', 'pageview');
or:
ga('create', 'UA-XXXXX-Y', 'auto');
ga('set', 'userId', USER_ID);
ga('send', 'pageview');

Google Analytics beacon sent but Analytics shows nothing

I installed Google Analytics (UA) and added tracking code to a html page.
I ran html page from the browser file:///C:/test.html and debugged using google debugger, it ran successfully and shows sent beacon - but Analytics does not show the numbers. I waited for a day but still does not work.
**It works fine if I place the file in c:\Inetpub\wwwwroot and run as http://localhost/test.html
-------------------Code in test.html page-------
<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','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-******-*', 'auto',{'storage': 'none'});
ga('set', 'checkProtocolTask', null);
ga('send', 'pageview');
</script>
Can anyone tell me if google analytics works in my case.
What you want to do should definitely work, but you'll have to modify a few other things you're sending to prevent the hit from being dropped by Google Analytics.
Basically what's happening now is your hit is invalid. As you can see here using the Hit Builder tool.
I think you should be able to get around this by using the page field instead of the location field (which is auto-populated by analytics.js when you create the tracker), like so:
ga('set', 'location', null);
ga('set', 'page', '/test.html');
ga('send', 'pageview');
I've never tried this without also setting the location field, so it's possible Google Analytics will still complain that no hostname is present (Note: I tested this with Google Tag Assistant and it did complain, but it didn't say anything about the hit being invalid, so it will probably work fine.)
If using page instead of location doesn't work, you can always just modify location and give it a fake hostname, and that will definitely work.
ga(function(tracker) {
var oldLocation = tracker.get('location');
var newLocation = oldLocation.replace('file:///C:', 'http://fakehostname.com');
tracker.set('location', newLocation);
})
ga('send', 'pageview');
In either case, you won't need to unset the checkProtocolTask because you're now not using the file protocol.
Since it sounds like hostname doesn't matter in your case, giving it a fake value should have no impact on your reports.

Google Analytics UserID Tracking Code

I'm setting up UserID tracking in Google Analytics. Google's documentation displays this piece of code to push UserIDs into analytics:
ga('create', 'UA-XXXX-Y', { 'userId': 'USER_ID' });
ga('send', 'pageview');
However, when I'm in my Google Analytic's settings area, it displays the following piece of code to include UserIDs.
ga(‘set’, ‘&uid’, {{USER_ID}}); // Set the user ID using signed-in user_id.
What is the difference between set and create? Do they both achieve the same result?
Both of them should work. Including the user id in the 'create' call includes setting it as well.
ga('create', 'UA-XXXX-Y', {'userId': 'USER_ID'});
ga('send', 'pageview');
is equivalent to
ga('create', 'UA-XXXX-Y');
ga('set', '&uid', {{USER_ID}}); // Set the user ID using signed-in user_id.
ga('send', 'pageview');
If you do copy and paste provided in GA, then you will need to change the smart quotes to straight quotes, because smart quotes will break GA.

disable campaigns google analytics

I would like to disable Campaign Tracking on my GA property. I use Universal Analytics.
I've seen documentation here about method "_setCampaignTrack"
https://developers.google.com/analytics/devguides/collection/gajs/methods/gaJSApiCampaignTracking#_gat.GA_Tracker_._setCampaignTrack
But I'm wondering how I implement it in GA Universal.
I've tried this code:
ga('create', 'UA-37650441-9', 'auto');
ga('set','_setCampaignTrack', false);
ga('send', 'pageview');
So I added
ga('set','_setCampaignTrack', false);
But I'm not sure...please help me! ;)
Any suggestions would be really appreciated.
Thanks.
_setCampaigntrack was part of the older asynchronous code and I don't think that will work. AFAIIK there is no (documented) option to disable campaign tracking in Universal Analytics (looking at the field reference confirms this).
However you could override the campaign name/medium/source-fields with the respective set field calls:
ga('set', 'campaignName', '(not set)');
ga('set', 'campaignSource', '(not set)');
ga('set', 'campaignMedium', '(not set)');
or do the same thing via filters in your view settings. That should give a similar result.

Tracking Visits on a Roll Up Account with Google Analytics Universal

I am just looking for some help setting up a single view of all the sites we have. I can find lots of help for the old analytics tracking code but not for the new universal analytics.
I've got 5 sites each with their own tracking code in place but i'd like to create a roll up account that allows me to look at an overview of all the sites in a single view. Just wondering what i need to add to the code to do this?
It's currently;
<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-XXXX', 'auto');
ga('require', 'linkid', 'linkid.js');
ga('require', 'displayfeatures');
ga('send', 'pageview');
</script>
And I would just like to know what i need to add in to be able to include the numbers in a roll up account too? I think it's possible to simply edit this code with the UA of the roll up account but I'm not sure of the correct way to format this.
Any help would be appreciated,
Thanks
Roxi
It works rather like it did with asynchronous analytics, only you pass the name of the tracker in the (optional) configuration object:
ga('create', 'UA-XXXXX-X', {
'name': 'firstTracker',
});
ga('create', 'UA-XXXXX-Y', {
'name': 'secondTracker',
});
ga('firstTracker.send', 'pageview');
ga('secondTracker.send', 'pageview');
Any other options (e.g cookie domain, which is the third parameter if you don't use a configuration object) are set as key-value pairs in the configuration object. You find this in the documentation under Advanced Configuration.

Resources