*/
define('GA_TRACKFILES_EXTENSIONS', '7z|aac|avi|csv|doc|exe|flv|gif|gz|jpe?g|js|mp(3|4|e?g)|mov|pdf|phps|png|ppt|rar|sit|tar|torrent|txt|wma|wmv|xls|xml|zip');
function googleanalytics_help($section) {
switch ($section) {
case 'admin/settings/googleanalytics':
return t('Google Analytics is a free statistics package based on the excellent Urchin system.');
}
}
function googleanalytics_perm() {
return array('administer google analytics', 'use PHP for tracking visibility');
}
function googleanalytics_menu($maycache) {
$items = array();
if ($maycache) {
$items[] = array(
'path' => 'admin/settings/googleanalytics',
'title' => t('Google Analytics'),
'description' => t('Configure the settings used to generate your Google Analytics tracking code.'),
'callback' => 'drupal_get_form',
'callback arguments' => 'googleanalytics_admin_settings_form',
'access' => user_access('administer google analytics'),
'type' => MENU_NORMAL_ITEM,
);
}
return $items;
}
/**
* Implementation of hook_footer() to insert Javascript at the end of the page
*/
function googleanalytics_footer($main = 0) {
global $user;
$id = variable_get('googleanalytics_account', '');
// 1. Check if the GA account number has a value.
// 2. Track page views based on visibility value.
// 3. Check if we should track the currently active user's role.
if (!empty($id) && _googleanalytics_visibility() && _googleanalytics_track($user)) {
// Use the old version of Google Analytics?
$legacy_version = variable_get('googleanalytics_legacy_version', TRUE);
$scope = variable_get('googleanalytics_js_scope', 'footer');
// Should a local cached copy of urchin.js or ga.js be used?
$js_file = ($legacy_version) ? 'urchin.js' : 'ga.js';
$url = 'http://www.google-analytics.com/'. $js_file;
if (variable_get('googleanalytics_cache', 0) && (variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC) == FILE_DOWNLOADS_PUBLIC) && $source = _googleanalytics_cache($url)) {
drupal_add_js($source, 'module', $scope);
}
else {
$script = 'var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");';
$script .= 'document.write(unescape("%3Cscript src=\'" + gaJsHost + "google-analytics.com/'. $js_file .'\' type=\'text/javascript\'%3E%3C/script%3E"));';
drupal_add_js($script, 'inline', $scope);
}
// Download tracking
$path = drupal_get_path('module', 'googleanalytics');
if (variable_get('googleanalytics_trackfiles', TRUE) && $trackfiles_extensions = variable_get('googleanalytics_trackfiles_extensions', GA_TRACKFILES_EXTENSIONS)) {
drupal_add_js(array('googleanalytics' => array('trackDownload' => $trackfiles_extensions, 'LegacyVersion' => $legacy_version)), 'setting', 'header');
drupal_add_js($path .'/downloadtracker.js', 'module', $scope);
}
// Add User profile segmentation values
if (is_array($profile_fields = variable_get('googleanalytics_segmentation', '')) && ($user->uid > 0)) {
$p = module_invoke('profile', 'load_profile', $user);
$fields = array();
foreach ($profile_fields as $field => $title) {
$value = $user->$field;
if (is_array($value)) {
$value = implode(',', $value);
}
$fields[$field] = $value;
}
// Only show segmentation variable if there are specified fields.
$segmentation = '';
if (count($fields) > 0) {
if ($legacy_version) {
$segmentation = '__utmSetVar('. drupal_to_js(implode(':', $fields)) .');';
} else {
$segmentation = 'pageTracker._setVar('. drupal_to_js(implode(':', $fields)) .');';
}
}
}
// Site search tracking support.
$url_custom = '';
if (module_exists('search') && variable_get('googleanalytics_site_search', FALSE) && arg(0) == 'search') {
$keys = search_get_keys();
$url_custom = drupal_to_js(url('search/'. arg(1), 'search='. trim($keys)));
}
// Track access denied (403) and file not found (404) pages.
if (function_exists('drupal_get_headers')) {
$headers = drupal_get_headers();
if (strstr($headers, 'HTTP/1.1 403 Forbidden')) {
if ($legacy_version) {
// See http://www.google.com/support/analytics/bin/answer.py?answer=86928
$url_custom = '"/403.html?page=" + _udl.pathname + _udl.search';
}
else {
// See http://www.google.com/support/analytics/bin/answer.py?answer=86927
$url_custom = '"/403.html?page=" + document.location.pathname + document.location.search + "&from=" + document.referrer';
}
}
elseif (strstr($headers, 'HTTP/1.1 404 Not Found')) {
if ($legacy_version) {
$url_custom = '"/404.html?page=" + _udl.pathname + _udl.search';
}
else {
$url_custom = '"/404.html?page=" + document.location.pathname + document.location.search + "&from=" + document.referrer';
}
}
}
// Add any custom code snippets if specified
$codesnippet = variable_get('googleanalytics_codesnippet', '');
// Should the legacy code be used?
$script = '';
if ($legacy_version) {
$script .= '_uacct = '. drupal_to_js($id) .';';
if (!empty($segmentation)) {
$script .= $segmentation;
}
if (!empty($codesnippet)) {
$script .= $codesnippet;
}
$script .= 'urchinTracker('. $url_custom .');';
}
else {
$script .= 'var pageTracker = _gat._getTracker('. drupal_to_js($id) .');';
$script .= 'pageTracker._initData();';
if (!empty($segmentation)) {
$script .= $segmentation;
}
if (!empty($codesnippet)) {
$script .= $codesnippet;
}
$script .= 'pageTracker._trackPageview('. $url_custom .');';
}
drupal_add_js($script, 'inline', 'footer');
}
}
/**
* Implementation of hook_admin_settings() for configuring the module
*/
function googleanalytics_admin_settings_form() {
$form['account'] = array(
'#type' => 'fieldset',
'#title' => t('Analytics account settings'),
'#collapsible' => FALSE,
);
$form['account']['googleanalytics_account'] = array(
'#type' => 'textfield',
'#title' => t('Google Analytics account number'),
'#default_value' => variable_get('googleanalytics_account', 'UA-'),
'#size' => 15,
'#maxlength' => 20,
'#required' => TRUE,
'#description' => t('The user account number (UA-xxxxxx-x) is unique to the websites domain. You can obtain a user account from the Google Analytics website.', array('@url' => 'http://www.google.com/analytics/'))
);
$form['account']['googleanalytics_legacy_version'] = array(
'#type' => 'checkbox',
'#title' => t('Legacy Google Analytics'),
'#default_value' => variable_get("googleanalytics_legacy_version", TRUE),
'#description' => t('This will enable Legacy Google Analytics (urchin.js) instead of most recent (ga.js).
Please note that Legacy Google Analytics will not receive feature updates and is not compatible with new features.'),
);
// Render the role overview.
$form['roles'] = array(
'#type' => 'fieldset',
'#title' => t('User role tracking'),
'#collapsible' => TRUE,
'#description' => t('Define what user roles should be tracked by Google Analytics.'),
);
$form['roles']['googleanalytics_track__user1'] = array(
'#type' => 'checkbox',
'#title' => t('Admin (user 1)'),
'#default_value' => variable_get('googleanalytics_track__user1', FALSE),
);
$roles = _googleanalytics_user_roles();
foreach ($roles as $rid => $rname) {
$form['roles']['googleanalytics_track_'. $rid] = array(
'#type' => 'checkbox',
'#title' => check_plain($rname),
'#default_value' => variable_get('googleanalytics_track_'. $rid, FALSE),
);
}
$form['segmentation'] = array(
'#type' => 'fieldset',
'#title' => t('User segmentation'),
'#collapsible' => TRUE,
'#description' => t('If your users have profile fields completed, you can track your logged in users based on a defined profile field.')
);
if (!module_exists('profile')) {
$form['segmentation']['profile'] = array(
'#type' => 'markup',
'#value' => t('You need to activate the !profile to use this feature.', array('!profile' => l(t('Profile module'), 'admin/build/modules'))),
'#prefix' => '
', '#suffix' => '
' ); } else { // Compile a list of fields to show. $fields = array( 'uid' => t('User ID'), 'name' => t('Username'), 'roles' => t('User roles') ); $result = db_query('SELECT name, title, type, weight FROM {profile_fields} ORDER BY weight'); while ($record = db_fetch_object($result)) { $fields[$record->name] = $record->title; } $form['segmentation']['googleanalytics_segmentation'] = array( '#type' => 'select', '#title' => t('Track'), '#description' => t('Selecting one or more values allows you to track users by profile values rather than simply an IP address. To select multiple items, hold down CTRL whilst selecting fields.'), '#default_value' => variable_get('googleanalytics_segmentation', ''), '#options' => $fields, '#size' => 10, '#multiple' => TRUE ); } // Link specific configurations. $form['linktracking'] = array( '#type' => 'fieldset', '#title' => t('Link tracking'), '#collapsible' => TRUE, '#collapsed' => FALSE, ); $form['linktracking']['googleanalytics_trackfiles'] = array( '#type' => 'checkbox', '#title' => t('Track files'), '#default_value' => variable_get('googleanalytics_trackfiles', TRUE), '#description' => t('Enables tracking of files based on below file extensions list.') ); $form['linktracking']['googleanalytics_trackfiles_extensions'] = array( '#type' => 'textfield', '#title' => t('File extensions to track'), '#default_value' => variable_get('googleanalytics_trackfiles_extensions', GA_TRACKFILES_EXTENSIONS), '#description' => t('A pipe separated list of file extensions that should be tracked when clicked. Example: !extensions', array('!extensions' => GA_TRACKFILES_EXTENSIONS)) ); // Page specific visibility configurations. $form['page_vis_settings'] = array( '#type' => 'fieldset', '#title' => t('Page specific tracking settings'), '#collapsible' => TRUE, '#collapsed' => TRUE, ); $access = user_access('use PHP for tracking visibility'); $visibility = variable_get('googleanalytics_visibility', 0); $pages = variable_get('googleanalytics_pages', ''); if ($visibility == 2 && !$access) { $form['page_vis_settings'] = array(); $form['page_vis_settings']['visibility'] = array('#type' => 'value', '#value' => 2); $form['page_vis_settings']['pages'] = array('#type' => 'value', '#value' => $pages); } else { $options = array(t('Add to every page except the listed pages.'), t('Add to only the listed pages.')); $description = t("Enter one page per line as Drupal paths. The '*' character is a wildcard. Example paths are %blog for the blog page and %blog-wildcard for every personal blog. %front is the front page.", array('%blog' => 'blog', '%blog-wildcard' => 'blog/*', '%front' => 'TRUE
(PHP-mode, experts only).');
$description .= ' '. t('If the PHP-mode is chosen, enter PHP code between %php. Note that executing incorrect PHP-code can break your Drupal site.', array('%php' => ''));
}
$form['page_vis_settings']['googleanalytics_visibility'] = array(
'#type' => 'radios',
'#title' => t('Add tracking to specific pages'),
'#options' => $options,
'#default_value' => $visibility,
);
$form['page_vis_settings']['googleanalytics_pages'] = array(
'#type' => 'textarea',
'#title' => t('Pages'),
'#default_value' => $pages,
'#description' => $description,
);
}
// Advanced feature configurations.
$form['advanced'] = array(
'#type' => 'fieldset',
'#title' => t('Advanced'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#description' => t('You can add custom Google Analytics code here.')
);
$form['advanced']['googleanalytics_cache'] = array(
'#type' => 'checkbox',
'#title' => t('Cache tracking code file locally'),
'#description' => t("If checked, the tracking code file is received from Google Analytics and cached locally. It is updated daily from Google's servers to ensure updates to tracking code are reflected in the local copy. Do not activate this until after Google Analytics has confirmed your tracker."),
'#default_value' => variable_get('googleanalytics_cache', 0),
);
if (variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC) == FILE_DOWNLOADS_PRIVATE) {
$form['advanced']['googleanalytics_cache']['#disabled'] = TRUE;
$form['advanced']['googleanalytics_cache']['#description'] .= ' '. t('Public file transfers must be enabled to allow local caching.', array('!url' => url('admin/settings/file-system', drupal_get_destination())));
}
$site_search_dependencies = '