Multiple GA trackers gets each other variables - google-analytics

I need to split collecting data for two GA accounts, let`s name them UA-XXXXXXX-1 and UA-XXXXXXX-2. To implement this, I used example code from https://developers.google.com/analytics/devguides/collection/gajs/ (under "Pushing commands to multiple trackers also works" text) and here is my code:
_gaq.push(['_setAccount', 'UA-XXXXXXX-1']);
_gaq.push(['_trackPageview']);
_gaq.push(['_setCustomVar', 1, 'customVar1', 'cv1', 1]);
_gaq.push(['second._setAccount', 'UA-XXXXXXX-2']);
_gaq.push(['second._trackPageview']);
_gaq.push(['second._setCustomVar', 2, 'customVar2', 'cv2', 1]);
It is working, but I have both custom vars in both accounts. What I really need, is to track customVar1 only for UA-XXXXXXX-1 account, and customVar2 only for UA-XXXXXXX-2 account. Any ideas how to implement this?

First of all, _setCustomVar must come before _trackPageview.
Now to your problem:
This happens because User level custom vars are stored in the cookie. Since both your trackers share the same cookie the second tracker will be sent with the vars set on the first tracker.
You have 3 options.
1) Go With Universal Analytics
The right path here is to use Universal Analytics. Multi-tracking is not officially supported in Classic because it's buggy, as you probably noticed. And things are easy to break.
On Universal all custom dimensions are evaluated server side so this setup is supported. No data is stored on cookies for Custom Dimensions.
eg:
Provided you configured dimension1 on UA-XXXXXXX-1 and dimension2 on UA-XXXXXXX-2 through the Admin interface.
ga('create', 'UA-XXXXXXX-1', 'auto');
ga('send', 'pageview', {
'dimension1': 'cv1'
});
ga('create', 'UA-XXXXXXX-2', 'auto', {'name': 'newTracker'});
ga('newTracker.send', 'pageview', {
'dimension2': 'cv2'
});
More info:
Universal Analytics Advanced Configuration / Multiple Tracking Objects
Universal Analytics Custom Dimensions and Metrics
2) Keep Classic Analytics but, use session level customVars
If you definitively can't move to Universal Analytics and want to keep using Classic you can get around this issue by just using Session Level Custom Vars. To make it work you would only need to change the scope of the custom Var as seen below (from 1 to 2).
Unlike User scoped Custom Vars, Session Scoped CVs don't get stored on the cookie. So you will get around this issue. The downside is that the value will only be valid for that session, not future sessions from the same user.
_gaq.push(['_setAccount', 'UA-XXXXXXX-1']);
_gaq.push(['_setCustomVar', 1, 'customVar1', 'cv1', 2]);
_gaq.push(['_trackPageview']);
_gaq.push(['second._setAccount', 'UA-XXXXXXX-2']);
_gaq.push(['second._setCustomVar', 2, 'customVar2', 'cv2', 2]);
_gaq.push(['second._trackPageview']);
3) Keep Classic and User scoped CVs but use different cookies per tracker
You can configure GA to create 2 sets of cookies, one for each tracker one at the root domain and one at the subdomain.
If your site is: http://www.example.net setup your trackers like this:
_gaq.push(['_setAccount', 'UA-XXXXXXX-1']);
_gaq.push(['_setDomainName', 'example.net']);
_gaq.push(['_setCustomVar', 1, 'customVar1', 'cv1', 1]);
_gaq.push(['_trackPageview']);
_gaq.push(['second._setAccount', 'UA-XXXXXXX-2']);
_gaq.push(['second._setDomainName', 'www.example.net']);
_gaq.push(['second._setCustomVar', 2, 'customVar2', 'cv2', 1]);
_gaq.push(['second._trackPageview']);
This MUST to be done in all pages of your site. Not only this one. This will make sure that each tracker uses it's isolated cookieset and customVars won't leak from one to another.
Notice that if your site can be accessed without www.. eg: http://example.net/ this will fail and there is no workaround. You can't create 2 sets of cookies with the same name in the same domain and path. You just can't.
Also if you use _gaq.push(['_setDomainName', 'none']); or _gaq.push(['_setAllowHash', false]);, the above trick won't work and cookies will conflict. Your data will be weird. Just don't do it. You've been warned.
I can't stress enough that this is provided without guarantees and if your data breaks it's on you. Multiple trackers are tricky and that's why it was never officially supported.
More info:
ga.js API Reference Domains and Directories

Related

What is the correct syntax to set a custom dimension in Google Analytics?

I'd like to send some custom dimensions back to GA with every blog post that is read on my website - author and category.
When I set up the custom dimensions in GA I got this code to use:
var dimensionValue = 'SOME_DIMENSION_VALUE';
ga('set', 'dimension2', dimensionValue);
However, in the GA docs it specifies a different syntax using "send" rather than "set".
https://developers.google.com/analytics/devguides/collection/analyticsjs/custom-dims-mets
ga('send', 'pageview', { 'dimension4': '<?=$categories?>'});
We are using universal google analytics, but this conflicting information means I'm not sure which syntax to use.
Thanks in advance
The first code block is how you set the custom dimension (CD) that can be sent with any hit: event, pageview, transaction, etc.
The second code block is how you set the CD and send it with a pageview. So it's a specific example of the first method.
Both are valid, it's just that the second example is more complete.
When sending data to CDs, don't forget to create and define them in the GA configuration as well.

Cross Domain Conversion Tracking Google Analytics

I am trying to set up my analytic account and wondering if anyone can help shed some light on what I may be doing wrong. I have 6 e-com sites with products and the shopping cart is on a 7th separate site. Each site has its own unique domain.
I have a property set for each domain and then one additional property that is set as a global account. The theory was to track for each separate property and then additionally in the global account. This might be overkill since I am not overly concerned with traffic on the individual sites. But what I am having trouble with is I am paying for adwords so the ad lands on a shopping1.com and checks out on shopping7.com its not registering as a conersion since there is a switch in the domain name.
Here is the code that I, any input on how to make all 7 sites work as one and adwords realizing there was a conversion would be greatly appreciated. Do I need to set the domanname element? Also, do I need to add code to everything that links back and forth to each of the separate sites?
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-site1']);
_gaq.push(['_setAllowLinker', true]);
_gaq.push(['_trackPageview']);
_gaq.push(['global._setAccount', 'UA-global']);
_gaq.push(['global._setAllowLinker', true]);
_gaq.push(['global._trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://' : 'http://') + 'stats.g.doubleclick.net/dc.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
The code is okay, but not sufficient. First yes, you should use _setDomainName (with the value set to the respective domain names shopping1.com, shopping2.com etc).
More importantly you need to attach the linker functions to links, forms and iframes (and redirects, although with redirects you need to do the linking yourself) - basically every place where people change from one of your domains to another (it's not necessary to attach linker function to links that do not go to your domains).
Google Analytics uses cookies to store data, which cannot be shared between domains. The purpose of the linker function is to attach session information to the url so it is transmitted to the other domain, where it is read by the Google script and stored into a new cookie. Take a look at the documentation to see how the _link and linkByPost-functions are used.
If you are doing redirects you need to be careful that the redirect keeps the Google parameters.
Linker functions result in very long and ugly urls. Cross domain tracking works much better with Universal Analytics where you only have to transmit a single parameter (the client id) between domains.

Detect if user is already logged into LinkedIn and log the results

I'd like to track how many people hit my website that might be logged into LinkedIn. Depending on the results, I might add an interactive LinkedIn section using their API.
I already have the Javascript logic to detect if they're logged in. My question is what is the best approach to log this result? Can Google Analytics do it with a custom variable or event detection?
I'd love to report on something like, "10,000 visits to my website, of which X number of visitors were logged into LinkedIn".
Any help would be appreciated!
I believe Google Analytics Custom Tracking Variables would do the trick. Something along the lines of:
_gaq.push(['_setCustomVar',
1, // variable slot
'LinkedInUser', // variable
'true', // value
1 // variable scope, 1=visitor
]);
_gaq.push(['_trackPageview']);
For custom variables (not that is this is a user-triggered event after the initial GA page load logic, a call to _trackPageview is needed to push the data back to GA), or:
_gaq.push(['_trackEvent',
'PageView',
'LinkedIn'
]);
For custom events. Just pop something similar to that into your JS routine that checks for authed LinkedIn users.

Changing the delimiter from ? to # for Cross Domain Tracking

Trying to do cross domain tracking of links to an commerce site (Volusion); currently produces an error because of the ? delimiter. I see I can
_gaq.push(['_setAllowAnchor', true]);
on the volusion cart site in order to enable seeing # as the delimiter instead.
I'm using the script provided by Luna Metrics xdomain.js which auto tags outgoing links for cross domain tracking. It’s not clear to me what to change in the xdomain.js code to use # instead of ? as the delimiter, or can I make this default change with also using
_gaq.push(['_setAllowAnchor', true]);
on the referring domain where the outbound link is coming from?
You need to use _gaq.push(['_setAllowAnchor', true]) and _gaq.push(['_setAllowLinker', true]) on both ends. In theory you just need them in the receiving end. But usually the receiving end can be both. User can be going from domain A to domain B or the other way around.
After that you'll need to modify the lunametrics script because it won't allow you to send cookies in the anchor as it is today. It has this feature hardcoded. These are the lines that you need to modify in the lunametrics script.
Find this line:
var fullUrl = tracker._getLinkerUrl(jQuery(link).attr('href'));
And modify to this:
var fullUrl = tracker._getLinkerUrl(jQuery(link).attr('href'), true);
Then find this line:
_gaq.push(['_link',jQuery(link).attr('href')]);
And modify to this:
_gaq.push(['_link',jQuery(link).attr('href'), true]);
Here are the reference on these functions:
http://code.google.com/apis/analytics/docs/gaJS/gaJSApiDomainDirectory.html

Google Analytics multi-site tracker stopped collecting stats Nov 1, individual single-site trackers still working

I am using Google Analytics for a number of websites. I have one dedicated tracker for each site, as well as one global tracker (for network-wide stats). So each site is set up to use multiple trackers. I am loading Google Analytics asynchronously, and my tracker initialization code on each site looks like this:
var _gaq = _gaq || [];
_gaq.push(
/* global tracker */
['_setAccount', 'UA-XXXXX-XX'],
['_setDomainName', 'none'],
['_setAllowLinker', true],
['_trackPageview'],
['_trackPageLoadTime'],
/* dedicated site tracker */
['t1._setAccount', 'UA-YYYYY-YY'],
['t1._setDomainName', 'none'],
['t1._setAllowLinker', true],
['t1._trackPageview'],
['t1._trackPageLoadTime']
);
This was working great through Nov 1.
But starting Nov 2nd, the site trackers are all still working fine, but the global tracker just stopped collecting stats. It is reporting "0" visitors for almost every day since then (with the exception of Nov 2 itself and, oddly, Dec 9, each of which reported "1" visitor), down from hundreds of thousands of visitors each day through Nov 1.
I tried adding a prefix t0 to the global tracker like this:
var _gaq = _gaq || [];
_gaq.push(
/* global tracker */
['t0._setAccount', 'UA-XXXXX-XX'],
['t0._setDomainName', 'none'],
['t0._setAllowLinker', true],
['t0._trackPageview'],
['t0._trackPageLoadTime'],
/* dedicated site tracker */
['t1._setAccount', 'UA-YYYYY-YY'],
['t1._setDomainName', 'none'],
['t1._setAllowLinker', true],
['t1._trackPageview'],
['t1._trackPageLoadTime']
);
And gave it 48 hours, and still no help. Is there some new formatting requirement that I missed that this initialization code isn't following? Anything else that might explain this behavior?
Filters. Usually the culprit when data suddenly disappears.
Also, when you set up analytics is it always a good idea to create a raw profile that contains no filters. Use a second profile to add filters to. You can create multiple profile under the same account that uses the same tracking code so you do not have to add an additional ['t0._setAccount', 'UA-XXXXX-XX'] to your tracking code.
From How do I create a filter:
If you'd like to apply filters to your data while keeping your "raw" data intact, you can create a duplicate profile in your account. To do so, add a new profile using the Add profile for an existing domain option. When this option is selected, the tracking code generated for the new profile will be identical to the tracking code for the original profile, and data will be imported simultaneously into both. You won't need to change the tracking code on your site, and any filters applied to the first profile will not affect data in the second.

Resources