View source
<?php
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($path, $arg) {
switch ($path) {
case 'admin/settings/googleanalytics':
return t('<a href="@ga_url">Google Analytics</a> is a free statistics package based on the excellent Urchin system. This module provides services to better integrate Drupal with Google Analytics.', array(
'@ga_url' => 'http://www.google.com/analytics/',
));
}
}
function googleanalytics_perm() {
return array(
'administer google analytics',
'opt-in or out of tracking',
'use PHP for tracking visibility',
);
}
function googleanalytics_menu() {
$items['admin/settings/googleanalytics'] = array(
'title' => 'Google Analytics',
'description' => 'Configure the settings used to generate your Google Analytics tracking code.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'googleanalytics_admin_settings_form',
),
'access arguments' => array(
'administer google analytics',
),
'file' => 'googleanalytics.admin.inc',
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
function googleanalytics_init() {
global $user;
$id = variable_get('googleanalytics_account', '');
if (!empty($id) && _googleanalytics_visibility_pages() && _googleanalytics_visibility_user($user)) {
$legacy_version = variable_get('googleanalytics_legacy_version', FALSE);
$scope = variable_get('googleanalytics_js_scope', 'footer');
$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);
}
$link_settings = array();
if ($track_outgoing = variable_get('googleanalytics_trackoutgoing', 1)) {
$link_settings['trackOutgoing'] = $track_outgoing;
}
if ($track_mailto = variable_get('googleanalytics_trackmailto', 1)) {
$link_settings['trackMailto'] = $track_mailto;
}
if (($track_download = variable_get('googleanalytics_trackfiles', 1)) && ($trackfiles_extensions = variable_get('googleanalytics_trackfiles_extensions', GA_TRACKFILES_EXTENSIONS))) {
$link_settings['trackDownload'] = $track_download;
$link_settings['trackDownloadExtensions'] = $trackfiles_extensions;
}
if (!empty($link_settings)) {
$link_settings['LegacyVersion'] = $legacy_version ? 1 : 0;
drupal_add_js(array(
'googleanalytics' => $link_settings,
), 'setting', 'header');
drupal_add_js(drupal_get_path('module', 'googleanalytics') . '/googleanalytics.js', 'module', $scope);
}
if (variable_get('googleanalytics_trackadsense', FALSE)) {
drupal_add_js('window.google_analytics_uacct = ' . drupal_to_js($id) . ';', 'inline', 'header');
}
}
}
function googleanalytics_footer($main = 0) {
global $user;
$id = variable_get('googleanalytics_account', '');
if (!empty($id) && _googleanalytics_visibility_pages() && _googleanalytics_visibility_user($user)) {
$legacy_version = variable_get('googleanalytics_legacy_version', FALSE);
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;
}
$segmentation = '';
if (count($fields) > 0) {
if ($legacy_version) {
$segmentation = '__utmSetVar(' . drupal_to_js(implode(':', $fields)) . ');';
}
else {
$segmentation = 'pageTracker._setVar(' . drupal_to_js(implode(':', $fields)) . ');';
}
}
}
$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), array(
'query' => 'search=' . $keys,
)));
}
if (module_exists('translation') && variable_get('googleanalytics_translation_set', 0)) {
$node = menu_get_object();
if ($node && translation_supported_type($node->type) && isset($node->tnid) && $node->tnid != $node->nid) {
$source_node = node_load($node->tnid);
$languages = language_list();
$url_custom = drupal_to_js(url('node/' . $source_node->nid, array(
'language' => $languages[$source_node->language],
)));
}
}
if (function_exists('drupal_get_headers')) {
$headers = drupal_get_headers();
if (strstr($headers, 'HTTP/1.1 403 Forbidden')) {
if ($legacy_version) {
$url_custom = '"/403.html?page=" + _udl.pathname + _udl.search';
}
else {
$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';
}
}
}
$codesnippet_before = variable_get('googleanalytics_codesnippet_before', '');
$codesnippet_after = variable_get('googleanalytics_codesnippet_after', '');
$script = 'try{';
if ($legacy_version) {
$script .= '_uacct = ' . drupal_to_js($id) . ';';
if (!empty($segmentation)) {
$script .= $segmentation;
}
if (!empty($codesnippet_before)) {
$script .= $codesnippet_before;
}
$script .= 'urchinTracker(' . $url_custom . ');';
if (!empty($codesnippet_after)) {
$script .= $codesnippet_after;
}
}
else {
$script .= 'var pageTracker = _gat._getTracker(' . drupal_to_js($id) . ');';
if (!empty($segmentation)) {
$script .= $segmentation;
}
if (!empty($codesnippet_before)) {
$script .= $codesnippet_before;
}
$script .= 'pageTracker._trackPageview(' . $url_custom . ');';
if (!empty($codesnippet_after)) {
$script .= $codesnippet_after;
}
}
$script .= '} catch(err) {}';
drupal_add_js($script, 'inline', 'footer');
}
}
function googleanalytics_user($type, $edit, &$account, $category = NULL) {
switch ($type) {
case 'form':
if ($category == 'account' && user_access('opt-in or out of tracking') && ($custom = variable_get('googleanalytics_custom', 0)) != 0 && _googleanalytics_visibility_roles($account)) {
$form['googleanalytics'] = array(
'#type' => 'fieldset',
'#title' => t('Google Analytics configuration'),
'#weight' => 3,
'#collapsible' => TRUE,
'#tree' => TRUE,
);
switch ($custom) {
case 1:
$title = t('Disable user tracking');
$description = t('Users are tracked by default, but you are able to opt out.');
break;
case 2:
$title = t('Enable user tracking');
$description = t('Users are <em>not</em> tracked by default, but you are able to opt in.');
break;
}
$form['googleanalytics']['custom'] = array(
'#type' => 'checkbox',
'#title' => $title,
'#description' => $description,
'#default_value' => isset($account->googleanalytics['custom']) ? $account->googleanalytics['custom'] : $custom == 1,
);
return $form;
}
break;
}
}
function googleanalytics_requirements($phase) {
$requirements = array();
if ($phase == 'runtime') {
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 <a href="@url">Google Analytics settings page</a>.', array(
'@url' => url('admin/settings/googleanalytics'),
)),
'severity' => REQUIREMENT_ERROR,
'value' => t('Not configured'),
);
}
}
return $requirements;
}
function googleanalytics_cron() {
if (time() - variable_get('googleanalytics_last_cache', 0) >= 86400) {
file_delete(file_directory_path() . '/googleanalytics/urchin.js');
file_delete(file_directory_path() . '/googleanalytics/ga.js');
if (variable_get('preprocess_js', 0)) {
drupal_clear_js_cache();
}
variable_set('googleanalytics_last_cache', time());
}
}
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) {
if (file_check_directory($directory, FILE_CREATE_DIRECTORY)) {
return file_save_data($result->data, $directory . '/' . basename($location), FILE_EXISTS_REPLACE);
}
}
}
else {
return $file_destination;
}
}
function _googleanalytics_visibility_user($account) {
$enabled = FALSE;
if (_googleanalytics_visibility_roles($account)) {
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;
}
function _googleanalytics_visibility_roles($account) {
$enabled = FALSE;
$roles = variable_get('googleanalytics_roles', array());
if (array_sum($roles) > 0) {
foreach (array_keys($account->roles) as $rid) {
if (isset($roles[$rid]) && $rid == $roles[$rid]) {
$enabled = TRUE;
break;
}
}
}
else {
$enabled = TRUE;
}
return $enabled;
}
function _googleanalytics_visibility_pages() {
static $page_match;
if (!isset($page_match)) {
$visibility = variable_get('googleanalytics_visibility', 0);
$pages = variable_get('googleanalytics_pages', '');
if (!empty($pages)) {
if ($visibility < 2) {
$path = drupal_get_path_alias($_GET['q']);
$page_match = drupal_match_path($path, $pages);
if ($path != $_GET['q']) {
$page_match = $page_match || drupal_match_path($_GET['q'], $pages);
}
$page_match = !($visibility xor $page_match);
}
else {
$page_match = drupal_eval($pages);
}
}
else {
$page_match = TRUE;
}
}
return $page_match;
}