You are here

function better_statistics_preprocess_html in Better Statistics 7

Implements hook_preprocess_html().

If accesslog mode is set to client-side only or mixed mode, we add JS when HTML output is being rendered so that client-side data collection can be used for access statistics and/or node hit statistics.

File

./better_statistics.module, line 156
Drupal hook implementations for the Better Statistics module.

Code

function better_statistics_preprocess_html(&$vars) {
  $default_js = '';
  $bs = variable_get('better_statistics_bs_var', 'bs');
  $bs_path = drupal_get_path('module', 'better_statistics');

  // If the accesslog is set to client-side or mixed mode, do some processing.
  $al_mode = variable_get('statistics_enable_access_log', BETTER_STATISTICS_ACCESSLOG_DISABLED);
  if ($al_mode == BETTER_STATISTICS_ACCESSLOG_MIXED || $al_mode == BETTER_STATISTICS_ACCESSLOG_CLIENT) {

    // Grab all declared JS from Better Statistics fields and add them here
    // at the JS_DEFAULT group level in such a way that they are all
    // aggregated together.
    $fields = variable_get('better_statistics_fields', better_statistics_get_default_fields());
    $options = array(
      'scope' => 'header',
      'group' => JS_DEFAULT,
      'every_page' => FALSE,
    );
    foreach ($fields as $field) {
      if (isset($field['js']['data'])) {
        $data = $field['js']['data'];
        unset($field['js']['data']);
        drupal_add_js($data, array_merge($field['js'], $options));
      }
    }

    // Add BS JS API accesslog method. Note that internal path should always
    // be the same for a page even when cached, so we manually add it here.
    $default_js .= $bs . "('set', 'path', '" . truncate_utf8($_GET['q'], 255) . "'); ";
    $default_js .= $bs . "('accesslog'); ";

    // Prevent access statistics from being collected in hook_exit() so that
    // duplicate statistics are never gathered when in mixed mode.
    global $conf;
    $conf['statistics_enable_access_log'] = BETTER_STATISTICS_ACCESSLOG_DISABLED;
  }

  // If enabled, add code to JS API implementation for the node counter.
  // @see statistics_exit()
  $ev_mode = variable_get('statistics_count_content_views', BETTER_STATISTICS_ENTITY_VIEW_DISABLED);
  if ($ev_mode == BETTER_STATISTICS_ENTITY_VIEW_CLIENT) {

    // We are counting content views.
    if (arg(0) == 'node' && is_numeric(arg(1)) && arg(2) == NULL) {

      // Add a call to the method.
      $default_js .= $bs . "('entity_view', 'node', " . arg(1) . "); ";

      // Prevent the node counter from being triggered in hook_exit() so that
      // duplicate statistics are never gathered when in mixed mode.
      global $conf;
      $conf['statistics_count_content_views'] = FALSE;
    }
  }

  // If any BS JS API methods have been added to the request, add the API.
  $api_added = better_statistics_add_js_api();

  // Add a default implementation of the BS JS API.
  if ($api_added && !empty($default_js)) {
    $default_js = preg_replace('!\\s+!', ' ', $default_js);
    drupal_add_js($default_js, array(
      'type' => 'inline',
      'group' => JS_THEME,
      'weight' => 25,
    ));
  }
}