diff --git a/better_statistics.module b/better_statistics.module index 4bec390..c4eb989 100644 --- a/better_statistics.module +++ b/better_statistics.module @@ -26,6 +26,10 @@ define('BETTER_STATISTICS_ENTITY_VIEW_DISABLED', 0); define('BETTER_STATISTICS_ENTITY_VIEW_DEFAULT', 1); define('BETTER_STATISTICS_ENTITY_VIEW_CLIENT', 2); +/** + * Cookie name for cache status. + */ +define('BETTER_STATISTICS_CACHE_COOKIE', 'Drupal.cache.status'); /** * Implements hook_menu(). @@ -97,6 +101,42 @@ function better_statistics_ctools_plugin_api_hook_name() { return 'statistics_api'; } +/** + * Implements hook_boot(). + * + * On cached pages, adds a reference cookie to the HTTP response to track + * the Drupal cache status. + */ +function better_statistics_boot() { + // Ensure variables can be accessed. + drupal_bootstrap(DRUPAL_BOOTSTRAP_VARIABLES, FALSE); + + // Return if Better Statistics is disabled or in default mode. + $al_mode = variable_get('statistics_enable_access_log', BETTER_STATISTICS_ACCESSLOG_DISABLED); + if ($al_mode == BETTER_STATISTICS_ACCESSLOG_DISABLED || $al_mode == BETTER_STATISTICS_ACCESSLOG_DEFAULT) { + return; + } + + // Is Drupal page caching enabled. + $drupal_cache_enabled = variable_get('cache'); + + // Is Better Statistics enabled to track cached pages. + $bs_cache_logging = variable_get('statistics_access_log_restrictions_cache', TRUE); + + // Set a cookie with cache status retrieved from the headers. + if ($drupal_cache_enabled && $bs_cache_logging) { + $headers = headers_list(); + if (array_search('X-Drupal-Cache: HIT', $headers) !== FALSE) { + setcookie(BETTER_STATISTICS_CACHE_COOKIE, 'HIT', REQUEST_TIME + 60); + } + elseif (array_search('X-Drupal-Cache: MISS', $headers) !== FALSE) { + setcookie(BETTER_STATISTICS_CACHE_COOKIE, 'MISS', REQUEST_TIME + 60); + } + else { + setcookie(BETTER_STATISTICS_CACHE_COOKIE, 'void', REQUEST_TIME + 60); + } + } +} /** * Implements hook_exit(). @@ -495,21 +535,3 @@ function better_statistics_add_js_api() { return $js_added; } - -function better_statistics_boot() { - drupal_bootstrap(DRUPAL_BOOTSTRAP_VARIABLES, FALSE); - $cache_enabled = variable_get('cache'); - $al_mode = variable_get('statistics_enable_access_log', BETTER_STATISTICS_ACCESSLOG_DISABLED); - if ($cache_enabled && ($al_mode == BETTER_STATISTICS_ACCESSLOG_MIXED || $al_mode == BETTER_STATISTICS_ACCESSLOG_CLIENT)) { - $headers = headers_list(); - if (array_search('X-Drupal-Cache: HIT', $headers) !== FALSE) { - setcookie('Drupal.cache.status', 'HIT', time() + 60); - } - elseif (array_search('X-Drupal-Cache: MISS', $headers) !== FALSE) { - setcookie('Drupal.cache.status', 'MISS', time() + 60); - } - else { - setcookie('Drupal.cache.status', 'void', time() + 60); - } - } -} diff --git a/js/fields/custom.js b/js/fields/custom.js index 4ea6e48..f2a4bb0 100644 --- a/js/fields/custom.js +++ b/js/fields/custom.js @@ -1,16 +1,18 @@ -function getCookieVal(key) { - var tmp = document.cookie.match((new RegExp(key + '=[a-zA-Z0-9.()=|%/_]+($|;)', 'g'))); - if (!tmp || !tmp[0]) { - return null; - } - else { - return unescape(tmp[0].substring(key.length + 1, tmp[0].length).replace(';', '')) || null; - } -}; - document.addEventListener('statistics.accesslog', function(event) { + + // Retrieve a cookie's value by key. + function getCookie(key) { + var tmp = document.cookie.match((new RegExp(key + '=[a-zA-Z0-9.()=|%/_]+($|;)', 'g'))); + if (!tmp || !tmp[0]) { + return null; + } + else { + return unescape(tmp[0].substring(key.length + 1, tmp[0].length).replace(';', '')) || null; + } + }; + // Cache Status. - var cacheStatus = getCookieVal('Drupal.cache.status'); + var cacheStatus = getCookie('Drupal.cache.status'); if (cacheStatus == 'void' || cacheStatus == null) { event.data.cache = ''; }