function _googleanalytics_cache in Google Analytics 6.4
Same name and namespace in other branches
- 5 googleanalytics.module \_googleanalytics_cache()
- 6 googleanalytics.module \_googleanalytics_cache()
- 6.2 googleanalytics.module \_googleanalytics_cache()
- 6.3 googleanalytics.module \_googleanalytics_cache()
- 7.2 googleanalytics.module \_googleanalytics_cache()
- 7 googleanalytics.module \_googleanalytics_cache()
Download/Synchronize/Cache tracking code file locally.
Parameters
$location: The full URL to the external javascript file.
$synchronize: Synchronize to local cache if remote file has changed.
Return value
mixed The path to the local javascript file on success, boolean FALSE on failure.
2 calls to _googleanalytics_cache()
- googleanalytics_add_js in ./
googleanalytics.module - Adds Google Analytics tracking scripts.
- googleanalytics_cron in ./
googleanalytics.module - Implementation of hook_cron().
File
- ./
googleanalytics.module, line 449 - Drupal Module: Google Analytics
Code
function _googleanalytics_cache($location, $synchronize = FALSE) {
$path = file_create_path('googleanalytics');
$file_destination = $path . '/' . basename($location);
if (!file_exists($file_destination) || $synchronize) {
// Download the latest tracking code.
$result = drupal_http_request($location);
if ($result->code == 200) {
if (file_exists($file_destination)) {
// Synchronize tracking code and and replace local file if outdated.
$data_hash_local = md5(file_get_contents($file_destination));
$data_hash_remote = md5($result->data);
// Check that the files directory is writable.
if ($data_hash_local != $data_hash_remote && file_check_directory($path)) {
// Save updated tracking code file to disk.
file_save_data($result->data, $file_destination, FILE_EXISTS_REPLACE);
watchdog('googleanalytics', 'Locally cached tracking code file has been updated.', array(), WATCHDOG_INFO);
// Change query-strings on css/js files to enforce reload for all users.
_drupal_flush_css_js();
}
}
else {
// Check that the files directory is writable.
if (file_check_directory($path, FILE_CREATE_DIRECTORY)) {
// There is no need to flush JS here as core refreshes JS caches
// automatically, if new files are added.
file_save_data($result->data, $file_destination, FILE_EXISTS_REPLACE);
watchdog('googleanalytics', 'Locally cached tracking code file has been saved.', array(), WATCHDOG_INFO);
// Return the local JS file path.
return base_path() . $file_destination;
}
}
}
}
else {
// Return the local JS file path.
return base_path() . $file_destination;
}
}