You are here

getclicky.module in Clicky - Web Analytics in Real Time 5

Same filename and directory in other branches
  1. 8 getclicky.module
  2. 6 getclicky.module
  3. 7 getclicky.module

File

getclicky.module
View source
<?php

/*
 * Drupal Module: getclicky
 * Adds the required Javascript to the bottom of all your Drupal pages
 * to allow tracking by the getclicky statistics package.
 *
 * @author: gloscon
 */
define('GC_TRACKFILES', 'pdf|zip|mp3');
function getclicky_help($section) {
  switch ($section) {
    case 'admin/settings/getclicky':
      return t('getClicky is a free statistics package .');
  }
}
function getclicky_menu($maycache) {
  $items = array();
  if ($maycache) {
    $items[] = array(
      'path' => 'admin/settings/getclicky',
      'title' => t('Get Clicky'),
      'description' => t('Configure the settings used to generate your Get Clicky tracking code.'),
      'callback' => 'drupal_get_form',
      'callback arguments' => 'getclicky_admin_settings',
      'access' => user_access('administer site configuration'),
      'type' => MENU_NORMAL_ITEM,
    );
  }
  return $items;
}

/**
 * Implementation of hook_footer()  to insert Javascript at the end of the page
 */
function getclicky_footer($main = 0) {
  global $user;
  $id = variable_get('getclicky_account', '');

  // Check if we should track the currently active user's role
  $track = _getclicky_track($user);

  // Don't track page views in the admin sections
  if ($id && arg(0) != 'admin' && $track == TRUE) {
    $prefix = '://www';

    // Are we on a secure page?
    if (isset($_SERVER['HTTPS']) && !empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) != 'off') {
      $prefix = 's://ssl';
    }

    // Add User profile segmentation values
    if (is_array($profile_fields = variable_get('getclicky_segmentation', '')) && $user->uid > 0) {
      $p = module_invoke('profile', 'load_profile', $user);
      $fields = array();
      foreach ($profile_fields as $field => $title) {
        $value = $user->{$field};
        if (is_array($value)) {
          $value = implode(',', $value);
        }
        $fields[$field] = $value;
      }

      // Only show segmentation variable if there are specified fields.
      $segmentation = '';
      if (count($fields) > 0) {
        $segmentation = drupal_to_js(implode(':', $fields));
      }
    }

    // Download tracking
    $path = drupal_get_path('module', 'getclicky');

    // Add any custom code snippets if specified
    $codesnippet = variable_get('getclicky_codesnippet', '');

    // Site search tracking support.
    $url_custom = '';
    if (module_exists('search') && variable_get('getclicky_site_search', FALSE) && arg(0) == 'search') {
      $keys = search_get_keys();
      $url_custom = url('search/' . arg(1), 'search=' . trim($keys));
    }

    // Surround custom urls with single quotes.
    if (!empty($url_custom)) {
      $url_custom = drupal_to_js($url_custom);
    }
    $getclicky_account = variable_get('getclicky_account', '');
    $getclicky_db = variable_get('getclicky_db', '');
    if (variable_get('getclicky_image', FALSE)) {
      $script = '<a title="Clicky Web Analytics" href="http://getclicky.com/' . $getclicky_account . '"><img alt="Clicky Web Analytics" src="http://static.getclicky.com/media/links/badge.gif" border="0" /></a>';
      $script .= '<script src="http://static.getclicky.com/' . $getclicky_account . '.js" type="text/javascript"></script>';
      $script .= '<noscript><img alt="Clicky" src="http://in.getclicky.com/' . $getclicky_account . '-db4.gif" /></noscript>';
      $script .= '<script type="text/javascript">';
      if (!empty($codesnippet)) {
        $script .= $codesnippet;
      }
      $script .= '</script>';
    }
    else {
      $script = '<script src="http://static.getclicky.com/' . $getclicky_account . '.js" type="text/javascript"></script>';
      $script .= '<noscript><img alt="Clicky" src="http://in.getclicky.com/' . $getclicky_account . '-db4.gif" /></noscript>';
      $script .= '<script type="text/javascript">';
      if (!empty($codesnippet)) {
        $script .= $codesnippet;
      }
      $script .= '</script>';
    }
    return $script;
  }
}

/**
 *
 * @param $account
 *   A user object containing an array of roles to check
 * @return boolean
 *   A decision on if the current user is being tracked by getClicky
 */
function _getclicky_track($account) {

  // By default we don't track users.
  $track = FALSE;
  foreach (array_keys($account->roles) as $role) {

    // Add the tracking code if user is member of one role that should be tracked.
    if (variable_get('getclicky_track_' . $role, FALSE)) {
      $track = TRUE;
    }
  }

  // Handle behavior for administrative user 1.
  if ($account->uid == 1 && variable_get('getclicky_track__user1', FALSE)) {

    // Enable tracking of user 1 if tracking for "authenticated user" is disabled.
    $track = TRUE;
  }
  elseif ($account->uid == 1 && !variable_get('getclicky_track__user1', FALSE)) {

    // User 1 is a member of "authenticated user". Disable user tracking
    // if user 1 shouldn't, but "authenticated user" should be tracked.
    $track = FALSE;
  }
  return $track;
}

