You are here

shurly_analytics.module in ShURLy 7

Same filename and directory in other branches
  1. 8 shurly_analytics/shurly_analytics.module

Analytics ShURLy functionalities.

File

shurly_analytics/shurly_analytics.module
View source
<?php

/**
 * @file
 *  Analytics ShURLy functionalities.
 */

/**
 * Implements hook_menu().
 */
function shurly_analytics_menu() {
  $items = array();
  $items['admin/config/system/shurly/analytics'] = array(
    'title' => 'ShURLy Analytics',
    'description' => 'Configure ShURLy Analytics',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'shurly_analytics_settings_form',
    ),
    'access arguments' => array(
      'Configure shurly analytics',
    ),
  );
  return $items;
}

/**
 * Implements hook_permission().
 */
function shurly_analytics_permission() {
  return array(
    'Configure shurly analytics' => array(
      'title' => t('Configure ShURLy Analytics'),
    ),
  );
}

/**
 * Implements hook_settings_form().
 */
function shurly_analytics_settings_form($form, &$form_state) {
  global $base_url;
  $google_analytics = variable_get('googleanalytics_account', 'UA-');
  $form['shurlyanalytics_account'] = array(
    '#title' => t('Enter you Identifiant (ID) Web Property'),
    '#type' => 'textfield',
    '#default_value' => variable_get('shurlyanalytics_account', $google_analytics),
    '#size' => 15,
    '#maxlength' => 20,
    '#required' => TRUE,
  );
  return system_settings_form($form);
}
function shurly_analytics_boot() {
  return;
}

/**
 * Implements shurly_redirect_after().
 */
function shurly_analytics_shurly_redirect_after($row) {
  if (variable_get('shurlyanalytics_account')) {
    $ga_id = variable_get('shurlyanalytics_account');
    $cid = _shurly_analytics_cookies();
    $path = "/" . arg(0);
    $destination = $row->destination;
    $z = microtime(true);
    $data = array(
      'v' => 1,
      'tid' => $ga_id,
      'cid' => $cid,
      'uip' => $_SERVER['REMOTE_ADDR'],
      't' => 'pageview',
      'dh' => $_SERVER['SERVER_NAME'],
      'dl' => $destination,
      'dp' => $path,
      'aip' => 1,
      'dr' => $_SERVER['HTTP_REFERER'],
      'z' => $z,
    );
    if ($data) {
      $getString = 'https://ssl.google-analytics.com/collect';
      $getString .= '?payload_data&';
      $getString .= http_build_query($data);

      // drupal_http_request() is not available during hook_boot()
      $request = file_get_contents($getString, false);
      if ($request) {
        return;
      }
      return;
    }
  }
}

/**
 * Function to retrieve the cid from the cookie
 * Taken from http://www.stumiller.me/implementing-google-analytics-measurement-protocol-in-php-and-wordpress/
 */
function _shurly_analytics_cookies() {
  if (isset($_COOKIE['_ga'])) {
    list($version, $domainDepth, $cid1, $cid2) = preg_split('/[\\.]/', $_COOKIE['_ga'], 4);
    $contents = array(
      'version' => $version,
      'domainDepth' => $domainDepth,
      'cid' => $cid1 . '.' . $cid2,
    );
    $cid = $contents['cid'];
  }
  else {
    $cid = _shurly_analytics_get_uuid();
  }
  return $cid;
}

/**
 * Function to get a unique uuid
 * Taken from http://www.stumiller.me/implementing-google-analytics-measurement-protocol-in-php-and-wordpress/
 */
function _shurly_analytics_get_uuid() {
  return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x', mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xfff) | 0x4000, mt_rand(0, 0x3fff) | 0x8000, mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff));
}