'));
if ($access) {
$options[] = t('Add if the following PHP code returns 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,
);
}
$profile_enabled = module_exists('profile');
$form['segmentation'] = array(
'#type' => 'fieldset',
'#title' => t('User segmentation settings'),
'#collapsible' => TRUE,
);
// Compile a list of fields to show.
$fields = array(
'uid' => t('User identifier'),
'name' => t('User name'),
'roles' => t('User roles')
);
if ($profile_enabled) {
$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('Add segmentation information to tracking code'),
'#description' => t('Segment users based on different properties, additionally to the basic IP address based tracking provided by Google Analytics.') . (!$profile_enabled ? ' '. t('Enable the profile module to be able to use profile fields for more granular tracking.', array('@module_list' => url('admin/build/modules'))) : '') .' '. t('Selecting one or more values is supported. To select multiple items, hold down CTRL while selecting fields.'),
'#default_value' => variable_get('googleanalytics_segmentation', ''),
'#options' => $fields,
'#size' => $profile_enabled ? 10 : 3,
'#multiple' => TRUE
);
// Link specific configurations.
$form['linktracking'] = array(
'#type' => 'fieldset',
'#title' => t('Link tracking settings'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$form['linktracking']['googleanalytics_trackoutgoing'] = array(
'#type' => 'checkbox',
'#title' => t('Track outgoing links'),
'#default_value' => variable_get('googleanalytics_trackoutgoing', 1),
'#description' => t('Enables tracking of clicks on outgoing links.')
);
$form['linktracking']['googleanalytics_trackmailto'] = array(
'#type' => 'checkbox',
'#title' => t('Track mailto links'),
'#default_value' => variable_get('googleanalytics_trackmailto', 1),
'#description' => t('Enables tracking of clicks on mailto links.')
);
$form['linktracking']['googleanalytics_trackfiles'] = array(
'#type' => 'checkbox',
'#title' => t('Track download links'),
'#default_value' => variable_get('googleanalytics_trackfiles', 1),
'#description' => t('Enables tracking of clicks on links to files based on the file extensions list below.')
);
$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 with regular expression support. Example: !extensions', array('!extensions' => GA_TRACKFILES_EXTENSIONS))
);
// Advanced feature configurations.
$form['advanced'] = array(
'#type' => 'fieldset',
'#title' => t('Advanced settings'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['advanced']['googleanalytics_legacy_version'] = array(
'#type' => 'radios',
'#title' => t('Google Analytics version used'),
'#default_value' => variable_get('googleanalytics_legacy_version', 0),
'#options' => array(
0 => t('Latest (ga.js) tracking code'),
1 => t('Legacy (urchin.js) tracking code')
),
'#description' => t('On December 13, 2007, Google rolled out a new API for its tracking code, and suggests all new sites to use this code. You should only use the older legacy code, if you have custom tracking code tied to that API. Otherwise it is suggested you use the latest API, as the legacy code will not receive feature updates and is not compatible with new features.', array('@ga_js_url' => 'http://analytics.blogspot.com/2007/12/announcing-new-graphing-tools-gajs.html')),
);
$form['advanced']['googleanalytics_cache'] = array(
'#type' => 'checkbox',
'#title' => t('Cache tracking code file locally'),
'#description' => t("If checked, the tracking code file is retrieved 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 = '';
$site_search_dependencies .= t('Depends on: !dependencies', array('!dependencies' => (module_exists('search') ? 'Search'. t(' (enabled)') : 'Search'. t(' (disabled)'))));
$site_search_dependencies .= '
';
// Google already have many translations, if not - they display a note to change the language.
global $locale;
$form['advanced']['googleanalytics_site_search'] = array(
'#type' => 'checkbox',
'#title' => t('Track internal search'),
'#description' => t('If checked, internal search keywords are tracked. You must configure your Google account to use the internal query parameter search. For more information see How do I set up Site Search for my profile.', array('!url' => 'http://www.google.com/support/analytics/bin/answer.py?hl='. $locale .'&answer=75817')) . $site_search_dependencies,
'#default_value' => variable_get('googleanalytics_site_search', FALSE),
'#disabled' => (module_exists('search') ? FALSE : TRUE),
);
$form['advanced']['googleanalytics_codesnippet'] = array(
'#type' => 'textarea',
'#title' => t('Custom JavaScript code'),
'#default_value' => variable_get('googleanalytics_codesnippet', ''),
'#rows' => 5,
'#description' => t('You can add custom Google Analytics code snippets here. These will be added to every page that Google Analytics appears on. Do not include the <script> tags, and always end your code with a semicolon (;).', array('!snippets' => 'http://drupal.org/node/248699'))
);
$form['advanced']['googleanalytics_js_scope'] = array(
'#type' => 'select',
'#title' => t('JavaScript scope'),
'#description' => t("Warning: Google recommends adding the external JavaScript files to footer for performance reasons."),
'#options' => array(
'footer' => t('Footer'),
'header' => t('Header'),
),
'#default_value' => variable_get('googleanalytics_js_scope', 'footer'),
);
return system_settings_form($form);
}
function googleanalytics_admin_settings_form_validate($form_id, $form_values) {
if (!preg_match('/^UA-\d{4,}-\d+$/', $form_values['googleanalytics_account'])) {
form_set_error('googleanalytics_account', t('A valid Google Analytics account number is case sensitive and formatted like UA-xxxxxx-x.'));
}
// This is for the Newbie's who cannot read a text area description.
if (preg_match('/^(.*)<\/?script(.*)>(.*)/', $form_state['values']['googleanalytics_codesnippet'])) {
form_set_error('googleanalytics_codesnippet', t('Do not include the <script> tags in this field.'));
}
}
function googleanalytics_admin_settings_form_submit($form_id, $form_values) {
// Trim some text area values.
$form_values['googleanalytics_pages'] = trim($form_values['googleanalytics_pages']);
$form_values['googleanalytics_codesnippet'] = trim($form_values['googleanalytics_codesnippet']);
system_settings_form_submit($form_id, $form_values);
}
/**
* Implementation of hook_requirements().
*/
function googleanalytics_requirements($phase) {
$requirements = array();
if ($phase == 'runtime') {
// Raise warning if Google user account has not been set yet.
if (!preg_match('/^UA-\d{4,}-\d+$/', variable_get('googleanalytics_account', 'UA-'))) {
$requirements['googleanalytics'] = array(
'title' => t('Google Analytics module'),
'description' => t('Google Analytics module has not been configured yet. Please configure its settings from the Google Analytics settings page.', array('@url' => url('admin/settings/googleanalytics'))),
'severity' => REQUIREMENT_ERROR,
'value' => t('Not configured'),
);
}
}
return $requirements;
}
/**
* Implementation of hook_cron().
*/
function googleanalytics_cron() {
// Regenerate the google analytics urchin.js or ga.js every day.
if (time() - variable_get('googleanalytics_last_cache', 0) >= 86400) {
// Legacy google analytics version.
file_delete(file_directory_path() .'/googleanalytics/urchin.js');
// New google analytics version.
file_delete(file_directory_path() .'/googleanalytics/ga.js');
variable_set('googleanalytics_last_cache', time());
}
}
/**
* Download and cache the urchin.js file locally.
* @param $location
* The full URL to the external javascript file.
* @return mixed
* The path to the local javascript file on success, boolean FALSE on failure.
*/
function _googleanalytics_cache($location) {
$directory = file_directory_path() .'/googleanalytics';
$file_destination = $directory .'/'. basename($location);
if (!file_exists($file_destination)) {
$result = drupal_http_request($location);
if ($result->code == 200) {
// Check that the files directory is writable
if (file_check_directory($directory, FILE_CREATE_DIRECTORY)) {
return file_save_data($result->data, $directory .'/'. basename($location), FILE_EXISTS_REPLACE);
}
}
}
else {
return $file_destination;
}
}
/**
* Tracking visibility check for an user object.
*
* @param $account
* A user object containing an array of roles to check.
* @return boolean
* A decision on if the current user is being tracked by Google Analytics.
*/
function _googleanalytics_visibility_user($account) {
$enabled = FALSE;
// Is current user a member of a role that should be tracked?
if (_googleanalytics_visibility_roles($account)) {
// Use the user's block visibility setting, if necessary.
if (($custom = variable_get('googleanalytics_custom', 0)) != 0) {
if ($account->uid && isset($account->googleanalytics['custom'])) {
$enabled = $account->googleanalytics['custom'];
}
else {
$enabled = ($custom == 1);
}
}
else {
$enabled = TRUE;
}
}
return $enabled;
}
/**
* Based on visibility setting this function returns TRUE if GA code should
* be added for the current role and otherwise FALSE.
*/
function _googleanalytics_visibility_roles($account) {
$enabled = FALSE;
$roles = variable_get('googleanalytics_roles', array());
if (array_sum($roles) > 0) {
// One or more roles are selected for tracking.
foreach (array_keys($account->roles) as $rid) {
// Is the current user a member of one role enabled for tracking?
if (isset($roles[$rid]) && $rid == $roles[$rid]) {
// Current user is a member of a role that should be tracked.
$enabled = TRUE;
break;
}
}
}
else {
// No role is selected for tracking, therefor all roles should be tracked.
$enabled = TRUE;
}
return $enabled;
}
/**
* Based on visibility setting this function returns TRUE if GA code should
* be added to the current page and otherwise FALSE.
*/
function _googleanalytics_visibility_pages() {
$visibility = variable_get('googleanalytics_visibility', 0);
$pages = variable_get('googleanalytics_pages', '');
// Match path if necessary.
if (!empty($pages)) {
if ($visibility < 2) {
$path = drupal_get_path_alias($_GET['q']);
// Compare with the internal and path alias (if any).
$page_match = _googleanalytics_match_path($path, $pages);
if ($path != $_GET['q']) {
$page_match = $page_match || _googleanalytics_match_path($_GET['q'], $pages);
}
// When $visibility has a value of 0, the block is displayed on
// all pages except those listed in $pages. When set to 1, it
// is displayed only on those pages listed in $pages.
$page_match = !($visibility xor $page_match);
}
else {
$page_match = drupal_eval($pages);
}
}
else {
$page_match = TRUE;
}
return $page_match;
}
/**
* D6 backport of drupal_match_path().
*/
function _googleanalytics_match_path($path, $patterns) {
static $regexps;
if (!isset($regexps[$patterns])) {
$regexps[$patterns] = '/^('. preg_replace(array('/(\r\n?|\n)/', '/\\\\\*/', '/(^|\|)\\\\($|\|)/'), array('|', '.*', '\1'. preg_quote(variable_get('site_frontpage', 'node'), '/') .'\2'), preg_quote($patterns, '/')) .')$/';
}
return preg_match($regexps[$patterns], $path);
}
/**
* D6 backport orders core standard roles on top and translate core roles.
*/
function _googleanalytics_user_roles() {
// System roles take the first two positions.
$roles = array(
DRUPAL_ANONYMOUS_RID => NULL,
DRUPAL_AUTHENTICATED_RID => NULL,
);
$result = db_query('SELECT * FROM {role} ORDER BY name');
while ($role = db_fetch_object($result)) {
switch ($role->rid) {
// We only translate the built in role names
case DRUPAL_ANONYMOUS_RID:
$roles[$role->rid] = t($role->name);
break;
case DRUPAL_AUTHENTICATED_RID:
$roles[$role->rid] = t($role->name);
break;
default:
$roles[$role->rid] = $role->name;
}
}
// Filter to remove unmatched system roles.
return array_filter($roles);
}