/**
 * Implementation of hook_admin_settings() for configuring the module
 */
function getclicky_admin_settings() {
  $form['account'] = array(
    '#type' => 'fieldset',
    '#title' => t('Get Clicky Account Settings'),
    '#collapsible' => FALSE,
  );
  $form['account']['getclicky_account'] = array(
    '#type' => 'textfield',
    '#title' => t('User ID'),
    '#default_value' => variable_get('getclicky_account', ''),
    '#size' => 15,
    '#maxlength' => 20,
    '#required' => TRUE,
    '#description' => t('The user account is unique to the websites domain. You can obtain a user account from the <a href="@url">Get Clicky</a> website.', array(
      '@url' => ' http://getclicky.com/',
    )),
  );

  // Render the role overview.
  $result = db_query('SELECT * FROM {role} ORDER BY name');
  $form['roles'] = array(
    '#type' => 'fieldset',
    '#title' => t('User Role Tracking'),
    '#collapsible' => TRUE,
    '#description' => t('Define what user roles should be tracked by Get Clicky. <strong>Note:</strong> Drupal Admin pages are never tracked.'),
  );
  $form['roles']['getclicky_track__user1'] = array(
    '#type' => 'checkbox',
    '#title' => t('Admin (user 1)'),
    '#default_value' => variable_get('getclicky_track__user1', FALSE),
  );
  while ($role = db_fetch_object($result)) {
    $form['roles']['getclicky_track_' . $role->rid] = array(
      '#type' => 'checkbox',
      '#title' => check_plain($role->name),
      '#default_value' => variable_get('getclicky_track_' . $role->rid, FALSE),
    );
  }
  $form['getimg'] = array(
    '#type' => 'fieldset',
    '#title' => t('Enable Image'),
    '#collapsible' => TRUE,
    '#description' => t(''),
  );
  $form['getimg']['getclicky_image'] = array(
    '#type' => 'checkbox',
    '#title' => t('GetClicky Image'),
    '#default_value' => variable_get('getclicky_image', FALSE),
  );
  $form['segmentation'] = array(
    '#type' => 'fieldset',
    '#title' => t('User Segmentation'),
    '#collapsible' => TRUE,
    '#description' => t('If your users have profile fields completed, you can track your logged in users based on a defined profile field.'),
  );
  if (!module_exists('profile')) {
    $form['segmentation']['profile'] = array(
      '#type' => 'markup',
      '#value' => t('You need to activate the !profile to use this feature.', array(
        '!profile' => l(t('Profile module'), 'admin/build/modules'),
      )),
      '#prefix' => '<p>',
      '#suffix' => '</p>',
    );
  }
  else {

    // Compile a list of fields to show.
    $fields = array(
      'uid' => t('User ID'),
      'name' => t('Username'),
      'roles' => t('User Roles'),
    );
    $result = db_query('SELECT name, title, type, weight FROM {profile_fields} ORDER BY weight');
    while ($record = db_fetch_object($result)) {
      $fields[$record->name] = $record->title;
    }
    $form['segmentation']['getclicky_segmentation'] = array(
      '#type' => 'select',
      '#title' => t('Track'),
      '#description' => t('Selecting one or more values allows you to track users by profile values rather than simply an IP address. To select multiple items, hold down CTRL whilst selecting fields.'),
      '#default_value' => variable_get('getclicky_segmentation', ''),
      '#options' => $fields,
      '#size' => 10,
      '#multiple' => TRUE,
    );
  }
  $form['getclicky_trackfiles'] = array(
    '#type' => 'textfield',
    '#title' => t('File Extensions To Track'),
    '#default_value' => variable_get('getclicky_trackfiles', GC_TRACKFILES),
    '#description' => t('A pipe seperated list of file extensions that should be tracked when clicked. Example !extensions', array(
      '!extensions' => GC_TRACKFILES,
    )),
  );
  $form['advanced'] = array(
    '#type' => 'fieldset',
    '#title' => t('Advanced'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#description' => t('You can add custom Get Clicky code here.'),
  );
  $form['advanced']['getclicky_codesnippet'] = array(
    '#type' => 'textarea',
    '#title' => t('JavaScript Code'),
    '#default_value' => variable_get('getclicky_codesnippet', ''),
    '#rows' => 15,
    '#description' => t('Paste <a href="@snippets">custom code snippets here</a>. These will be added to every page that Get Clicky appears on. For help with this feature see the <a href="@blog">cutroni.com blog</a>. <strong>Do not include the &lt;script&gt; tags</strong>, and always end your code with a semicolon (;).', array(
      '@snippets' => 'http://drupal.org/node/39282',
      '@blog' => 'http://cutroni.com/blog/',
    )),
  );
  return system_settings_form($form);
}

Functions

Namesort descending Description
getclicky_admin_settings Implementation of hook_admin_settings() for configuring the module
getclicky_footer Implementation of hook_footer() to insert Javascript at the end of the page
getclicky_help
getclicky_menu
_getclicky_track

Constants

Namesort descending Description
GC_TRACKFILES