Community Documentation

How to change the Web Property ID per hostname / domain / language

Last updated March 25, 2011. Created by hass on October 4, 2009.
Edited by EvanDonovan. Log in to edit this page.

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.

Change Web Property ID per hostname

[settings.php]

<?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';
}
?>

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]

<?php
$conf
['i18n_variables'] = array (
  
'googleanalytics_account',
);
?>

Comments

Drupal 7

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.

Page status

No known problems

Log in to edit this page

About this page

Drupal version
Drupal 5.x, Drupal 6.x, Drupal 7.x
Audience
Site administrators

Site Building Guide

Drupal’s online documentation is © 2000-2012 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License.
nobody click here