You are here

function piwik_footer in Piwik Web Analytics 5

Same name and namespace in other branches
  1. 6.2 piwik.module \piwik_footer()
  2. 6 piwik.module \piwik_footer()

Implementation of hook_footer() to insert Javascript at the end of the page

File

./piwik.module, line 71

Code

function piwik_footer($main = 0) {
  global $user;
  $id = variable_get('piwik_site_id', '');

  // 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) && _piwik_visibility_pages() && _piwik_visibility_user($user)) {
    $piwik_vars = array();
    $url_custom = '';
    if (module_exists('search') && variable_get('piwik_site_search', FALSE) && arg(0) == 'search' && ($keys = search_get_keys())) {
      $url_custom = drupal_to_js(url('search', 'query=' . $keys));
    }

    // Track access denied (403) and file not found (404) pages.
    if (function_exists('drupal_get_headers')) {
      $headers = drupal_get_headers();
      if (strstr($headers, 'HTTP/1.1 403 Forbidden')) {
        $url_custom = '"/403/URL=" + document.location.pathname + document.location.search + "/From=" + document.referrer';
      }
      elseif (strstr($headers, 'HTTP/1.1 404 Not Found')) {
        $url_custom = '"/404/URL=" + document.location.pathname + document.location.search + "/From=" + 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');
  }
}