Developer tip on placing multiple Google analytics codes on various pages.
MYMODULE = multi_ga
/**
* Implementation of hook_menu().
*/
function MYMODULE_menu() {
$items['admin/config/multi_ga/settings-ga'] = array(
'title' => 'Google analytic settings',
'description' => 'Place google analytic setting for each pages.',
'page callback' => 'drupal_get_form',
'page arguments' => array('MYMODULE_ga_config'),
'access arguments' => array('administer'),
);
return $items;
}
/**
* Google Analytic settngs.
*/
function MYMODULE_ga_config($form_state) {
$description = t('Separate each Google Analytic code by semicolon. individual by comma. e.g UA-52705955-1,news-events/*;');
$format = t('
Format: google_analytic_code,path_pattern;');
$form['MYMODULE_google_analytic_mapping'] = array(
'#type' => 'textarea',
'#title' => t('Google Analytic Pathwise Mapping.'),
'#description' => $description . $format,
'#default_value' => variable_get('MYMODULE_google_analytic_mapping', ''),
'#required' => FALSE
);
return system_settings_form($form);
}
/**
* Add css to a particular path.
* @param [type] $page [description]
* @return [type] [description]
*/
function MYMODULE_page_alter(&$page) {
MYMODULE_extend_google_anaytics();
}
/**
* Set Google Analytic ID for each path pattern settings.
* @return [type] [description]
*/
function MYMODULE_extend_google_anaytics() {
$site_path = drupal_get_path_alias();
$MYMODULE_google_analytic_mapping = MYMODULE_determine_ga_mappings('MYMODULE_google_analytic_mapping', '');
foreach ($MYMODULE_google_analytic_mapping as $pos => $data_info) {
foreach ($data_info as $path => $data) {
if (drupal_match_path($site_path, $path)) {
$google_id = drupal_strtoupper($data['name']);
MYMODULE_apply_google_analytic($google_id);
}
}
}
}
/**
* Apply google Analytic on the page.
* @param [type] $google_id [description]
* @return [type] [description]
*/
function MYMODULE_apply_google_analytic($google_id) {
if ($google_id != '') {
$script = "var _gaq = _gaq || [];_gaq.push(['_setAccount', '$google_id']);_gaq.push(['_trackPageview']);(function() {var ga = document.createElement('script');ga.type = 'text/javascript';ga.async = true;ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';var s = document.getElementsByTagName('script')[0];s.parentNode.insertBefore(ga, s);})();";
drupal_add_js($script, array('type' => 'inline', 'scope' => 'header'));
}
}
/**
* Get Configuration processed in array form for Google Analytic.
* @param string $key [description]
* @return [type] [description]
*/
function MYMODULE_determine_ga_mappings($mapping_key, $default_data = '') {
$ga_label = variable_get($mapping_key, $default_data);
$ga_label = str_ireplace(" ", "", $ga_label);
$ga_label_arr = explode(";", $ga_label);
$ga_label_mapping = array();
foreach($ga_label_arr as $pos => $data) {
$data_arr = explode(",", $data);
if (!isset($data_arr[0])) {
continue;
}
$key = drupal_strtolower($data_arr[0]);
$name = (isset($data_arr[1])) ? $data_arr[1] : '';
if (empty($name)) {
continue;
}
$ga_label_mapping[$pos][$name]['name'] = $key;
}
return $ga_label_mapping;
}