You are here

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

Same filename and directory in other branches
  1. 8 getclicky.module
  2. 5 getclicky.module
  3. 6 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.
 *
 * Lets you see stats (tables, charts) from within drupal :
 * see the GetClicky settings page.
 */

// define constants
// option 0 = dont track admin pages by default
define("GETCLICKY_ADMIN_PAGES_OPTIONS_DEFAULTS", 0);

// default rids for roles tracking
// set anonymous ( rid = 1 ) and authenticated ( rid  = 2 ) as selected
define("GETCLICKY_TRACK_ROLES_OPTIONS_DEFAULTS", serialize(array(
  1,
  2,
)));

/**
 * Implements hook_help().
 */
function getclicky_help($path, $arg) {
  switch ($path) {
    case 'admin/config/system/getclicky':
      return t('GetClicky is a free statistics package.');
  }
}

/**
 * Implements hook_menu().
 */
function getclicky_menu() {
  $items = array();
  $items['admin/config/system/getclicky'] = array(
    'title' => 'GetClicky Settings',
    'description' => 'Configure the settings used to generate your GetClicky tracking code.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'getclicky_admin_settings_form',
    ),
    'access arguments' => array(
      'administer site configuration',
    ),
    'file' => 'getclicky.admin.inc',
  );
  $items['admin/reports/getclicky-stats-dashboard'] = array(
    'title' => 'Getclicky Stats Dashboard',
    'description' => 'Show Stats Dashboard',
    'page callback' => '__getclicky_output_stats_dashboard',
    'access arguments' => array(
      'administer site configuration',
    ),
  );
  return $items;
}

/**
 * Implements hook_page_build().
 */
function getclicky_page_build(&$page) {
  global $user;

  // Don't track page views in the admin sections

  //@TODO: use path_get_admin_paths() instead of arg(0) comparison to admin.
  if (__getclicky_track_or_not($user)) {
    $site_id_number = trim(variable_get('getclicky_site_id_number', ''));

    // 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);
    }
    $script = __getclicky_output_js_code($site_id_number);

    // Optionally pass username data to Clicky by adding additional code
    // BEFORE the Clicky tracking code.
    if (variable_get('getclicky_pass_username', FALSE) && isset($user->name)) {
      $script = '<script type="text/javascript">
        var clicky_custom = {session: {username: "' . $user->name . '"}};
        </script>' . $script;
    }
    $page['page_bottom']['getclicky'] = array(
      '#type' => 'markup',
      '#markup' => $script,
    );
  }
}
function __getclicky_track_or_not_based_on_user($account) {

  // By default we don't track users.
  $track = FALSE;

  // role-based decision
  $roles = variable_get('getclicky_track_roles', unserialize(GETCLICKY_TRACK_ROLES_OPTIONS_DEFAULTS));
  foreach (array_keys($account->roles) as $role) {
    if (isset($roles[$role]) && ($roles[$role] != 0 && $roles[$role] != '0')) {
      $track = TRUE;
      break;
    }
  }
  return $track;
}
function __getclicky_track_or_not_based_on_page() {

  // By default we don't track users.
  $track = FALSE;

  // current page based decision
  $track_admin_pages = variable_get('getclicky_track_admin_pages', GETCLICKY_ADMIN_PAGES_OPTIONS_DEFAULTS);
  $is_admin = path_is_admin(current_path());
  if ($is_admin && $track_admin_pages == '1' || !$is_admin) {
    $track = TRUE;
  }

  // TODO: if admin settings filter gives true, track = true
  return $track;
}
function __getclicky_track_or_not_based_on_site_id_number_empty() {

  // By default we don't track users.
  $track = FALSE;

  // site__id_number != empty decision
  $site_id_number = trim(variable_get('getclicky_site_id_number', ''));
  if (!empty($site_id_number)) {
    $track = TRUE;
  }
  return $track;
}

/**
 * Decides whether track the user or not.
 *
 * @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_or_not($account) {

  // By default we don't track users.
  $track = FALSE;
  if (__getclicky_track_or_not_based_on_user($account) && __getclicky_track_or_not_based_on_page() && __getclicky_track_or_not_based_on_site_id_number_empty()) {
    $track = TRUE;
  }
  return $track;
}
function __getclicky_output_banner_html_code($site_id_number) {
  return '<a title="Real Time Web Analytics" href="http://clicky.com/' . $site_id_number . '"><img alt="Real Time Web Analytics" src="//static.getclicky.com/media/links/badge.gif" border="0" /></a>';
}

/**
 * Outputs getclicky code.
 */
function __getclicky_output_js_code($site_id_number) {
  $js_code = '';
  if (variable_get('getclicky_show_banner_image', FALSE)) {
    $js_code .= __getclicky_output_banner_html_code($site_id_number);
  }
  $js_code .= '
    <script src="//static.getclicky.com/js" type="text/javascript"></script>
    <script type="text/javascript">try { clicky.init(' . $site_id_number . '); }catch(e){}</script>
    <noscript><p><img alt="Clicky" width="1" height="1" src="//in.getclicky.com/' . $site_id_number . 'ns.gif" /></p></noscript>

';

  // Add any custom code snippets if specified
  $codesnippet = variable_get('getclicky_codesnippet', '');
  if (!empty($codesnippet)) {
    $js_code .= '<script type="text/javascript">';
    $js_code .= $codesnippet;
    $js_code .= '</script>';
  }
  return $js_code;
}

/**
 * Implements hook_block_info().
 */
function getclicky_block_info() {
  $blocks['getclicky_banner'] = array(
    'info' => t('Show GetClicky banner image'),
  );
  return $blocks;
}

/**
 * Implements hook_block_view().
 */
function getclicky_block_view($delta = '') {
  switch ($delta) {
    case "getclicky_banner":
      $block = array(
        'content' => variable_get('getclicky_banner_image_html_code_for_copying', '<a title="Clicky Web Analytics" href="http://getclicky.com/' . trim(variable_get('getclicky_site_id_number', '')) . '"><img alt="Clicky Web Analytics" src="http://static.getclicky.com/media/links/badge.gif" border="0" /></a>'),
      );
      break;
  }
  return $block;
}

/**
 * Insert an iframe with the getclicky stats for the current account.
 */
function __getclicky_output_stats_dashboard() {
  $site_id = trim(variable_get('getclicky_site_id_number', ''));
  $site_key = trim(variable_get('getclicky_site_key_number', ''));
  if ($site_id && $site_key) {

    // 1000x1300 seems to be a sweet spot for avoiding inner pane scrolling
    $iframe = '<iframe style="border: none; width: 1000px; height: 1300px;" src="http://getclicky.com/stats/wp-iframe?site_id=' . $site_id . '&sitekey=' . $site_key . '"></iframe>';
  }
  else {
    $iframe = t("You have not configured your GetClicky account, please !configure_link ...", array(
      '!configure_link',
      l('enter site id and sitekey', 'admin/config/system/getclicky'),
    ));
  }
  return $iframe;
}