*/
define('GA_TRACKFILES', 'pdf|zip|mp3');
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_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',
'access' => user_access('administer site configuration'),
'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', '');
// Check if we should track the currently active user's role
$track = _googleanalytics_track($user);
// Don't track page views in the admin sections
if ($id && (arg(0) != 'admin') && $track == TRUE) {
$prefix = '://www';
// Are we on a secure page?
if (isset($_SERVER['HTTPS']) && !empty($_SERVER['HTTPS']) && (strtolower($_SERVER['HTTPS']) != 'off')) {
$prefix = 's://ssl';
}
// Use the old version of Google Analytics?
$legacy_version = variable_get('googleanalytics_legacy_version', TRUE);
// 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)) .');';
}
}
}
// Download tracking
$path = drupal_get_path('module', 'googleanalytics');
if ($trackfiles = variable_get('googleanalytics_trackfiles', GA_TRACKFILES)) {
drupal_add_js(array('googleanalytics' => array('trackDownload' => $trackfiles, 'LegacyVersion' => $legacy_version)), 'setting', 'header');
drupal_add_js($path .'/downloadtracker.js', 'module', 'footer');
}
// Add any custom code snippets if specified
$codesnippet = variable_get('googleanalytics_codesnippet', '');
// Should a local cached copy of urchin.js or ga.js be used?
if (variable_get('googleanalytics_cache', 0) && (variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC) == FILE_DOWNLOADS_PUBLIC)) {
if ($legacy_version) {
$source = _googleanalytics_cache('http://www.google-analytics.com/urchin.js');
}
else {
$source = _googleanalytics_cache('http://www.google-analytics.com/ga.js');
}
if (!empty($source)) {
$source = base_path() . $source;
}
}
if (!isset($source)) {
if ($legacy_version) {
$source = 'http'. $prefix .'.google-analytics.com/urchin.js';
}
else {
$source = 'http'. $prefix .'.google-analytics.com/ga.js';
}
}
// 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 = url('search/'. arg(1), 'search='. trim($keys));
}
// Surround custom urls with single quotes.
if (!empty($url_custom)) {
$url_custom = drupal_to_js($url_custom);
}
// Should the legacy code be used?
if ($legacy_version) {
$script = '\n";
$script .= '\n";
}
else {
$script = '\n";
$script .= '\n";
}
return $script;
}
}
/**
* Implementation of hook_admin_settings() for configuring the module
*/
function googleanalytics_admin_settings() {
$form['account'] = array(
'#type' => 'fieldset',
'#title' => t('Analytics Account Settings'),
'#collapsible' => FALSE,
);
$form['account']['googleanalytics_account'] = array(
'#type' => 'textfield',
'#title' => t('User ID'),
'#default_value' => variable_get('googleanalytics_account', 'UA-'),
'#size' => 15,
'#maxlength' => 20,
'#required' => TRUE,
'#description' => t('The user account 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 instead of most recent.
Please note that Legacy Google Analytics will not receive feature updates and is not compatible with new features.'),
);
// Render the role overview.
$result = db_query('SELECT * FROM {role} ORDER BY name');
$form['roles'] = array(
'#type' => 'fieldset',
'#title' => t('User Role Tracking'),
'#collapsible' => TRUE,
'#description' => t('Define what user roles should be tracked by Google Analytics. Note: Drupal Admin pages are never tracked.'),
);
$form['roles']['googleanalytics_track__user1'] = array(
'#type' => 'checkbox',
'#title' => t('Admin (user 1)'),
'#default_value' => variable_get('googleanalytics_track__user1', FALSE),
);
while ($role = db_fetch_object($result)) {
$form['roles']['googleanalytics_track_'. $role->rid] = array(
'#type' => 'checkbox',
'#title' => check_plain($role->name),
'#default_value' => variable_get('googleanalytics_track_'. $role->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 ); } $form['googleanalytics_trackfiles'] = array( '#type' => 'textfield', '#title' => t('File Extensions To Track'), '#default_value' => variable_get('googleanalytics_trackfiles', GA_TRACKFILES), '#description' => t('A pipe seperated list of file extensions that should be tracked when clicked. Example !extensions', array('!extensions' => GA_TRACKFILES)) ); $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 = '