function piwik_footer in Piwik Web Analytics 6
Same name and namespace in other branches
- 5 piwik.module \piwik_footer()
- 6.2 piwik.module \piwik_footer()
Implementation of hook_footer() to insert Javascript at the end of the page.
File
- ./
piwik.module, line 69 - Drupal Module: Piwik Adds the required Javascript to the bottom of all your Drupal pages to allow tracking by the Piwik statistics package.
Code
function piwik_footer($main = 0) {
global $user;
$id = variable_get('piwik_site_id', '');
if (!empty($id) && _piwik_visibility_pages() && _piwik_visibility_user($user)) {
$piwik_vars = array();
$url_custom = '';
// Site search tracking support.
if (module_exists('search') && variable_get('piwik_site_search', FALSE) && arg(0) == 'search' && ($keys = search_get_keys())) {
$url_custom = drupal_to_js(url('search', array(
'query' => 'query=' . $keys,
)));
}
// If this node is a translation of another node, pass the original
// node instead.
if (module_exists('translation') && variable_get('piwik_translation_set', 0)) {
// Check we have a node object, it supports translation, and its
// translated node ID (tnid) doesn't match its own node ID.
$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],
)));
}
}
// 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')) {
$url_custom = '"403/URL = " + encodeURIComponent(document.location.pathname + document.location.search) + "/From=" + encodeURIComponent(document.referrer)';
}
elseif (strstr($headers, '404 Not Found')) {
$url_custom = '"404/URL = " + encodeURIComponent(document.location.pathname + document.location.search) + "/From=" + encodeURIComponent(document.referrer)';
}
}
// Add any custom code snippets if specified.
$codesnippet_before = variable_get('piwik_codesnippet_before', '');
$codesnippet_after = variable_get('piwik_codesnippet_after', '');
// Build settings code. See http://piwik.org/docs/javascript-tracking/
$script = 'try {';
$script .= 'var piwikTracker = Piwik.getTracker(pkBaseURL + "/piwik.php", ' . drupal_to_js(variable_get('piwik_site_id', '')) . ');';
if (!empty($url_custom)) {
$script .= 'piwikTracker.setDocumentTitle(' . $url_custom . ');';
}
if (variable_get('piwik_track', 1) && !(variable_get('piwik_trackfiles_extensions', PK_TRACKFILES_EXTENSIONS) == PK_TRACKFILES_EXTENSIONS)) {
$script .= 'piwikTracker.setDownloadExtensions(' . drupal_to_js(variable_get('piwik_trackfiles_extensions', PK_TRACKFILES_EXTENSIONS)) . ');';
}
// Add custom variables.
if (!empty($piwik_vars)) {
$piwik_vars_fields = array();
foreach ($piwik_vars as $name => $value) {
$piwik_vars_fields[] = drupal_to_js($name) . ':' . drupal_to_js($value);
}
if (count($piwik_vars_fields) > 0) {
$script .= 'piwikTracker.setCustomVars({ ' . implode(', ', $piwik_vars_fields) . ' });';
}
}
if (!empty($codesnippet_before)) {
$script .= $codesnippet_before;
}
$script .= 'piwikTracker.trackPageView();';
// Add link tracking.
if (variable_get('piwik_track', 1)) {
$script .= 'piwikTracker.enableLinkTracking();';
}
if (!empty($codesnippet_after)) {
$script .= $codesnippet_after;
}
$script .= '} catch(err) {}';
drupal_add_js($script, 'inline', 'footer');
}
}