You are here

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

Same filename and directory in other branches
  1. 8 getclicky.module
  2. 5 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.
 *
 * Lets you see stats (tables, charts) from within drupal :
 * see the GetClicky settings page
 *
 * @author: rsvelko, gloscon
 */
define("GETCLICKY_FIELDS_INIT", serialize(array(
  'uid' => t('User ID (uid in mysql)'),
  'name' => t('Username (name)'),
  'roles' => t('User Roles (roles)'),
)));
define("GETCLICKY_FIELDS_TO_CLICKY_DEFAULT_MAPPINGS", serialize(array(
  'name' => 'username',
)));
function getclicky_help($path, $arg) {
  switch ($path) {
    case 'admin/settings/getclicky':
      return t('GetClicky is a free statistics package.');
  }
}

/**
 * Implementation of hook_perm().
 */
function getclicky_perm() {
  return array(
    'access clicky stats dashboard',
    'administer getclicky',
    'opt-in or out of getclicky tracking',
    'use PHP for tracking visibility',
  );
}

// TODO: write install hook for perms from 1.6 to here
// foreach roles as role) { if role has perm "administer site configuration" ) { give that role the clicky perm } }

/**
 * Implementation of hook_menu().
 */
function getclicky_menu() {
  $items = array();
  $items['admin/settings/getclicky'] = array(
    'title' => t('GetClicky'),
    'description' => t('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 getclicky',
    ),
    'file' => 'getclicky.admin.inc',
  );
  $items['admin/settings/getclicky/settings'] = array(
    'title' => 'Settings',
    'access arguments' => array(
      'administer getclicky',
    ),
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => -10,
  );
  $items['admin/settings/getclicky/clicky-stats-dashboard'] = array(
    'title' => 'Clicky Stats Dashboard',
    'description' => 'Shows the Clicky Stats Dashboard',
    'page callback' => 'drupal_goto',
    'page arguments' => array(
      'admin/reports/clicky-stats-dashboard',
    ),
    'access arguments' => array(
      'access clicky stats dashboard',
    ),
    'type' => MENU_LOCAL_TASK,
    'weight' => 10,
  );
  $items['admin/reports/clicky-stats-dashboard'] = array(
    'title' => 'Clicky Stats Dashboard',
    'description' => 'Shows the Clicky Stats Dashboard',
    'page callback' => '__getclicky_output_stats_dashboard',
    'access arguments' => array(
      'access clicky stats dashboard',
    ),
    'type' => MENU_LOCAL_TASK,
    'weight' => 10,
  );
  return $items;
}

/**
 * Implementation of hook_footer()  to insert Javascript at the end of the page
 */
function getclicky_footer($main = 0) {
  global $user;
  $user = user_load($user->uid);
  $site_id_number = variable_get('getclicky_site_id_number', '');
  $segmentation = '';
  $script = '';
  $arg = arg();
  $arg = implode('/', $arg);
  if ($site_id_number && __getclicky_should_we_track_path($arg) && __getclicky_should_we_track_user($user)) {

    // Add User profile segmentation values
    if (is_array($profile_fields = variable_get('getclicky_segmentation', '')) && $user->uid > 0) {

      // pass by ref in node_invoke NOT possible!
      //      $p_bool = module_invoke('profile', 'load_profile', &$user);

      //$fields = array();
      if (count($profile_fields) > 0) {
        $segmentation .= <<<HERE
<script type="text/javascript">
  var clicky_custom = {};
  clicky_custom.session = {

HERE;
        foreach ($profile_fields as $field_drupal_name => $title) {
          $field_value = $user->{$field_drupal_name};
          if (is_array($field_value)) {
            foreach ($field_value as $key => $value) {
              $field_value[$key] = "'" . $field_value[$key] . "'";
            }
            $field_value = "[" . implode(',', $field_value) . "]";
          }
          else {
            $field_value = "'" . $field_value . "'";
          }
          $mapping = unserialize(GETCLICKY_FIELDS_TO_CLICKY_DEFAULT_MAPPINGS);
          $field_js_name = !empty($mapping[$field_drupal_name]) ? $mapping[$field_drupal_name] : $field_drupal_name;
          $segmentation .= <<<HERE
    '{<span class="php-variable">$field_js_name</span>}' : {<span class="php-variable">$field_value</span>},

HERE;
        }

        // Only show segmentation variable if there are specified fields.
        $segmentation .= <<<HERE
  };
</script>
HERE;
      }
      $script .= $segmentation;
    }

    // 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);
    }

    //    print_r($url_custom);
    // todo add code for url custom
    $script .= __getclicky_output_js_code($site_id_number);

    //    print_r($script);
    return $script;
  }
}

// getclicky_trackfiles not used in the code

