You are here

google_analytics_et.module in Google Analytics Event Tracking 6

Google Analytics Event Tracking Module.

Provides an extention to the google analytics module. Creates an interfacce for reusable event tracking through the google analytics event tracking system.

File

google_analytics_et.module
View source
<?php

/**
 * @file
 * Google Analytics Event Tracking Module.
 *
 * Provides an extention to the google analytics module. Creates an
 * interfacce for reusable event tracking through the google analytics
 * event tracking system.
 */

/**
 * Implements hook_init().
 */
function google_analytics_et_init() {
  global $user;
  $id = variable_get('googleanalytics_account', '');

  // Get page status code for visibility filtering.

  //$status = drupal_get_http_header('Status');
  $trackable_status_codes = array(
    '403 Forbidden',
    '404 Not Found',
  );

  // Get all selectors defined by google_analytics_et_api hook.
  $selectors = module_invoke_all('google_analytics_et_api');

  // If the override selectors toggle is TRUE then only load the selectors
  // from the variable table.
  $override_hooked_selectors = variable_get('google_analytics_et_selectors_override', FALSE);
  if ($override_hooked_selectors) {
    $selectors = variable_get('google_analytics_et_selectors', '');
  }

  // Sanitize event tracking input to limit the threat of xss.
  $selectors = _google_analytics_et_sanitize_event_trackers($selectors);
  $js = array(
    'googleAnalyticsETSettings' => array(
      'selectors' => $selectors,
    ),
  );

  //if (!empty($id) && (_googleanalytics_visibility_pages() || in_array($status, $trackable_status_codes)) && _googleanalytics_visibility_user($user)) {
  if (!empty($id) && _googleanalytics_visibility_pages() && _googleanalytics_visibility_user($user)) {

    // Get the page scope setting from googleanalytics:
    $scope = variable_get('googleanalytics_js_scope', 'header');
    drupal_add_js($js, 'setting');
    drupal_add_js(drupal_get_path('module', 'google_analytics_et') . '/js/google_analytics_et.js', 'module', $scope);
  }
}

/**
 * Add a single selector to the list of selectors.
 */
function google_analytics_et_add_event_tracker($selector_array) {

  // Get whole list of event trackers.
  $event_trackers = google_analytics_et_get_event_trackers();

  // Append this event tracker to the end of the list.
  $event_trackers[] = $selector_array;

  // Save list of event trackers.
  variable_set('google_analytics_et_selectors', $event_trackers);
}

/**
 * Remove a single selector from the list of events being tracked.
 */
function google_analytics_et_remove_event_tracker($selector) {

  // Get whole list of event trackers.
  $event_trackers = google_analytics_et_get_event_trackers();
  $new_event_trackers = array();

  // Go through all events and copy all that do not match into the new array.
  if (is_array($event_trackers)) {
    foreach ($event_trackers as $key => $value) {
      if ($event_trackers[$key]['selector'] != $selector) {
        $new_event_trackers[] = $event_trackers[$key];
      }
    }
  }

  // Save list of event trackers.
  variable_set('google_analytics_et_selectors', $new_event_trackers);
}

/**
 * Override the hooked event tracking. Do not load hooks.
 *
 * This function can be used to toggle the override status (by not passing
 * a value) or this function can be used to set the override status outright
 * by passing TRUE or FALSE.
 *
 * @param bool $status
 *   This can be left to the default value of NULL if one just wishes to
 *   toggle the override status. If a valid TRUE or FALSE value is passed then
 *   the override status is changed to that.
 */
function google_analytics_et_override_event_tracking($status = NULL) {
  if ($status == NULL) {
    $new_status = variable_get('google_analytics_et_selectors_override', NULL) ? FALSE : TRUE;
    variable_set('google_analytics_et_selectors_override', $new_status);
  }
  elseif (_google_analytics_et_override_event_tracking_validate($status)) {
    variable_set('google_analytics_et_selectors_override', $status);
  }
}

/**
 * Validates the input of the override event input function
 */
function _google_analytics_et_override_event_tracking_validate($status) {
  if ($status == TRUE || $status == FALSE) {
    return TRUE;
  }
  else {
    return FALSE;
  }
}

/**
 * Sanitizes event tracking data that is being put into javascript.
 */
function _google_analytics_et_sanitize_event_trackers($selectors) {
  $sanitized_selectors = array();
  if (is_array($selectors)) {
    foreach ($selectors as $selector) {
      foreach ($selector as $s) {
        $s = filter_xss($s);
      }
      $sanitized_selectors[] = $selector;
    }
  }
  return $sanitized_selectors;
}

/**
 * Get the event tracking override status.
 */
function google_analytics_et_get_event_trackering_override_status() {
  $status = variable_get('google_analytics_et_selectors_override', FALSE);
  return $status;
}

/**
 * Get the event trackers.
 */
function google_analytics_et_get_event_trackers() {
  $et = array();
  $et = variable_get('google_analytics_et_selectors', $et);
  return $et;
}

/**
 * Loads the Event Tracking Selectors, Categories, Action, Label, Value, etc.
 *
 * Allows other modules to implement custom event tracking though the
 * Google Analytics Event Tracking API module.
 *
 * @ingroup google_analytics_et_api_hooks
 */
function google_analytics_et_api() {

  // Loads the user created selectors from the variable table.
  return variable_get('google_analytics_et_selectors', array());
}

Functions

Namesort descending Description
google_analytics_et_add_event_tracker Add a single selector to the list of selectors.
google_analytics_et_api Loads the Event Tracking Selectors, Categories, Action, Label, Value, etc.
google_analytics_et_get_event_trackering_override_status Get the event tracking override status.
google_analytics_et_get_event_trackers Get the event trackers.
google_analytics_et_init Implements hook_init().
google_analytics_et_override_event_tracking Override the hooked event tracking. Do not load hooks.
google_analytics_et_remove_event_tracker Remove a single selector from the list of events being tracked.
_google_analytics_et_override_event_tracking_validate Validates the input of the override event input function
_google_analytics_et_sanitize_event_trackers Sanitizes event tracking data that is being put into javascript.