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:

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',
);

Comments

MattFielding’s picture

i18n in Drupal 7 works differently and utilises the Variable module. The methods outlined above will no longer work.

Instead a custom module with the following hooks is required to allow the 'googleanalytics_account' variable to be translatable.

<?php
/**
* Implements hook_variable_info().
*/
function mymodule_variable_info($options) {
  $variables['googleanalytics_account'] = array(
    'title' => t('Google Analytics Web Property ID'),
  );

  return $variables;
}

/**
* Implements hook_variable_info_alter() to make these variables 'localizable' .
*/
function mymodule_variable_info_alter(&$variables, $options) {
  // Make the above variables translatable
  $variables['googleanalytics_account']['localize'] = TRUE;
}

The 'Google Analytics Web Property ID' can then be selected on the i18n variable settings page. Enabling the variable on this page will give the Google Analytics admin page extra options.

ktrev’s picture

I am really confused what to do..
Can you please explain all the steps for handshaking between domain access module and Google analytics.
I want each of my domain to be registered separately in Google analytics

ngstigator’s picture

Confirming that this works with current iterations of googleanalytics and i18n. Thanks, Matt, for sharing!

heikki’s picture

In Drupal 8, the variable name has changed a bit, use this format:
$config['google_analytics.settings']['account'] = 'UA-1234-1';