05.22Multiple Google analytics accounts on a single domain
Google analytics is a great tool used by web developers and web media professionals to monitor web traffic to a particular domain. The analytical data generated by Google analytics can be used to streamline a company or brand’s web strategy to reach a much greater or focused audience in a way that was never before possible using conventional web server tracking systems. The problem however arises when, for example, a particular company has different sub divisions or different unrelated campaigns which they require analytics data for, but wish to view this data independently of the analytics data for the rest of the site.
A friend and colleague of mine, Gary Greenberg from GIGtech (http://www.gigtech.co.za), has come up with a rather interesting way of accomplishing this and has decided to accompany me in creating this post. So a big thank you to him. The following is meant to be a step-by step guide to setting up multiple Google analytics accounts on a single domain. It is by no means perfect and may require tweaking , but the main idea and working model will be presented. This tutorial assumes you already have a gmail account that you can use to set up analytics accounts.
Step 1: Create a Google analytics website profile for the main domain (www.example.com)
Log in to Google analytics by either entering your Google details or registering for Google analytics using your Google details. If you do not already know the address, Google analytics can be accessed by navigating to http://www.google.com/analytics . From here, click on the link “Add Website Profile” located on the bottom left of the main page. Using the form that is displayed, ensure the radio button titled “Add a Profile for a new domain” is selected and in the URL text box, enter the domain name. For the purpose of this tutorial, this would be www.example.com, select your time zone and click “Continue”. You will now be taken to the screen which presents you with your tracking code for that web profile. Copy this tracking code into a text document for later reference.
Step 2: Create a website profile for each required sub folder on your domain (www.example.com/folder1 | www.example.com/folder2)
Now create a separate analytics website profile for each of the sub folders on the domain that you may wish to monitor. Repeat the process as described above, entering the domain with the sub folder into the URL text field. Remember to leave out the leading forward slash as this may cause issues (i.e. www.example.com/folder1 NOT www.example.com/folder1/). For each of the new website profiles you create for the sub directories on your site, remember to keep the tracking code in a safe place for later use. You are going to want your site to determine which tracking code to initiate based on where the user is on your domain. If all pages on your site make use of a common include file, for example “top.php”, containing the header information for all pages, you will require logic to determine which tracking ID code to implement based on which page is being loaded. If each page makes use of its own header information, you can simply insert the relevant tracking code for each website profile in the bottom section of each page in each corresponding sub folder.
Step 3: Verification work-around for tracking code on your domain
In order for the tracking code to be verified by Google please ensure you place its reference on the index page of your root folder (e.g. www.example.com/index.php). To accomplish this, you need to manually insert all the tracking codes on your root’s index page - even if you are not making use of this page this work-around will allow the verification process to complete. The best approach to design this is to make use of a dynamic programming language such as PHP or Ruby to prevent you from having to update this code every time.
For us to continue with the dynamic work-around, you will first have to understand how Google has designed the format of their user accounts and tracking codes. Firstly, when you register for a Google Analytics account you will be given an account number which is probably an incremented number such as 0000007. Once you have been assigned this unique identifier you will then make use of it for your Analytics website profiles in the format UA-0000007-2 (this denotes: user account 7 of analytics profile 2). You may create a for loop, in your preferred language, for profiles from one to thirty for example - presuming you might create a number of profiles in the future and don’t wish to update this limit every time. This will subsequently run through the profiles without considering if they exist or not therefore “fooling” Google into thinking the codes are immediately being made use of once you create a new website profile in your Analytics account. The following diagram attempts to explain what is described above:
You are therefore required to insert script similar to the following on the main index page of your domain (i.e. www.example.com/index.php), just underneath the closing </body> tag:
<script src=”http://www.google-analytics.com/urchin.js” type=”text/javascript”>
</script><?php for ($x=2; $x<=20; $x++)
{
?>
<script type=”text/javascript”>
_uacct = “UA-0000007-<? echo $x; ?>”;
urchinTracker();
</script>
<?php
}
?>
The above code basically initializes all the tracking codes for the profiles you created using the analytics control panel. The above script runs on a loop up to twenty to account for any future website profiles you may create. This is an important step, if these tracking codes are not initialized in the root of your domain, the subfolders cannot be tracked.
Step 4: Insert the tracking code for each sub folder in their corresponding sections
For the tracking of your pages to occur properly, you are required to place the tracking code for each sub folder on your domain within the template section of each sub folder. Google requests that you place the code at the end of your template to prevent their code from hampering the loading of the information on the relevant page that is being viewed by the end user. This will therefore allow the tracking code to initialize once the majority of the page has been loaded thus allowing Google’s script to load more efficiently. Users of the page will therefore not be forced to wait for the tracking code to process before the page they are attempting to view is fully displayed. You can decide to either make use of a single common include utilized by each folder on your domain (the recommended approach), or you can insert the unique tracking code with each sub folder’s unique tracking ID on each of the sub folder’s pages manually, but this would most likely become rather time consuming. The code you would insert would consist of something like the following (for use with a common include file utilized by all sub folders):
<script src=”http://www.google-analytics.com/urchin.js” type=”text/javascript”></script>
<script type=”text/javascript”>
<?php//Sub-folder 1
if($page_id==2)
{
?>
_uacct = “UA-0000007-2″;
<?php
} //Sub-folder 2
elseif ($page_id==3)
{
?>
_uacct = “UA-0000007-3″;
<?php
}//Sub-folder 3
elseif ($page_id==4)
{
?>
_uacct = “UA-0000007-4″;
<?php
}
?>
urchinTracker();
</script><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-0000007-1″);
pageTracker._initData();
pageTracker._trackPageview();
</script>
The above code checks the page ID of the current page (declared before including the common include file) and uses that to declare and invoke the relevant Google analytics tracking code with corresponding tracking ID. You will notice an additional piece of JavaScript at the bottom. This script is used to initialize the tracking code ID of the main domain (www.example.com, UA-0000007-1), this allows the tracking of the entire domain to occur within the main profile. With all this code inserted within all relevant pages, you will now be tracking the entire domain as well as each separate folder. To view tracking data, you will simply need to click on the required website profile within the analytics control panel and view its corresponding reports. If you are making use of an IDE editor such as Dreamweaver, you might want to consider creating an editable region or Dreamweaver variable to accommodate for the multiple tracking codes if you are making use of the same template throughout the website. There you have it, a step by step guide to setting up multiple Google analytics accounts on a single domain. If you have any questions or problems, or if you notice any inconsistencies in this tutorial, please comment below and we will try and assist wherever we can.



Nice write up, but it looks like Miss Heidi had the better solution, check it…
http://www.gottaquirk.com/post/1200/how-to-track-more-than-four-goals-in-google-analytics-part-2
July 31st, 2008 at 10:49 pm
I’d have to say that Heidi’s solution has a different use and focus than the above tutorial. Heidi’s solution could be used to extend upon what is discussed above as a way of analyzing results. This methodology was used to address a problem which we faced while dealing with a client needing consolidated, individual reports for subfolders on a domain. The above article also deals with the technical aspects associated with configuring the analytics tracking code within website files. Heidi’s article is very useful nonetheless.
August 4th, 2008 at 10:18 am