function googleanalytics_footer in Google Analytics 5
Same name and namespace in other branches
- 6 googleanalytics.module \googleanalytics_footer()
- 6.2 googleanalytics.module \googleanalytics_footer()
- 6.3 googleanalytics.module \googleanalytics_footer()
Implementation of hook_footer() to insert Javascript at the end of the page
File
- ./
googleanalytics.module, line 96
Code
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_pages() && _googleanalytics_visibility_user($user)) {
// Use the old version of Google Analytics?
$legacy_version = variable_get('googleanalytics_legacy_version', 0);
// Add User profile segmentation values
if (is_array($profile_fields = variable_get('googleanalytics_segmentation', '')) && $user->uid > 0) {
$p = 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=' . $keys));
}
// Track access denied (403) and file not found (404) pages.
if (function_exists('drupal_get_headers')) {
$headers = drupal_get_headers();
if (strstr($headers, '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, '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_before = variable_get('googleanalytics_codesnippet_before', '');
$codesnippet_after = variable_get('googleanalytics_codesnippet_after', '');
// Should the legacy code be used?
$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');
}
}