You are here

analytics.module in Analytics 6

Same filename and directory in other branches
  1. 8 analytics.module
  2. 7 analytics.module

Provides an API for tracking anlytics about your Drupal site.

File

analytics.module
View source
<?php

/**
 * @file
 * Provides an API for tracking anlytics about your Drupal site.
 */

/**
 * Implementation of hook_autoload_info().
 */
function analytics_autoload_info() {
  return array(
    'Analytic' => array(
      'file' => 'includes/analytic.inc',
    ),
    'AnalyticData' => array(
      'file' => 'includes/analyticdata.inc',
    ),
  );
}

/**
 * Implementation of hook_perm().
 */
function analytics_perm() {
  return array(
    'administer analytics settings',
  );
}

/**
 * Implementation of hook_menu().
 */
function analytics_menu() {
  $items = array();
  $items['admin/settings/analytics'] = array(
    'title' => 'Analytics settings',
    'description' => 'Configure settings for capturing analytics',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'analytics_settings_form',
    ),
    'access arguments' => array(
      'administer analytics settings',
    ),
    'file' => 'analytics.admin.inc',
    'type' => MENU_NORMAL_ITEM,
  );
  $items['analytics/api/%'] = array(
    'title' => 'Analytics settings',
    'page callback' => 'analytics_js_callback',
    'page arguments' => array(
      2,
    ),
    'access callback' => TRUE,
    'type' => MENU_CALLBACK,
  );
  return $items;
}

/**
 * Implementation of hook_init().
 */
function analytics_init() {
  global $user;
  if (variable_get('analytics_js_enable', TRUE)) {
    $path = drupal_get_path('module', 'analytics');
    drupal_add_js($path . '/analytics.js', 'module', 'header');
  }
  if (variable_get('analytics_js_settings_enable', FALSE)) {
    $data['analytics'] = array(
      'user_id' => $user->uid,
      'user_name' => $user->name,
      'logged_in' => $user->uid ? TRUE : FALSE,
      'roles' => $user->roles,
    );
    drupal_add_js($data, 'setting', 'header');
  }
}

/**
 * Helper function for loading menu items.
 */
function analytics_analytic_load($event_id) {
  $events = Analytic::listEvents();
  if (in_array($event_id, array_keys($events))) {
    $event = new AnalyticData();
    $event
      ->setEventId($event_id);
    return $event;
  }
  return FALSE;
}
function analytics_js_callback($event_name) {
  if (empty($event_name) || empty($_POST['analytics_data'])) {
    watchdog('analytics', 'Poorly formed javascript request');
  }
  $data = unserialize($_POST['analytics_data']);
  $analytic = new Analytic();
  $analytic
    ->track($event_name, $data);
  exit;
}

Functions

Namesort descending Description
analytics_analytic_load Helper function for loading menu items.
analytics_autoload_info Implementation of hook_autoload_info().
analytics_init Implementation of hook_init().
analytics_js_callback
analytics_menu Implementation of hook_menu().
analytics_perm Implementation of hook_perm().