function _google_analytics_cache in Google Analytics 8.2
Same name and namespace in other branches
- 8.3 google_analytics.module \_google_analytics_cache()
Download/Synchronize/Cache tracking code file locally.
Parameters
string $location: The full URL to the external javascript file.
bool $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 _google_analytics_cache()
- google_analytics_cron in ./
google_analytics.module - Implements hook_cron().
- google_analytics_page_attachments in ./
google_analytics.module - Implements hook_page_attachments().
File
- ./
google_analytics.module, line 491 - Drupal Module: Google Analytics.
Code
function _google_analytics_cache($location, $synchronize = FALSE) {
$path = 'public://google_analytics';
$file_destination = $path . '/' . basename($location);
$filesystem = \Drupal::service('file_system');
if (!file_exists($file_destination) || $synchronize) {
// Download the latest tracking code.
try {
$data = (string) \Drupal::httpClient()
->get($location)
->getBody();
if (file_exists($file_destination)) {
// Synchronize tracking code and and replace local file if outdated.
$data_hash_local = Crypt::hashBase64(file_get_contents($file_destination));
$data_hash_remote = Crypt::hashBase64($data);
// Check that the files directory is writable.
if ($data_hash_local != $data_hash_remote && $filesystem
->prepareDirectory($path)) {
// Save updated tracking code file to disk.
$filesystem
->saveData($data, $file_destination, FileSystemInterface::EXISTS_REPLACE);
// Based on Drupal Core class AssetDumper.
if (extension_loaded('zlib') && \Drupal::config('system.performance')
->get('js.gzip')) {
$filesystem
->saveData(gzencode($data, 9, FORCE_GZIP), $file_destination . '.gz', FileSystemInterface::EXISTS_REPLACE);
}
\Drupal::logger('google_analytics')
->info('Locally cached tracking code file has been updated.');
// 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 ($filesystem
->prepareDirectory($path, FileSystemInterface::CREATE_DIRECTORY)) {
// There is no need to flush JS here as core refreshes JS caches
// automatically, if new files are added.
$filesystem
->saveData($data, $file_destination, FileSystemInterface::EXISTS_REPLACE);
// Based on Drupal Core class AssetDumper.
if (extension_loaded('zlib') && \Drupal::config('system.performance')
->get('js.gzip')) {
$filesystem
->saveData(gzencode($data, 9, FORCE_GZIP), $file_destination . '.gz', FileSystemInterface::EXISTS_REPLACE);
}
\Drupal::logger('google_analytics')
->info('Locally cached tracking code file has been saved.');
// Return the local JS file path.
return file_url_transform_relative(file_create_url($file_destination));
}
}
} catch (RequestException $exception) {
watchdog_exception('google_analytics', $exception);
}
}
else {
// Return the local JS file path.
return file_url_transform_relative(file_create_url($file_destination));
}
}