Written in collaboration with Travis Moser, Experience Architect
Setting up Google Analytics to capture site metrics is usually a pretty straightforward task. However, sometimes it requires a bit of creative thinking when you run into unexpected challenges.
The Challenge
On a recent project, we were installing Google Analytics to capture data for a website that was contained within a sub-directory of a larger corporate site. The business owner wanted to capture data specific to her sub-site—in particular—how many visits to the sub-site it took for a user to submit a contact form. It was important for the business owner to know this to make a judgment on how well the content was doing to explain the offering and persuade the user to take the next step. A reasonable request, right?
Here’s where the challenge came in. Because marketing campaigns were going to be sending visitors directly to the sub-site, the business owner needed to record visitors to the sub-site separately from visitors to the larger corporate site. Currently, the cookie tracking unique visits was being written to the corporate domain. So, for example, if a user visited the corporate site two times, then visited the sub-site twice and submitted a contact form on the second visit, Google Analytics would show that it took this user four visits to submit a contact form. We would have no way of knowing how many of those visits prior to the last visit were actually visits to the sub-site.
We called on Experience Architect Travis Moser to do a little investigating. Here’s what he learned and how he recommended resolving the problem.
The Solution
We needed to figure out how to show that even if visitors had previously visited the larger corporate site, they should be counted as new visitors when they arrived on the sub-site. At the same time, we needed to track these visits to the sub-site to the larger corporate site Google Analytics account. Essentially, we needed to track the visitors to the sub-site twice—once to the larger corporate Google Analytics account—and once to the sub-site Google Analytics account, while making sure the data for one account didn’t affect the data for the other.
On the surface, that doesn’t seem too difficult. Let’s say the domain name for the site is http://www.acme.com and the sub-site is at http://www.acme.com/sub-site/. After some research, this is the code that we found others using:
var _gaq = _gaq || []; gaq.push( ['setAccount', 'UA-XXXXA-X'], // global site account number ['trackPageview'], ['ss.setAccount', 'UA-XXXXB-X'], // sub-site account number ['ss.trackPageview'] );
The problem with this code is that because both site accounts use the same domain name, their cookies will conflict, resulting in inaccurate visitor counts. The accuracy of visitor counts was critical to determining the effectiveness of the site content. We realized we needed to force Google Analytics to set two separate cookies—one for each account. This can be accomplished by either setting a separate domain name for each account or by setting the cookie path for one of the accounts to a different directory. Because the sub-site is in a separate directory, it made more sense to set the cookie path for the sub-site account like this:
var _gaq = _gaq || [];
gaq.push(
['setAccount', 'UA-XXXXA-X'], // global site account number
['trackPageview'],
['ss.setAccount', 'UA-XXXXB-X'], // sub-site account number
['ss.setCookiePath', '/sub-site'], // different cookie for sub-site
['ss.trackPageview']
);
Now the data is tracked into two separate Google Analytics accounts with two separate cookies: the larger corporate site account’s cookie domain is set to www.acme.com, and the sub-site’s cookie domain is set to www.acme.com/sub-site/. Problem solved! The business owner will have an understanding of how the new site content is doing to get visitors to take action, and the corporate site owners will still be able to accurately track unique visitors to the site overall.
Travis, you rock!



