Google Analytics subdomain tracking, custom script & filters
There are a few pro’s and con’s to setting up individual Google Analytics website profiles for subdomains VS using a custom script to track to track your visitors across a website’s various subdomains. This post will cover using a custom Google Analytics script to track your visitor data across various subdomains within your website. If you are concerned on how you will be able to differ between the various subdomains from your analytics data, not to worry, i will cover that further within this post.
I recently had the task of creating a new analytics account for a client, where the website had a few key subdomains, each with their own unique content, however the parent domain was simply a landing page, used to direct users to the relevant subdomains depending on the content needs. Now as the parent domain performs well within search engines (as well as the central point of contact for any navigational search), the need to track visitor data from the parent domain and then across all subdomains became a necessity.
Custom GA tracking script
After creating your analytics account and your first website profile (for your parent domain), you will need to make a small change to the GA tracking script, which you will be installing on the parent domain and across all subdomains and their pages.
In the above screenshot, we have added an additional line of code to the script (directly under the UA tracking line), pagetracker._setDomainName(“.example.com”);. This allows Google Analytics to use the same cookies across the various subdomains to track visitor data. The downside to this would be that you will not be able to differ between similar page names within the subdomains, such as area.example.com/index.html and location.example.com/index.html (Analytics reports only display the request URI). Adding custom filters to your profiles will however allow you to to differ between the subdomain pages.
Create additional website profiles for each of your subdomains
Creating custom filters
Now it’s time to start setting up filters for each of your subdomain website profiles. The current tracking script customisation tracks visits across all subdomains, therefore we will need to add a filter to each subdomain profile to only include traffic from that specific domain for your reports. Select the edit option associated with the subdomain profile and add a new filter. I only want to see visitor data associated with the subdomain profile i’ve created, now create a custom filter to include only from the subdomain associated with our profile.
As seen in the above screenshot, i am filtering all traffic to display only traffic associated with the specific subdomain website profile within the filter pattern. You will need to create a custom filter for each of the subdomain website profiles you have created within your Google Analytics account to track individual subdomain visitor data. Take note that additional subdomains can be added to the pattern filter if needed by simply separating them with a pipe (|).
Finally, our parent domain profile will also require a filter, as mentioned earlier, Google Analytics only displays the request URI, therefore it will be difficult to differ between pages with the same name. For this i need to create an advanced filter for the parent domain website profile that will append the hostname to the request URI to display in the analytics reports.
Ensure that the field entries for the advanced filter are entered in identically to the ones within the screenshot above. Save the filter and we are now complete. Your Analytics account setup should look similar to the diagram below.
Now you are ready to track subdomain specific data within the individual website profiles created, as well as see an overview report from the parent domain profile for all data across the subdomains and parent domain for all your reporting needs.