How to change the Web Property ID per hostname / domain / language
In rare situations you may like to use different Google Analytics Web Property ID's (UA-1234-1) on your sites. By adding the below code snippets to your sites sites/default/settings.php your are able to override the Google Analytics Web Property ID dynamically.
Change Web Property ID per hostname
[settings.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';
}
D7: Change Web Property ID via Internationalization module (i18n), Organic Groups, and additional contexts.
The Google Analytics module 7.x-1.3+ integrates with Variables module. It is possible to create additional Web Property values for the contexts provided in a given Variable Realm. Examples include:
- Variable Translation (i18n) allows setting a Web Property ID per language.
- OG Variables permits settings Web Property IDs in an OG Context.
- Domain Variable permits settings the Web Property ID for a combination of domains and languages
- If logic beyond these modules is necessary, one can create their own Variable realm and corresponding class/methods.
D6: Change Web Property ID with Internationalization module (i18n)
If you are running the Internationalization module you can setup a Web Property ID per language. I18n allows you to have Multilingual Variables. After you add the below code snippet to your settings.php you can go to your Google Analytics settings page and will see This is a multilingual variable. on the end of the Web Property ID description. Now you are able to configure a Web Property ID per language.
[settings.php]
$conf['i18n_variables'] = array (
'googleanalytics_account',
);
Help improve this page
You can:
- Log in, click Edit, and edit this page
- Log in, click Discuss, update the Page status value, and suggest an improvement
- Log in and create a Documentation issue with your suggestion