| 1 |
<?php
|
| 2 |
// $Id: piwik.module,v 1.1.2.2 2008/05/12 15:40:33 hass Exp $
|
| 3 |
|
| 4 |
/*
|
| 5 |
* Drupal Module: Piwik
|
| 6 |
* Adds the required Javascript to the bottom of all your Drupal pages
|
| 7 |
* to allow tracking by the Piwik statistics package.
|
| 8 |
*
|
| 9 |
* @author: Alexander Hass <www.hass.de>
|
| 10 |
*/
|
| 11 |
|
| 12 |
define('PK_TRACKFILES_EXTENSIONS', '7z|aac|avi|csv|doc|exe|flv|gif|gz|jpe?g|js|mp(3|4|e?g)|mov|pdf|phps|png|ppt|rar|sit|tar|torrent|txt|wma|wmv|xls|xml|zip');
|
| 13 |
|
| 14 |
function piwik_help($path, $arg) {
|
| 15 |
switch ($path) {
|
| 16 |
case 'admin/settings/piwik':
|
| 17 |
return t('<a href="@pk_url">Piwik - Web analytics</a> is an open source (GPL license) web analytics software. It gives interesting reports on your website visitors, your popular pages, the search engines keywords they used, the language they speak... and so much more. Piwik aims to be an open source alternative to <a href="@ga_url">Google Analytics</a>.', array('@pk_url' => 'http://www.piwik.org/', '@ga_url' => 'http://www.google.com/analytics/'));
|
| 18 |
}
|
| 19 |
}
|
| 20 |
|
| 21 |
function piwik_perm() {
|
| 22 |
return array('administer piwik', 'administer own tracking configuration', 'use PHP for tracking visibility');
|
| 23 |
}
|
| 24 |
|
| 25 |
function piwik_menu() {
|
| 26 |
$items['admin/settings/piwik'] = array(
|
| 27 |
'title' => 'Piwik',
|
| 28 |
'description' => 'Configure the settings used to generate your Piwik tracking code.',
|
| 29 |
'page callback' => 'drupal_get_form',
|
| 30 |
'page arguments' => array('piwik_admin_settings_form'),
|
| 31 |
'access arguments' => array('administer piwik'),
|
| 32 |
'file' => 'piwik.admin.inc',
|
| 33 |
'type' => MENU_NORMAL_ITEM,
|
| 34 |
);
|
| 35 |
|
| 36 |
return $items;
|
| 37 |
}
|
| 38 |
|
| 39 |
function piwik_init() {
|
| 40 |
global $user;
|
| 41 |
|
| 42 |
$id = variable_get('piwik_idsite', '');
|
| 43 |
|
| 44 |
// 1. Check if the piwik account number has a value.
|
| 45 |
// 2. Track page views based on visibility value.
|
| 46 |
// 3. Check if we should track the currently active user's role.
|
| 47 |
if (!empty($id) && _piwik_visibility_pages() && _piwik_visibility_user($user)) {
|
| 48 |
|
| 49 |
$url_http = variable_get('piwik_url_http', '');
|
| 50 |
$url_https = variable_get('piwik_url_https', '');
|
| 51 |
$scope = variable_get('piwik_js_scope', 'footer');
|
| 52 |
|
| 53 |
// Should a local cached copy of piwik.js be used?
|
| 54 |
if (variable_get('piwik_cache', 0) && (variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC) == FILE_DOWNLOADS_PUBLIC) && $source = _piwik_cache($url_http)) {
|
| 55 |
drupal_add_js($source, 'module', $scope);
|
| 56 |
}
|
| 57 |
else {
|
| 58 |
$script = 'var pkBaseURL = (("https:" == document.location.protocol) ? "'. check_url($url_https) .'" : "'. check_url($url_http) .'");';
|
| 59 |
$script .= 'document.write(unescape("%3Cscript src=\'" + pkBaseURL + "piwik.js\' type=\'text/javascript\'%3E%3C/script%3E"));';
|
| 60 |
drupal_add_js($script, 'inline', $scope);
|
| 61 |
}
|
| 62 |
|
| 63 |
}
|
| 64 |
}
|
| 65 |
|
| 66 |
/**
|
| 67 |
* Implementation of hook_footer() to insert Javascript at the end of the page.
|
| 68 |
*/
|
| 69 |
function piwik_footer($main = 0) {
|
| 70 |
global $user;
|
| 71 |
|
| 72 |
$id = variable_get('piwik_idsite', '');
|
| 73 |
|
| 74 |
if (!empty($id) && _piwik_visibility_pages() && _piwik_visibility_user($user)) {
|
| 75 |
|
| 76 |
$piwik_vars = array();
|
| 77 |
|
| 78 |
/* TODO: For future use.
|
| 79 |
// Add User profile segmentation values.
|
| 80 |
if (is_array($profile_fields = variable_get('piwik_segmentation', '')) && ($user->uid > 0)) {
|
| 81 |
|
| 82 |
$p = module_invoke('profile', 'load_profile', $user);
|
| 83 |
|
| 84 |
$fields = array();
|
| 85 |
foreach ($profile_fields as $field => $title) {
|
| 86 |
$value = $user->$field;
|
| 87 |
|
| 88 |
if (is_array($value)) {
|
| 89 |
$value = implode(',', $value);
|
| 90 |
}
|
| 91 |
|
| 92 |
$fields[$field] = $value;
|
| 93 |
}
|
| 94 |
|
| 95 |
// Only show segmentation variable if there are specified fields.
|
| 96 |
if (count($fields) > 0) {
|
| 97 |
$piwik_vars['user_segmentation'] = implode(':', $fields);
|
| 98 |
}
|
| 99 |
}
|
| 100 |
|
| 101 |
// Site search tracking support.
|
| 102 |
if (module_exists('search') && variable_get('piwik_site_search', FALSE) && arg(0) == 'search') {
|
| 103 |
$keys = search_get_keys();
|
| 104 |
$piwik_vars['search_type'] = arg(1);
|
| 105 |
$piwik_vars['search_keys'] = trim($keys);
|
| 106 |
}
|
| 107 |
|
| 108 |
// Track access denied (403) and file not found (404) pages.
|
| 109 |
if (function_exists('drupal_get_headers')) {
|
| 110 |
$headers = drupal_get_headers();
|
| 111 |
if (strstr($headers, 'HTTP/1.1 403 Forbidden')) {
|
| 112 |
$piwik_vars['status_code'] = '403';
|
| 113 |
}
|
| 114 |
elseif (strstr($headers, 'HTTP/1.1 404 Not Found')) {
|
| 115 |
$piwik_vars['status_code'] = '404';
|
| 116 |
}
|
| 117 |
}
|
| 118 |
*/
|
| 119 |
|
| 120 |
// Add any custom code snippets if specified.
|
| 121 |
$codesnippet = variable_get('piwik_codesnippet', '');
|
| 122 |
|
| 123 |
// Build settings code.
|
| 124 |
$script = '';
|
| 125 |
$script .= 'piwik_action_name = document.title;';
|
| 126 |
$script .= 'piwik_idsite = '. drupal_to_js(variable_get('piwik_idsite', '')) .';';
|
| 127 |
$script .= 'piwik_url = pkBaseURL + "piwik.php";';
|
| 128 |
if (!empty($piwik_vars)) {
|
| 129 |
$piwik_vars_fields = array();
|
| 130 |
foreach ($piwik_vars as $name => $value) {
|
| 131 |
$piwik_vars_fields[] = drupal_to_js($name) .':'. drupal_to_js($value);
|
| 132 |
}
|
| 133 |
$script .= 'piwik_vars = { '. implode(', ', $piwik_vars_fields) .' };';
|
| 134 |
}
|
| 135 |
else {
|
| 136 |
$script .= 'piwik_vars = "";';
|
| 137 |
}
|
| 138 |
if (!empty($codesnippet)) {
|
| 139 |
$script .= $codesnippet;
|
| 140 |
}
|
| 141 |
$script .= 'piwik_log(piwik_action_name, piwik_idsite, piwik_url, piwik_vars);';
|
| 142 |
|
| 143 |
drupal_add_js($script, 'inline', 'footer');
|
| 144 |
}
|
| 145 |
}
|
| 146 |
|
| 147 |
/**
|
| 148 |
* Implementation of hook_user().
|
| 149 |
*
|
| 150 |
* Allow users to decide if tracking code will be added to pages or not.
|
| 151 |
*/
|
| 152 |
function piwik_user($type, $edit, &$account, $category = NULL) {
|
| 153 |
switch ($type) {
|
| 154 |
case 'form':
|
| 155 |
if ($category == 'account' && user_access('administer own tracking configuration') && ($custom = variable_get('piwik_custom', 0)) != 0 && _piwik_visibility_roles($account)) {
|
| 156 |
$form['piwik'] = array(
|
| 157 |
'#type' => 'fieldset',
|
| 158 |
'#title' => t('Piwik configuration'),
|
| 159 |
'#weight' => 3,
|
| 160 |
'#collapsible' => TRUE,
|
| 161 |
'#tree' => TRUE
|
| 162 |
);
|
| 163 |
$form['piwik']['custom'] = array(
|
| 164 |
'#type' => 'checkbox',
|
| 165 |
'#title' => t('User tracking'),
|
| 166 |
'#default_value' => isset($account->piwik['custom']) ? $account->piwik['custom'] : ($custom == 1)
|
| 167 |
);
|
| 168 |
|
| 169 |
return $form;
|
| 170 |
}
|
| 171 |
break;
|
| 172 |
|
| 173 |
}
|
| 174 |
}
|
| 175 |
|
| 176 |
/**
|
| 177 |
* Implementation of hook_requirements().
|
| 178 |
*/
|
| 179 |
function piwik_requirements($phase) {
|
| 180 |
$requirements = array();
|
| 181 |
|
| 182 |
if ($phase == 'runtime') {
|
| 183 |
// Raise warning if Piwik user account has not been set yet.
|
| 184 |
if (!preg_match('/^\d{1,}$/', variable_get('piwik_account', ''))) {
|
| 185 |
$requirements['piwik'] = array(
|
| 186 |
'title' => t('Piwik module'),
|
| 187 |
'description' => t('Piwik module has not been configured yet. Please configure its settings from the <a href="@url">Piwik settings page</a>.', array('@url' => url('admin/settings/piwik'))),
|
| 188 |
'severity' => REQUIREMENT_ERROR,
|
| 189 |
'value' => t('Not configured'),
|
| 190 |
);
|
| 191 |
}
|
| 192 |
}
|
| 193 |
|
| 194 |
return $requirements;
|
| 195 |
}
|
| 196 |
|
| 197 |
/**
|
| 198 |
* Implementation of hook_cron().
|
| 199 |
*/
|
| 200 |
function piwik_cron() {
|
| 201 |
// Regenerate the piwik.js every day.
|
| 202 |
if (time() - variable_get('piwik_last_cache', 0) >= 86400) {
|
| 203 |
file_delete(file_directory_path() .'/piwik/piwik.js');
|
| 204 |
|
| 205 |
// Clear aggregated JS files.
|
| 206 |
if (variable_get('preprocess_js', 0)) {
|
| 207 |
drupal_clear_js_cache();
|
| 208 |
}
|
| 209 |
|
| 210 |
variable_set('piwik_last_cache', time());
|
| 211 |
}
|
| 212 |
}
|
| 213 |
|
| 214 |
/**
|
| 215 |
* Download and cache the piwik.js file locally.
|
| 216 |
*
|
| 217 |
* @param $location
|
| 218 |
* The full URL to the external javascript file.
|
| 219 |
* @return mixed
|
| 220 |
* The path to the local javascript file on success, boolean FALSE on failure.
|
| 221 |
*/
|
| 222 |
function _piwik_cache($location) {
|
| 223 |
$directory = file_directory_path() .'/piwik';
|
| 224 |
$file_destination = $directory .'/'. basename($location);
|
| 225 |
if (!file_exists($file_destination)) {
|
| 226 |
$result = drupal_http_request($location);
|
| 227 |
if ($result->code == 200) {
|
| 228 |
// Check that the files directory is writable
|
| 229 |
if (file_check_directory($directory, FILE_CREATE_DIRECTORY)) {
|
| 230 |
return file_save_data($result->data, $directory .'/'. basename($location), FILE_EXISTS_REPLACE);
|
| 231 |
}
|
| 232 |
}
|
| 233 |
}
|
| 234 |
else {
|
| 235 |
return $file_destination;
|
| 236 |
}
|
| 237 |
}
|
| 238 |
|
| 239 |
/**
|
| 240 |
* Tracking visibility check for an user object.
|
| 241 |
*
|
| 242 |
* @param $account
|
| 243 |
* A user object containing an array of roles to check.
|
| 244 |
* @return boolean
|
| 245 |
* A decision on if the current user is being tracked by Piwik.
|
| 246 |
*/
|
| 247 |
function _piwik_visibility_user($account) {
|
| 248 |
|
| 249 |
$enabled = FALSE;
|
| 250 |
|
| 251 |
// Is current user a member of a role that should be tracked?
|
| 252 |
if (_piwik_visibility_roles($account)) {
|
| 253 |
|
| 254 |
// Use the user's block visibility setting, if necessary.
|
| 255 |
if (($custom = variable_get('piwik_custom', 0)) != 0) {
|
| 256 |
if ($account->uid && isset($account->piwik['custom'])) {
|
| 257 |
$enabled = $account->piwik['custom'];
|
| 258 |
}
|
| 259 |
else {
|
| 260 |
$enabled = ($custom == 1);
|
| 261 |
}
|
| 262 |
}
|
| 263 |
else {
|
| 264 |
$enabled = TRUE;
|
| 265 |
}
|
| 266 |
|
| 267 |
}
|
| 268 |
|
| 269 |
return $enabled;
|
| 270 |
}
|
| 271 |
|
| 272 |
/**
|
| 273 |
* Based on visibility setting this function returns TRUE if GA code should
|
| 274 |
* be added for the current role and otherwise FALSE.
|
| 275 |
*/
|
| 276 |
function _piwik_visibility_roles($account) {
|
| 277 |
|
| 278 |
$enabled = FALSE;
|
| 279 |
$roles = variable_get('piwik_roles', array());
|
| 280 |
|
| 281 |
if (array_sum($roles) > 0) {
|
| 282 |
// One or more roles are selected for tracking.
|
| 283 |
foreach (array_keys($account->roles) as $rid) {
|
| 284 |
// Is the current user a member of one role enabled for tracking?
|
| 285 |
if (isset($roles[$rid]) && $rid == $roles[$rid]) {
|
| 286 |
// Current user is a member of a role that should be tracked.
|
| 287 |
$enabled = TRUE;
|
| 288 |
break;
|
| 289 |
}
|
| 290 |
}
|
| 291 |
}
|
| 292 |
else {
|
| 293 |
// No role is selected for tracking, therefor all roles should be tracked.
|
| 294 |
$enabled = TRUE;
|
| 295 |
}
|
| 296 |
|
| 297 |
return $enabled;
|
| 298 |
}
|
| 299 |
|
| 300 |
/**
|
| 301 |
* Based on visibility setting this function returns TRUE if GA code should
|
| 302 |
* be added to the current page and otherwise FALSE.
|
| 303 |
*/
|
| 304 |
function _piwik_visibility_pages() {
|
| 305 |
static $page_match;
|
| 306 |
|
| 307 |
// Cache visibility setting in hook_init for hook_footer.
|
| 308 |
if (!isset($page_match)) {
|
| 309 |
|
| 310 |
$visibility = variable_get('piwik_visibility', 0);
|
| 311 |
$pages = variable_get('piwik_pages', '');
|
| 312 |
|
| 313 |
// Match path if necessary.
|
| 314 |
if (!empty($pages)) {
|
| 315 |
if ($visibility < 2) {
|
| 316 |
$path = drupal_get_path_alias($_GET['q']);
|
| 317 |
// Compare with the internal and path alias (if any).
|
| 318 |
$page_match = drupal_match_path($path, $pages);
|
| 319 |
if ($path != $_GET['q']) {
|
| 320 |
$page_match = $page_match || drupal_match_path($_GET['q'], $pages);
|
| 321 |
}
|
| 322 |
// When $visibility has a value of 0, the block is displayed on
|
| 323 |
// all pages except those listed in $pages. When set to 1, it
|
| 324 |
// is displayed only on those pages listed in $pages.
|
| 325 |
$page_match = !($visibility xor $page_match);
|
| 326 |
}
|
| 327 |
else {
|
| 328 |
$page_match = drupal_eval($pages);
|
| 329 |
}
|
| 330 |
}
|
| 331 |
else {
|
| 332 |
$page_match = TRUE;
|
| 333 |
}
|
| 334 |
|
| 335 |
}
|
| 336 |
return $page_match;
|
| 337 |
}
|