You are here

analytics.module in Analytics 7

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

File

analytics.module
View source
<?php

/**
 * Implements hook_hook_info().
 */
function analytics_hook_info() {
  $info['analytics_service_info'] = array(
    'group' => 'analytics',
  );
  $info['analytics_service_info_alter'] = array(
    'group' => 'analytics',
  );
  $info['analytics_default_service'] = array(
    'group' => 'analytics',
  );
  $info['analytics_default_service_alter'] = array(
    'group' => 'analytics',
  );
  return $info;
}

/**
 * Implements hook_permission().
 */
function analytics_permission() {
  return array(
    'administer analytics' => array(
      'title' => t('Administer analytics'),
      'description' => t('Configure analytics for the site.'),
    ),
  );
}

/**
 * Implements hook_menu().
 */
function analytics_menu() {
  $items['admin/config/services/analytics/settings'] = array(
    'title' => 'Settings',
    'description' => 'Enable and configure analytics services and settings.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'analytics_settings_form',
    ),
    'access arguments' => array(
      'administer analytics',
    ),
    'file' => 'analytics.admin.inc',
    'type' => MENU_LOCAL_TASK,
  );
  return $items;
}

/**
 * Implements hook_theme().
 */
function analytics_theme() {
  $info['analytics_js'] = array(
    'variables' => array(
      'js' => NULL,
    ),
    'file' => 'analytics.theme.inc',
  );
  return $info;
}

/**
 * Implements hook_page_build().
 */
function analytics_page_build(&$page) {
  $added = FALSE;
  if (!variable_get('analytics_disable_page_build', FALSE)) {
    $context = array(
      'page' => $page,
      'path' => current_path(),
      'account' => $GLOBALS['user'],
    );
    if ($output = analytics_get_output($context)) {
      $page += array(
        'page_bottom' => array(),
      );
      $page['page_bottom'] += $output;
      $added = TRUE;
    }
  }
  if ($added && variable_get('analytics_privacy_dnt', TRUE)) {
    drupal_add_js(drupal_get_path('module', 'analytics') . '/analytics.js', array(
      'every_page' => TRUE,
      'weight' => -100,
    ));
  }
}
function analytics_get_output(array $context = array()) {
  $results = array();
  $services = analytics_service_load_enabled();
  foreach ($services as $service) {
    try {
      if ($service
        ->canTrack($context) && ($output = $service
        ->getOutput($context))) {

        // Allow modules to alter the render array.
        drupal_alter('analytics_output', $output, $service);
        $results['analytics_' . $service
          ->getMachineName()] = $output;
      }
    } catch (Exception $e) {
      watchdog_exception('analytics', $e);
    }
  }
  return $results;
}

/**
 * @param $service
 *
 * @return AnalyticsServiceInterface
 *
 * @throws \Exception
 */
function analytics_get_service_instance($service) {
  $info = analytics_get_service_info();
  if (!empty($info[$service->service]['class']) && class_exists($info[$service->service]['class'])) {
    return new $info[$service->service]['class']((array) $service);
  }
  else {

    //throw new Exception("Could not find analytics service class for {$service->service}.");
  }
}
function analytics_get_service_info($service = NULL, $reset = FALSE) {
  $info =& drupal_static(__FUNCTION__, array());
  if ($reset) {
    $info = NULL;
    cache_clear_all('analytics-info:', 'cache', TRUE);
  }
  $cid = 'analytics-info:' . $GLOBALS['language']->language;
  if (!isset($info[$cid])) {
    $info[$cid] = array();
    if ($cache = cache_get($cid)) {
      $info[$cid] = $cache->data;
    }
    else {
      foreach (module_implements('analytics_service_info') as $module) {
        if ($module_info = module_invoke($module, 'analytics_service_info')) {
          foreach ($module_info as $key => $value) {
            $module_info[$key]['module'] = $module;
          }
          $info[$cid] = array_merge_recursive($info[$cid], $module_info);
        }
      }
      drupal_alter('analytics_service_info', $info[$cid]);
      cache_set($cid, $info[$cid]);
    }
  }
  if (isset($service)) {
    return !empty($info[$cid][$service]) ? $info[$cid][$service] : FALSE;
  }
  else {
    return $info[$cid];
  }
}

/**
 * @return AnalyticsServiceInterface[]
 */
function analytics_service_load_enabled() {
  $results =& drupal_static(__FUNCTION__);
  if (!isset($results)) {
    $results = array();
    ctools_include('export');
    $services = ctools_export_crud_load_all('analytics_service');
    foreach ($services as $id => $service) {
      if (empty($service->disabled) && ($instance = analytics_get_service_instance($service))) {
        $results[$id] = $instance;
      }
    }
  }
  return $results;
}

/**
 * Implements hook_ctools_plugin_directory().
 */
function analytics_ctools_plugin_directory($module, $plugin) {
  if ($module == 'ctools' && $plugin == 'export_ui') {
    return 'lib/' . $plugin;
  }
}
function analytics_service_add_service_options() {
  $results = array();
  foreach (analytics_get_service_info(NULL, TRUE) as $key => $service) {
    if (!empty($service['multiple'])) {
      $results[$key] = $service['label'];
    }
    else {
      ctools_include('export');
      if (ctools_export_load_object('analytics_service', 'conditions', array(
        'service' => $key,
      ))) {
        continue;
      }
      $results[$key] = $service['label'];
    }
  }
  return $results;
}

/**
 * Implements hook_cron().
 */
function analytics_cron() {
  if (!variable_get('analytics_cache_urls', FALSE)) {
    variable_del('analytics_cached_urls');
    return;
  }
  $directory = 'public://analytics';
  if (!file_prepare_directory($directory)) {
    variable_del('analytics_cached_urls');
    return;
  }
  $cached_urls = array();
  foreach (analytics_service_load_enabled() as $service) {
    if ($urls = $service
      ->getCacheableUrls()) {
      $destination = 'public://analytics/' . $service
        ->getMachineName();
      foreach ($urls as $url) {
        if (isset($cached_urls[$url])) {

          // If this file has already been cached, don't download it again.
          continue;
        }
        if (!file_prepare_directory($destination)) {
          continue;
        }
        if ($file = system_retrieve_file($url, $destination, FALSE, FILE_EXISTS_REPLACE)) {
          $cached_urls[$url] = $file;
        }
      }
    }
  }
  variable_set('analytics_cached_urls', $cached_urls);
}

/**
 * Implements hook_file_uri_alter().
 */
function analytics_file_url_alter(&$uri) {
  $cached_urls =& drupal_static(__FUNCTION__);
  if (!isset($cached_urls)) {
    $cached_urls = variable_get('analytics_cached_urls', array());
  }
  if (isset($cached_urls[$uri])) {
    $uri = $cached_urls[$uri];
  }
}
function analytics_url($url, $relative = TRUE) {
  $url = file_create_url($url);
  if ($relative) {
    $url = preg_replace('/^https?:/', '', $url);
  }
  return $url;
}

/**
 * Implements hook_features_export_alter().
 */
function analytics_features_export_alter(&$export, $module_name) {
  if (!empty($export['features']['analytics_service'])) {
    ctools_include('export');
    $services = ctools_export_crud_load_multiple('analytics_service', $export['features']['analytics_service']);
    foreach ($services as $service) {

      // Add the service's module as a dependency.
      if ($info = analytics_get_service_info($service->service)) {
        $export['dependencies'][$info['module']] = $info['module'];
      }
    }

    // Ensure the dependencies list is still sorted alphabetically.
    ksort($export['dependencies']);
  }
}