JavascriptLocalCache.php in Google Analytics 4.x
File
src/JavascriptLocalCache.php
View source
<?php
namespace Drupal\google_analytics;
use Drupal\Component\Utility\Crypt;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\Core\State\StateInterface;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\RequestException;
class JavascriptLocalCache {
const GOOGLE_ANALYTICS_JAVASCRIPT_URL = 'https://www.googletagmanager.com/gtag/js';
protected $fileSystem;
protected $configFactory;
protected $logger;
protected $httpClient;
protected $state;
public function __construct(ClientInterface $http_client, FileSystemInterface $file_system, ConfigFactoryInterface $config_factory, LoggerChannelFactoryInterface $logger_factory, StateInterface $state) {
$this->httpClient = $http_client;
$this->fileSystem = $file_system;
$this->configFactory = $config_factory;
$this->state = $state;
$this->logger = $logger_factory
->get('google_analytics');
}
public function fetchGoogleAnalyticsJavascript(string $tracking_id, bool $synchronize = FALSE) {
$path = 'public://google_analytics';
$remote_url = self::GOOGLE_ANALYTICS_JAVASCRIPT_URL . '?id=' . $tracking_id;
$file_destination = $path . '/gtag.js';
if (!$this->configFactory
->get('google_analytics.settings')
->get('cache')) {
return $remote_url;
}
if (!file_exists($file_destination) || $synchronize) {
try {
$data = (string) $this->httpClient
->get($remote_url)
->getBody();
if (file_exists($file_destination)) {
$data_hash_local = Crypt::hashBase64(file_get_contents($file_destination));
$data_hash_remote = Crypt::hashBase64($data);
if ($data_hash_local != $data_hash_remote && $this->fileSystem
->prepareDirectory($path)) {
$this->fileSystem
->saveData($data, $file_destination, FileSystemInterface::EXISTS_REPLACE);
if (extension_loaded('zlib') && $this->configFactory
->get('system.performance')
->get('js.gzip')) {
$this->fileSystem
->saveData(gzencode($data, 9, FORCE_GZIP), $file_destination . '.gz', FileSystemInterface::EXISTS_REPLACE);
}
$this->logger
->info('Locally cached tracking code file has been updated.');
_drupal_flush_css_js();
}
}
else {
if ($this->fileSystem
->prepareDirectory($path, FileSystemInterface::CREATE_DIRECTORY)) {
$this->fileSystem
->saveData($data, $file_destination, FileSystemInterface::EXISTS_REPLACE);
if (extension_loaded('zlib') && $this->configFactory
->get('system.performance')
->get('js.gzip')) {
$this->fileSystem
->saveData(gzencode($data, 9, FORCE_GZIP), $file_destination . '.gz', FileSystemInterface::EXISTS_REPLACE);
}
$this->logger
->info('Locally cached tracking code file has been saved.');
}
}
} catch (RequestException $exception) {
watchdog_exception('google_analytics', $exception);
return $remote_url;
}
}
$query_string = '?' . (\Drupal::state()
->get('system.css_js_query_string') ?: '0');
return file_url_transform_relative(file_create_url($file_destination)) . $query_string;
}
public function clearGoogleAnalyticsJsCache() {
$path = 'public://google_analytics';
if (is_dir($path)) {
$this->fileSystem
->deleteRecursive($path);
_drupal_flush_css_js();
$this->logger
->info('Local Google Analytics file cache has been purged.');
}
}
}