I am looking to use different Google Analytics codes based on the domain the site is being viewed on.
I read that the following code will work well for my scenario (in a Drupal environement):
<?php
// Override Google Analytics Web Property ID per hostname.
// Hostnames need to be lower-case!
switch ($_SERVER['HTTP_HOST']) {
case 'www.example.com':
case 'www.example.net':
case 'forum.example.net':
$conf['googleanalytics_account'] = 'UA-123456-2';
break;
case 'www.example.org':
$conf['googleanalytics_account'] = 'UA-123456-3';
break;
default:
$conf['googleanalytics_account'] = 'UA-123456-1';
}
?>
I am using WordPress. Will this code work for me? If so, I do not exactly understand it…Is this saying that all sites that ARE NOT listed, will use teh default GA code (bottom one), an the http://www.example.org will use the middle GA code, and all of the others will use the top GA code?
You shouldn’t be setting filtering at the website level. In a nutshell you should use your GA code to declare ALL the subdomains and domains you want to bring into your GA account. Then, in your GA account, you can set up multiple profiles with filtering options.
http://services.google.com/analytics/breeze/en/v5/domains_subdomainsv22_ad1/
(Jump right to slide 12 if you want a simple graphic overview)
Do this:
1.) Set up your site GA code to encompass all the domains you want to track.
2.) Choose the account in GA > Admin Menu > Create New Profile
3.) Click the Filters tab > Choose Predefined Filter > Include only > traffic from the domains > (a subdomain/domain)
4.) Save
Now do that for every subdomain/domain you want to add. This is generally the best practice for this.