You are here

function piwik_page_alter in Piwik Web Analytics 7

Same name and namespace in other branches
  1. 7.2 piwik.module \piwik_page_alter()

Implementation of hook_page_alter() to insert JavaScript to the appropriate scope/region of the page.

File

./piwik.module, line 64
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_page_alter(&$page) {
  global $user;
  $id = variable_get('piwik_site_id', '');

  // Get page status code for visibility filtering.
  $status = drupal_get_http_header('Status');
  $trackable_status_codes = array(
    '403 Forbidden',
    '404 Not Found',
  );

  // 1. Check if the piwik 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() || in_array($status, $trackable_status_codes)) && _piwik_visibility_user($user)) {
    $url_http = variable_get('piwik_url_http', '');
    $url_https = variable_get('piwik_url_https', '');
    $scope = variable_get('piwik_js_scope', 'footer');

    // Should a local cached copy of the tracking code be used?
    $piwik = 'var pkBaseURL = (("https:" == document.location.protocol) ? "' . check_url($url_https) . '" : "' . check_url($url_http) . '");';
    if (variable_get('piwik_cache', 0) && ($url = _piwik_cache($url_http . '/piwik.js'))) {
      drupal_add_js($url, array(
        'scope' => $scope,
      ));
    }
    else {
      $piwik .= 'document.write(unescape("%3Cscript src=\'" + pkBaseURL + "/piwik.js\' type=\'text/javascript\'%3E%3C/script%3E"));';
    }
    drupal_add_js($piwik, array(
      'scope' => $scope,
      'type' => 'inline',
    ));
    $piwik_vars = array();
    $url_custom = '';

    // Site search tracking support.
    if (module_exists('search') && variable_get('piwik_site_search', FALSE) && arg(0) == 'search' && ($keys = piwik_search_get_keys())) {
      $url_custom = drupal_json_encode(rawurldecode(url('search', array(
        'query' => array(
          '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_json_encode(url('node/' . $source_node->nid, array(
          'language' => $languages[$source_node->language],
        )));
      }
    }

    // Track access denied (403) and file not found (404) pages.
    if ($status == '403 Forbidden') {
      $url_custom = '"403/URL = " + encodeURIComponent(document.location.pathname + document.location.search) + "/From=" + encodeURIComponent(document.referrer)';
    }
    elseif ($status == '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_json_encode(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_json_encode(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_json_encode($name) . ':' . drupal_json_encode($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) {}';

    // Add tracker code to scope.
    drupal_add_js($script, array(
      'scope' => $scope,
      'type' => 'inline',
    ));
  }
}