/**
 *
 * @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_should_we_track_user($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;
}
function __getclicky_should_we_track_path($arg) {
  return TRUE;
}
function __getclicky_output_banner_html_code($site_id_number) {
  return '<a title="Clicky Web Analytics" href="http://getclicky.com/' . $site_id_number . '"><img alt="Clicky Web Analytics" src="http://static.getclicky.com/media/links/badge.gif" border="0" /></a>';

  /*   TODO 3 badges

    <a title="Clicky Web Analytics" href="http://getclicky.com/157619"><img alt="Clicky Web Analytics" src="http://getclicky.com/media/links/clicky-125.gif" border="0" /></a>

    <a title="Clicky Web Analytics" href="http://getclicky.com/157619"><img alt="Clicky Web Analytics" src="http://getclicky.com/media/links/clicky-125-2.gif" border="0" /></a>

    <a title="Clicky Web Analytics" href="http://getclicky.com/157619"><img alt="Clicky Web Analytics" src="http://getclicky.com/media/links/badge.gif" border="0" /></a>
   */
}
function __getclicky_output_js_code_secure_or_unsecure($site_id_number, $secure) {
  if ($secure == 'secure') {
    $http_or_https = "https";
  }
  else {
    $http_or_https = "http";
  }
  $js_code = '
  <script src="' . $http_or_https . '://static.getclicky.com/js" type="text/javascript"></script>
  <script type="text/javascript"><!--//--><![CDATA[//><!--
  clicky.init(' . $site_id_number . ');
  //--><!]]></script>
  <noscript><p><img alt="Clicky" src="' . $http_or_https . '://static.getclicky.com/' . $site_id_number . 'ns.gif" /></p></noscript>';

  // Add any custom code snippets if specified
  $codesnippet = variable_get('getclicky_codesnippet', '');
  if ($codesnippet == "<br>" || $codesnippet == "<br />" || $codesnippet == "<p></p>" || preg_match('!^<p>.+</p>$!', $codesnippet)) {
    $codesnippet = "";
  }
  if (!empty($codesnippet)) {
    $js_code .= '<script type="text/javascript">';
    $js_code .= $codesnippet;
    $js_code .= '</script>';
  }
  return $js_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);
  }

  /*     '#options' => array(
  0      t('When onto a https page - do not inject any js code. I do not have a Pro account and/or I do not want to track https pages.'),
  1      t('When onto a https page - use the js code for secure pages. I have a Pro account.'),
  2      t('When onto a https page - use the js code for normal http pages. I do not have a Pro account and I do not mind the mixed-content-warning.'),
      ),
  */

  // Are we on a secure page?
  if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == "on") {
    $getclicky_secure_pages_tracking_option = variable_get('getclicky_secure_pages_tracking_option', 0);
    if ($getclicky_secure_pages_tracking_option == 0) {

      // do not inject
      $js_code .= '';
    }
    elseif ($getclicky_secure_pages_tracking_option == 1) {

      // https page and secure code
      $js_code .= __getclicky_output_js_code_secure_or_unsecure($site_id_number, 'secure');
    }
    elseif ($getclicky_secure_pages_tracking_option == 2) {

      // https page and unsecure code
      $js_code .= __getclicky_output_js_code_secure_or_unsecure($site_id_number, 'unsecure');
    }
  }
  else {

    // http page
    $js_code .= __getclicky_output_js_code_secure_or_unsecure($site_id_number, 'unsecure');
  }
  return $js_code;
}

/**
 * Implementation of hook_block()
 */
function getclicky_block($op = 'list', $delta = 0, $edit = array()) {
  if ($op == 'list') {
    $blocks['getclicky_banner'] = array(
      'info' => t('Show GetClicky banner image'),
      'weight' => 0,
      'status' => 0,
      //'region' => 'footer',
      'cache' => BLOCK_CACHE_GLOBAL,
    );
    return $blocks;
  }
  else {
    if ($op == 'view') {
      switch ($delta) {
        case "getclicky_banner":
          $block = array(
            'subject' => t(''),
            // empty on purpose
            'content' => variable_get('getclicky_banner_image_html_code_for_copying', '<a title="Clicky Web Analytics" href="http://getclicky.com/' . 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;
    }
  }
}
function __getclicky_output_stats_dashboard() {

  // todo: test if perms work as expected without site reports one
  if (user_access('access clicky stats dashboard')) {
    $site_id = variable_get('getclicky_site_id_number', '');
    $site_key = variable_get('getclicky_site_key_number', '');
    if ($site_id && $site_key) {
      $iframe = '<br /><iframe style="margin-left: 20px; width: 850px; height: 1000px;" src="http://getclicky.com/stats/wp-iframe?site_id=' . $site_id . '&sitekey=' . $site_key . '"></iframe>';
    }
    else {
      $iframe = "Empty. Please enter site id and sitekey ...";
    }
    return $iframe;
  }
}