You are here

function piwik_init in Piwik Web Analytics 6

Same name and namespace in other branches
  1. 6.2 piwik.module \piwik_init()

File

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

  // 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() && _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 piwik.js be used?
    $script = 'var pkBaseURL = (("https:" == document.location.protocol) ? "' . check_url($url_https) . '" : "' . check_url($url_http) . '");';
    if (variable_get('piwik_cache', 0) && variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC) == FILE_DOWNLOADS_PUBLIC && ($url = _piwik_cache($url_http . '/piwik.js'))) {
      drupal_add_js($url, 'module', $scope);
    }
    else {
      $script .= 'document.write(unescape("%3Cscript src=\'" + pkBaseURL + "/piwik.js\' type=\'text/javascript\'%3E%3C/script%3E"));';
    }
    drupal_add_js($script, 'inline', $scope);
  }
}