Upcoming LunaMetrics Events
San Francisco, Apr 28-30 Los Angeles, May 12-16 New York City, May 19-23 Chicago, Jun 16-18

Using Different Cookies When Tracking to Multiple Accounts in Google Analytics





The problems discussed in the previous post have been due to both sets of GA using the same cookies for holding information. Another option is to intentionally tell GA to use different cookies.  We can’t change the name of the cookies that GA uses, but we can change the domain and the path of the cookies to make them distinct.

cookies

If you have a simple site such as www.domain.com and domain.com redirects to www.domain.com this is no problem. You can use:

<script type=”text/javascript”>
var gaJsHost = ((“https:” == document.location.protocol) ? “https://ssl.” : “http://www.”);
document.write(unescape(“%3Cscript src=’” + gaJsHost + “google-analytics.com/ga.js’ type=’text/javascript’%3E%3C/script%3E”));
</script>

<script type=”text/javascript”>
var pageTracker = _gat._getTracker(“UA-11111-1″);
pageTracker._setDomainName(‘www.domain.com’);
pageTracker._trackPageview();

var otherTracker = _gat._getTracker(“UA-22222-1″);
otherTracker._setDomainName(‘domain.com’);
otherTracker._trackPageview();
</script>

This causes each tracker to use completely different cookies.  At this point you no longer have the problems listed above.  You can track some pages to both accounts, and other pages to just one account without any conflict. You can use different User Defined Segments on each. And you can use cross domain tracking for one set of Tracking Code but not the other.

In addition to every cookie having a Domain that you can set, every cookie has a Path that you can set as well.  By default the Path would just be “/”.  But with GA we can use the _setCookiePath(…); method to set it to the subdirectory that we are operating in.

Just like _setDomainName can’t set the domain of the cookie to something other than the domain you’re actually on, _setCookiePath must be used to set the Path to a subdirectory that the page you are tracking is actually in.

For example, if you wanted to additionally track just one subdirectory to its own account you could use the following change on the tracking code for the pages that appear inside that subdirectory on your website. (Where “subdirectory” below is replaced with the actual subdirectory you are tracking.)

<script type=”text/javascript”>
var gaJsHost = ((“https:” == document.location.protocol) ? “https://ssl.” : “http://www.”);
document.write(unescape(“%3Cscript src=’” + gaJsHost + “google-analytics.com/ga.js’ type=’text/javascript’%3E%3C/script%3E”));
</script>

<script type=”text/javascript”>
var pageTracker = _gat._getTracker(“UA-11111-1″);

pageTracker._setDomainName(‘domain.com’);

pageTracker._setCookiePath(‘/subdirectory/’);
pageTracker._trackPageview();

var otherTracker = _gat._getTracker(“UA-22222-1″);
otherTracker._setDomainName(‘domain.com’);
otherTracker._trackPageview();
</script>

Again, these two trackers will use different cookies and will not interfere with each other.

Finally, I’d like to point out a comment from my previous post made by André Scholten:

Also don’t forget to add an extra trackPageview call on the links you tagged manually. For example: if you added some tracking code to a print button it wil look like this: onclick=”pageTracker._trackPageview(’print’); otherTracker._trackPageview(’print’);”

If you want the data to appear in all accounts, then anytime you call _trackPageview for one tracker, you have to call it again for every other tracker you created (In this case, just 2).

http://www.lunametrics.com/blog/2009/03/06/cookies-tracking-multiple-accounts-ga/

47 Responses to “Using Different Cookies When Tracking to Multiple Accounts in Google Analytics”

Hi friends!

Complementary to André Scholten:
You can also define what we call “a proxy function”, which is a javascript method that can trigger the two Tracker accounts.
In example:
First the javascript:

function myTracker(uri){
pageTracker._trackPageview(uri);
otherTracker._trackPageview(uri);
}

So, you can add a single call in every manual links:
onclick=”myTracker(’print’);”

It makes sense?
:D

DangerMouse says:

Great post – would you suggest that there are any downsides to this method?

If you had multiple websites, on multiple distinct domains, would this method be appropriate for combining them in to one account, for extremely basic headline comparative reporting?

Cheers,

DM

John Henson says:

One of the downside is the extra work that it could create in certain situations. I have had one situation for a university where different departments had control of their own pre-existing GA, and we wanted to track an area of the overall site that crossed department (and GA) boundaries but did not encompass the entirety of one of the departments. I was able to define the GA cookies in such a way that our implementation was distinct from the other 2, but it meant that I had to implement cross-domain tracking in a sub-domain situation adding to the complexity somewhat.

For multiple distinct websites for which visitors would not normally traverse from one site to another during the course of a standard visit, I would use use 2 sets of code and the same cookies. So I would not worry about trying to define 2 sets of cookies for GA.

One set of code would send both sites to the same account, the second set of code would send each site to its own account. Because they are distinct sites, they will each track separately and independently — that is that each site will naturally have its own cookies.

And both sets of code on a site will use that sites single set of cookies.

What you end up with in the roll-up account is just the simple Sum of the 2 sites.

Of course the real answer depends on the situation and what specifically you need out of the data and how much work you want to put into the GA implementation.

DangerMouse asks questions like a teacher that already knows the answer and is just trying to get the students to continue further with their thinking and see how they answer.

John Henson says:

Yes, Leonardo. Although I haven’t used that method, it looks like an excellent way to handle it.

Luke Byrne says:

Hi John,

I came across this post and thought you may be able to pass your eye over the code I am trying to implement for our University site in order to segment Staff, Students and Visitors.

We use a central login system that Staff, Students and Visitors come to. We have over 400 subdomain sites and hence due to the limitations of 50 accounts in each profile I am future proofing and have set up 4 accounts. I am now trying to pass to values of who has logged in to each of these accounts using a method similar to above.

can you have a look at the below and tell me whether you think this will be successful. I am seeing data flow through into the various accounts, but it is not 100% clean as I would expect.

document.write(unescape(“%3Cscript src=’https://static.weboffice.uwa.edu.au/stats/uwa_ga.js’ type=’text/javascript’%3E%3C/script%3E”));

try {

var pageTracker = _gat._getTracker(“UA-1933569-1″);
pageTracker._setDomainName(“.uwa.edu.au”);
pageTracker._trackPageview();

var pageTracker2 = _gat._getTracker(“UA-7622267-1″);
pageTracker2._setDomainName(“.uwa.edu.au”);
pageTracker2._trackPageview();

var pageTracker3 = _gat._getTracker(“UA-7892406-1″);
pageTracker3._setDomainName(“.uwa.edu.au”);
pageTracker3._trackPageview();

var pageTracker4 = _gat._getTracker(“UA-7892507-1″);
pageTracker4._setDomainName(“.uwa.edu.au”);
pageTracker4._trackPageview();

} catch (err) { }

window.onload = function() {
var user_type = ‘visitor’;
if (user_type == ‘staff’) {
pageTracker._setVar(‘Staff’);
pageTracker2._setVar(‘Staff’);
pageTracker3._setVar(‘Staff’);
pageTracker4._setVar(‘Staff’);
} else if (user_type == ‘student’) {
pageTracker._setVar(‘Student’);
pageTracker2._setVar(‘Student’);
pageTracker3._setVar(‘Student’);
pageTracker4._setVar(‘Student’);
} else {
pageTracker._setVar(‘Visitor’);
pageTracker2._setVar(‘Visitor’);
pageTracker3._setVar(‘Visitor’);
pageTracker4._setVar(‘Visitor’);
}}

I look forward to your response.

Kind regards,

Luke Byrne

John Henson says:

Luke,

I don’t see anything technically wrong with the code you have here.

How is the data not ‘clean’?

Is there a reason you decided to host the ga.js file locally?

Since the _setVar is persistent, you only need to set it one time when they log in, and that value will stay with them even in future visits in which they are not logged in. Are you calling the setVar section of code every page? With user_type being their current login status?

Also it is important to understand how GA associates setVar with a visit. If the visitor doesn’t have a setVar, then the first setVar they are given is attached to that visit. If they already have a value for the setVar when they arrive at the website, then the existing value is used for that visit, even if you change it during the course of the visit. The reason I point this out is because it looks like you are resetting the setVar to Visitor if they are not logged in.

Here is an example.

Someone comes to your site for the first time. They get assigned as “Visitor” since they are not logged in. This visit now is a “Visitor” visit. They log in as a student. The value of the setVar changes in the cookie, but GA still considers them a “Visitor” for this visit. They leave. The come back tomorrow. The value of their cookie is “Student”. GA sees this, now this visit is a “Student” visit. They log out. Your code set the setVar to “Visitor”. When they come back the next time they will be “Visitor”.

I’m making some assumptions about the way user_type variable is used, etc, and I may be incorrect in my assumptions.

If you could give me an example of how the data is not clean, it might help me understand what the problem is.

John

Richard says:

Hi,

I posted in the previous thread to this, but as the discussion has moved on somewhat since then, I think my issue is probably more relevant here:

Hi,

I am suffering a similar issue which I would appreciate some help with if possible! I have 100 or so sites (location based) and up until now all of them have used seperate GA tracking. I would like to keep this but also add an extra page tracker that could be used for all pages on all sites. The sites heavily interlink, so conflicts in code are a big issue with implementation. The code I have thought of using is along the lines of:

try {
var pageTracker = _gat._getTracker(”UA-####-1?);
pageTracker._trackPageview();
var otherTracker = _gat._getTracker(”UA-####-2?);
otherTracker._setDomainName(”none”);
otherTracker._setAllowLinker(true);
otherTracker._trackPageview();
} catch(err) {}

where UA-####-2 is the cross domain tracker.

Is this going to work? Will I have the issue of the cookies? Is there anything else I need to do? I read somewhere that I may need to tag each and every cross domain link with a similar onclick event, but I can’t believe that is necessary? :O

Richard says:

Sorry to double post, but after reading your article again this is the code I think may work:

var portalTracker = _gat._getTracker(“UA-####-1″);
portalTracker._setCookiePath(“/portal/”);
portalTracker._trackPageview();
var otherTracker = _gat._getTracker(“UA-####-2″);
groupTracker._setDomainName(“none”);
groupTracker._setAllowLinker(true);
groupTracker._trackPageview();

Any comments greatly appreciated!!

Thanks in advance.

John Henson says:

Something like that may work.

It seems that each of your individual sites are based in their own sub-directory then? and portalTracker._setCookiePath(‘/portal/’); would change the directory path based on the site?

If so, then I think you’re probably on a right track.

But if you’re individual sites are all Only sub-directories you will probably be in a situation to not need cross domain tracking if you use _setDomainName(“basedomain.com”);

Could you provide an example of your individual sites URLs? Maybe with stand-ins for the actual domain name if you want?

For example are they like this:

http://www.domain.com/site1
http://www.domain.com/site2

Or

site1.domain.com
site2.domain.com

Or

http://www.domain-1.com/portal
http://www.domain-2.com/portal

or. . . . .

Richard says:

Hi John,

Pardon my ignorance, but I’m not sure I’ve understood it all correctly.

The setup is thus:

http://www.domain-1.com
http://www.domain-2.com
etc.

Visitors are able to move between sites (depending on what they search for) and therefore we would like to have cross domain tracking on one GA account and then several other GA to track each domain separately.

Is this possible?

Thanks

Richard

John Henson says:

Something like the following might work in your situation. In this scenario you are tracking each domain to its own UA-xxxxx-y number as well as tracking them together to a ‘roll-up’ UA number.

For this to work as-is, it assumes that in all cases the non-www version of your domains redirect to www versions. For example, if someone types in domain-1.com, the URL they would see in their address bar would be http://www.domain-1.com instead.

Also, the UA numbers would have to be changed.

The siteTracker would be to track just that domain to its own account, the overallTracker would be tracking both accounts together.

Whenever there was a link from one site to another you would have to call overallTracker._link or overallTracker._linkByPost

Here are examples for 2 different fake domains:

For domain-1.com:

var siteTracker = _gat._getTracker(”UA-####-1?);
siteTracker._setDomainName(‘domain-1.com’)
siteTracker._trackPageview();

var overallTracker = _gat._getTracker(”UA-####-2?);
overallTracker._setDomainName(‘www.domain-1.com’);
overallTracker._setAllowHash(false);
overallTracker._setAllowLinker(true);
overallTracker._trackPageview();

For domain-2.com:

var siteTracker = _gat._getTracker(”UA-####-3?);
siteTracker._setDomainName(‘domain-2.com’)
siteTracker._trackPageview();

var overallTracker = _gat._getTracker(”UA-####-2?);
overallTracker._setDomainName(‘www.domain-2.com’);
overallTracker._setAllowHash(false);
overallTracker._setAllowLinker(true);
overallTracker._trackPageview();

Misty says:

Going back to “the overallTracker would be tracking both accounts together,” how is the UA profile set up? Is overallTracker using the same UA number?

I’m also looking for a way to track conversions from one domain to another domain. Will the above script work for this?

Thanks,
Misty

John Henson says:

Misty,

If you want to track 2 sites together, then you have an ‘overallTracker’ on both sites, using the same UA-xxxx-y number.

To also track them separately, you’d have another tracker on each site, lets call it siteTracker that would use a different UA-xxxx-y number for each site.

Tracking conversions from one site to another is a different issue.

For that you just need to make sure that you’re using the cross domain tracking lines and calling either _link or _linkByPost as appropriate when you move from one site to the other.

suhel says:

Like I have 2 site abc.com and xyz.com if i put 2 code on site xyz.com which contain the Google analytics code of both site, will i am able to get data in both account

John Flynn says:

We host traffic from several other sites that refer to us. We want to:

Track using our account number.
Track another account by “dynamically loading” it from a DB (according to which site was the referrer.) This account is that company’s actual GA account.

For example, at the top of each page, we would see who linked to us, hit the DB, grab the account, and call a JS function that initializes both accounts.

Can this work? It seemed to work in small tests involving three separate domains, but a live test is showing far fewer hits on GA than we’re recording in our access logs.

Could you shed some light on this issue? Thank you.

John says:

Yes, I have seen that work with multiple accounts and hundreds of sites. It was checking the Request URI and not the Referrer though, but same concept.

It could be a problem with getting the referrer right, maybe a redirect issue where you are losing the referrer information?

John

Nicola says:

Hello John

I wondered if you could help me? I have two seperate Adwords accounts, one pointing to the UK and one to the USA. I have inserted the two trackers codes on the site http://www.opulentafrica.com but both accounts are showing the USA data (which is the first account listed on the webpage) is there a way to tell Google to track both?

Thanks for any help!

Nicola

Marcus Herou says:

Hi.

We are going to use GA to track the stats of all sites connected to us and using our stats due to that we would like to use GA as cross-reference to our statistics system.

Just wondering if the following code would work if the site connected to us already have another GA script on their site. We have got indications that this is the case from both big toplists in Sweden (using GA only) and Google themselves.

I suspect that it was the _setDomainName(“none”) which stirred things up when having multiple scripts.

try{
var pageTracker = _gat._getTracker(“UA-xxxxxxxxx-5″);
pageTracker._setDomainName(“none”);
pageTracker._setAllowLinker(true);
pageTracker._setAllowHash(false);
pageTracker._trackPageview();
} catch(err) {}

Perhaps better code ?

try{
var pageTracker = _gat._getTracker(“UA-xxxxxxxxx-5″);
pageTracker._setAllowLinker(true);
pageTracker._setAllowHash(false);
pageTracker._trackPageview();
} catch(err) {}

Any thoughts on how you would implement GA on 1000+ sites which may or may not have GA already on them ?

John says:

It seems like both GA accounts have cost data applied from the same Adwords account, and what you probably want is to apply the UK Adwords data to one account and the USA Adwords data to the other account.

Danny Johan says:

Hi Jon,

If a visitor moves from http://www.site.com has (with a single “master” code) to http://www.site.com/directory/ (using otherTracker to independently track the subdirectory) does this mean that the visit is picked up as a referral when viewing the /directory/ GA account? (Traffic Sources > Referring Sites)

Cheers Danny

Danny Johan says:

Sorry, to clarify, “picked up as a referral” was supposed to read: “picked up as a referral from http://www.site.com“.

John says:

If you’re using the same cookies for both trackers, then the visitor will not be shown as a referral from http://www.site.com, in this case, I think GA uses whatever Source/Medium information is already in those cookies. But I’m not 100% certain how GA behaves in this situation.

If you are using a different set of cookies for the /directory/ tracker, such as with _setCookiePath, then the visitor should be seen as being referred from http://www.site.com on the “otherTracker”.

John

John says:

Marcus,

The biggest issue with tracking across many sites which may have their own GA implementation, is doing things that unintentionally re-write cookies.

So let’s say you use either of the pieces of script you posted. And one of the sites already has GA on their site, but doesn’t use the cross domain tracking lines.

The cross domain tracking CHANGES the way GA writes cookies. So one version of GA can’t recognize the cookies written by the other version, and cookies get overwritten every pageview. Very Bad.

I don’t think there is any good way to do this right now, other than looking at each site’s existing code individually, and making a decision as to what to do.

I would also not use ‘pageTracker’, instead use something that would not be used in any of the potentially already existing GA code on the sites, such as, ‘_rollupTracker’ or something else that should be unique to your implementation.

Hope that helps a bit,

John

Danny Johan says:

Hi,

I’ve implemented your main + subdirectory tracking like so:

var pageTracker = _gat._getTracker(”UA-11111-1″);
pageTracker._setDomainName(’domain.com’);
pageTracker._setCookiePath(’/subdirectory/’);
pageTracker._trackPageview();
var otherTracker = _gat._getTracker(”UA-22222-1″);
otherTracker._setDomainName(’domain.com’);
otherTracker._trackPageview();

Whilst I am getting independent utm_a, utm_b and utm_c cookies for both trackers I am only getting one set of utm_z cookies. This means that a “referal” from the parent website to the /subdirectory/ is being tracked as “direct”. Do you have any idea on how to create a seperate set of utm_z cookies?

Best regards,

Danny

Danny Johan says:

Hi, changing the first ._setDomainName to ‘www.domain.com’ resolved the problem and I now see 2x sets of utm_z cookies.

BillT says:

Will this solution cause problems tracking bounces?

John says:

Bill

I believe there would be no impact on bounce rate.

Is there a specific situation that you’re dealing with that I might have overlooked?

John

BillT says:

Hi John,

I had implemented an alternate tracker solution, I think from this blog, that got around the second call to Google that was causing Google to think it was a second action on a page and therefore they could not track bounces accurately. Then the need came up to be able to track the site from two different GA accounts so I implemented this solution. The problem is that now the bounces no longer seem to be tracked properly once again. I was in the process of commenting out the call to _setDomainName to see if that call was somehow causing the problem.

Thank you for your help…Bill

John says:

If you email or post the code here, I can take a look at it, and see if I can tell what is causing the problem.

John

BillT says:

Hi John,

I found a mistake that allowed a second call to _trackPageview(). That would certainly cause the problem with bounces. I’m waiting to get some data back and confirm the solution. If it isn’t fixed then I will post the code. Thank you for assistance and your offer to help review the code.

Bill

JoeyM says:

Hi John,

I have been trying to solve this cross-domain problem for quite some time. I’ve called the Google AdWords hotline several times, but they usually come back several days later with a rudimentary suggestion that hasn’t helped. I’m hoping you can help.

My symptoms:
AdWords campaign data is linked and appears in the Analytics Visitors->AdWords reports. However, Goal Data is not showing up in those reports, even though I am receiving Goals for “Success Page” pageviews on the second domain.

Here is my code for the first domain’s send page (that sends the visitor to the next domain):

var gaJsHost = ((“https:” == document.location.protocol) ? “https://ssl.” : “http://www.”);
document.write(unescape(“%3Cscript src=’” + gaJsHost + “google-analytics.com/ga.js’ type=’text/javascript’%3E%3C/script%3E”));

try {
var secondTracker = _gat._getTracker(“UA-1111111-1″);
secondTracker._setDomainName(“none”);
secondTracker._setAllowLinker(true);
secondTracker._setAllowHash(false);
secondTracker._trackPageview(“go_to_domain_2″);
} catch(err) {}

Incidentally I am sending the visitor to the next page via Form -> Post. Here’s what my form looks like:

Since the send page is really a php redirect, I have function ThisFormSubmit()document.offer_form.submit();} located in the and ThisFormSubmit(); at the bottom of the page. This makes sure the GA code is loaded on the page before the _linkByPost method.

So far so good? Hope so!

The code on the second domain looks like the following. Note that we are using two different GA accounts on the second domain, and this may be where things are getting overwritten:

// Checking to see if google analytics script is already loaded
if (typeof(_gat) == “object”) { console.info(“_gat is available”); } else {
var gaJsHost = ((“https:” == document.location.protocol) ? “https://ssl.” : “http://www.”);
document.write(unescape(“%3Cscript src=’” + gaJsHost + “google-analytics.com/ga.js’ type=’text/javascript’%3E%3C/script%3E”));
}

if (typeof(pageTracker) == “object”) { console.info(“pageTracker is available”); } else {
try {
var pageTracker = _gat._getTracker(“UA-2222222-2″);
pageTracker._initData();
} catch(err) { console.error(err); }
}

try {
pageTracker._setDomainName(“none”);
pageTracker._setAllowLinker(true);
pageTracker._setAllowHash(false);
pageTracker._trackPageview(“order_page”);

var secondTracker = _gat._getTracker(“UA-1111111-1″);
secondTracker._initData();
secondTracker._setDomainName(“none”);
secondTracker._setAllowLinker(true);
secondTracker._setAllowHash(false);
secondTracker._trackPageview(“order_page”);
} catch(err) { console.error(err); }

Well, that’s all the code. I think we have successfully installed the code on all necessary pages in the funnel. In case you’re wondering what the funnel looks like, it’s something like this:

Landing page (domain 1) -> Send page (domain 1) -> Sales page (domain 2) -> Order page (domain 2) -> Success page (domain 2)

And to reiterate, I am in fact receiving success Page hits as Goals in account UA-1111111-1. I’m just not seeing those goals in my AdWords visitors reports.

Thanks in advance!

Joey

Magnus Ottosson says:

Hi,

I have tried this version:

var pageTracker = _gat._getTracker(”UA-11111-1″);

pageTracker._setDomainName(’domain.com’);

pageTracker._setCookiePath(’/subdirectory/’);
pageTracker._trackPageview();

var otherTracker = _gat._getTracker(”UA-22222-1″);
otherTracker._setDomainName(’domain.com’);
otherTracker._trackPageview();

Ans yes, it creates two sets of cookies. However I have noticied in my reports that the average time on site is doubled or sometimes tripled when I use this setup compared to just using the default tracking script. Anyone else who has noticed the same problem?

John says:

Joey,

Try removing all of the initData from the code.

John

John says:

Magnus,

I don’t see any problems with the code you posted. If you’d send me a link to the web page, I’d be willing to review the page and see if I can find the issue.

John

sci says:

i use Asynchronous Tracking,then how can i use tow accounts in my site?i want to use different _trackPageview,for example:

a page is 1.html

_gaq.push(['_setAccount', 'UA-7496810-2']);
_gaq.push(['_trackPageview']);
_gaq.push(['t2._setAccount', 'UA-12700744-1']);
_gaq.push(['t2._trackPageview''/book/1']);

does it work well?

John says:

sci,

The method you have in your comment should work fine, as-is. It works as well as the other method, it is just a different syntax for calling all the same functions.

Rick says:

Hi John -

Thanks so much for creating a dedicated place to discuss this esoteric issue. The code below has been implemented across a roots domain and approximately 150 subdomains. Data looks accurate for all subdomains, but the root domain each pageview is being viewed as a unique visit, resulting in entry and exit rates of 100%. One of the differences between the root and subs is the presence of an urchin call that looks like this:

urchinTracker();

Would this cause issues? Or is the code? Any insight you may be able to provide would be extremely appreciated!

var gaJsHost = ((“https:” == document.location.protocol) ? “https://ssl.” : “http://www.”);
document.write(unescape(“%3Cscript src=’” + gaJsHost + “google-analytics.com/ga.js’ type=’text/javascript’%3E%3C/script%3E”));

try{
var pageTracker = _gat._getTracker(“UA-xxxxxxx-2″);
pageTracker._setDomainName(“.site.org”);
pageTracker._setAllowHash(false);
pageTracker._trackPageview();
} catch(err) {}

var gaJsHost = ((“https:” == document.location.protocol) ? “https://ssl.” : “http://www.”);
document.write(unescape(“%3Cscript src=’” + gaJsHost + “google-analytics.com/ga.js’ type=’text/javascript’%3E%3C/script%3E”));

try{
var pageTracker = _gat._getTracker(“UA-xxxxxxx-1″);
pageTracker._setDomainName(“.site.org”);
pageTracker._setAllowHash(false);
pageTracker._trackPageview();
} catch(err) {}

Thanks!!!

John says:

Rick,

It looks like the problem is that the urchinTracker() call is writing cookies in One format, and the _trackPageview calls are writing cookies in a Different format.

When _setAllowHash(false); is used, GA alters the format in which it writes the cookie data.

One fix would be to switch the old style urchinTracker code to the new style pageTracker style.

Another fix would be to add this line above “urchinTracker();”

_uhash = 0;

That line is the equivalent of the _setAllowHash(false); line in the pageTracker version.

You may also benefit from _udn=”.site.org” right above the urchinTracker(); as well. Since that will have all trackers using the same cookies. Though, I don’t know the full situation.

If you are doing no Cross-domain tracking, only tracking across sub-domains, you might remove _setAllowHash(false); rather than adding it to the urchinTracker();

The right answer really depends on your situation and what each separate tracker is trying to track, etc.

Gerry says:

I am not sure of the pros and cons of using setDomainName versus the setCookiePath methods to avoid issues. I have a domain with many subdomains of the form xxx.domain.com. Some of the pages on some of these domains have GA code installed, but I have little control over this as it has been shoehorned in by some of our customers using many different GA accounts.
I would like to set up a separate GA account to track all pages on all the subdomains.
Should I use
pageTracker._setDomainName(’domain.com’);
or
pageTracker._setCookiePath(’/globaltracker/’);

Thanks for the great thread…

John says:

_setCookiePath(‘/globaltracker/’); would only be effective if you actually had a directory on the site called /globaltracker and if all of the content you were tracking was under that directory.

It is hard to say how you should setup global tracking for the entire site, because each of the customer implementations could be different, and interfere with your global code.

If I had to guess, without looking at the site and without looking at each existing implementation, I’d say that
1. you should substitute “globalTracker” for “pageTracker” in every case when you put in the global tracking code
2. you should use globalTracker._setDomainname(‘.domain.com’); in the global tracking code.

Just my best guess,

John

Joe says:

Hey John,

This is bringing up old subject matter, but way back when you answered the questions of tracking domain-1.com and domain-2.com in separate accounts, and then having a 3rd account in which you would aggregate the stats for domain-1.com and domain-2.com together.

I have a similar situation where I have about 4 sites in a network (related content, different entities). I’m currently tracking each site independently and using similar code (yet different) from what you supplied.

You can see my code using the old synchronous code at:
http://www.virtualblueridge.com/
and
http://www.blueridgebookstore.com/

Here is the sample you provided:

For domain-1.com:

var siteTracker = _gat._getTracker(”UA-####-1?);
siteTracker._setDomainName(‘domain-1.com’)
siteTracker._trackPageview();

var overallTracker = _gat._getTracker(”UA-####-2?);
overallTracker._setDomainName(‘www.domain-1.com’);
overallTracker._setAllowHash(false);
overallTracker._setAllowLinker(true);
overallTracker._trackPageview();

For domain-2.com:

var siteTracker = _gat._getTracker(”UA-####-3?);
siteTracker._setDomainName(‘domain-2.com’)
siteTracker._trackPageview();

var overallTracker = _gat._getTracker(”UA-####-2?);
overallTracker._setDomainName(‘www.domain-2.com’);
overallTracker._setAllowHash(false);
overallTracker._setAllowLinker(true);
overallTracker._trackPageview();

Now, I have two questions:

1) How would this code look in the new Asynchronous javascript?

2) If I only apply it to one site, and leave the other as the old code, what repurcussions might I experience?

I appreciate you for your help in advance and think you have great advice on this blog.

Joe

Joe says:

To follow up on my last post so that I don’t seem lazy… here’s my theory on how it should go, but I’m looking for a 2nd, or 3rd set of eyes.

Assuming domain-1.com’s account is UA-####-1,
and domain-2.com’s account is UA-####-2,
and the aggregation account is UA-####-3

Then in looking at the following, am I on the right track?

// for domain-1.com
_gaq.push(['a._setAccount', 'UA-####-1']);
_gaq.push(['a._setDomainName, 'domain-1.com']);
_gaq.push(['a._trackPageview']);
_gaq.push(['b._setAccount', 'UA-####-3']);
_gaq.push(['b._setAllowHash, false]);
_gaq.push(['b._setAllowLinker, true]);
_gaq.push(['b._setDomainName, 'www.domain-1.com']);
_gaq.push(['b._trackPageview']);

// for domain-2.com
_gaq.push(['a._setAccount', 'UA-####-2']);
_gaq.push(['a._setDomainName, 'domain-2.com']);
_gaq.push(['a._trackPageview']);
_gaq.push(['b._setAccount', 'UA-####-3']);
_gaq.push(['b._setAllowHash, false]);
_gaq.push(['b._setAllowLinker, true]);
_gaq.push(['b._setDomainName, 'www.domain-2.com']);
_gaq.push(['b._trackPageview']);

Are there any drawbacks to this?

Thanks again!

Joe

Paul says:

I am needing to track pages in multiple accounts also but is more than 2 accounts.

Will the code below work or is there something else I can try.

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXXXX-X']);
_gaq.push(['_trackPageview']);

// Second tracker
_gaq.push(['nsw._setAccount','UA-YYYYYYYY-Y']);
_gaq.push(['nsw._trackPageview']);

// Third tracker
_gaq.push(['qld._setAccount','UA-YYYYYYYY-Y']);
_gaq.push(['qld._trackPageview']);

// Forth tracker
_gaq.push(['vic._setAccount','UA-YYYYYYYY-Y']);
_gaq.push(['vic._trackPageview']);

(function() {
//As normal code
})();

Also for an onclick tracking or in a javascript function would I be right in thinking I had to reference as below to get the fictitious page logged in each account?

_gaq.push(['_trackPageview', '/Special-Page']);
_gaq.push(['nsw._trackPageview', '/Special-Page']);
_gaq.push(['qld._trackPageview', '/Special-Page']);
_gaq.push(['vic._trackPageview', '/Special-Page']);

Any help is greatly appreciated.

Paul

sriam says:

Paul,
Did your experiment work. We have tested this code and it does not seem to work. Not sure how to implement Multiple tracking in the new GA code.

Sriram

Rohit Patil says:

John,

Would a _SetDomainName(‘none’) work instead of having a unique domain name for both the codes?

Rohit

@Rohit: Be careful with _setDomainName(‘none’). It sets the domain field of the Google Analytics cookie to document.domain. You should only ever use this when you want to track a top-level domain independently from its subdomains, because it will make the top-level domain’s cookies inaccessible from its subdomains.

Rodolfo says:

Hi, John
It seems this post is the only reliable source on this issue.

We tried to use your suggestion as a test to a project we have but it didn’t work:

var pageTracker = _gat._getTracker(‘UA-XXXXXXXX-1′);
pageTracker._setDomainName(‘domain.com’);
pageTracker._trackPageview();

var otherTracker = _gat._getTracker(‘UA-XXXXXXXX-2′);
otherTracker._setDomainName(‘www.domain.com’);
otherTracker._trackPageview();

Are there any circuntances when it doesn’t work?