You are here

function _matomo_cache in Matomo Analytics 8

Same name and namespace in other branches
  1. 7.2 matomo.module \_matomo_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 _matomo_cache()
matomo_cron in ./matomo.module
Implements hook_cron().
matomo_page_attachments in ./matomo.module
Implements hook_page_attachments().

File

./matomo.module, line 493
Drupal Module: Matomo.

Code

function _matomo_cache($location, $synchronize = FALSE) {
  $path = 'public://matomo';
  $file_destination = $path . '/' . basename($location);
  if (!file_exists($file_destination) || $synchronize) {

    // Download the latest tracking code.
    try {
      $data = \Drupal::httpClient()
        ->get($location)
        ->getBody(TRUE);
      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 && \Drupal::service('file_system')
          ->prepareDirectory($path)) {

          // Save updated tracking code file to disk.
          \Drupal::service('file_system')
            ->saveData($data, $file_destination, FileSystemInterface::EXISTS_REPLACE);

          // Based on Drupal Core class AssetDumper.
          if (extension_loaded('zlib') && \Drupal::config('system.performance')
            ->get('js.gzip')) {
            \Drupal::service('file_system')
              ->saveData(gzencode($data, 9, FORCE_GZIP), $file_destination . '.gz', FileSystemInterface::EXISTS_REPLACE);
          }
          \Drupal::logger('matomo')
            ->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 (\Drupal::service('file_system')
          ->prepareDirectory($path, FileSystemInterface::CREATE_DIRECTORY)) {

          // There is no need to flush JS here as core refreshes JS caches
          // automatically, if new files are added.
          \Drupal::service('file_system')
            ->saveData($data, $file_destination, FileSystemInterface::EXISTS_REPLACE);

          // Based on Drupal Core class AssetDumper.
          if (extension_loaded('zlib') && \Drupal::config('system.performance')
            ->get('js.gzip')) {
            \Drupal::service('file_system')
              ->saveData(gzencode($data, 9, FORCE_GZIP), $file_destination . '.gz', FileSystemInterface::EXISTS_REPLACE);
          }
          \Drupal::logger('matomo')
            ->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('matomo', $exception);
    }
  }
  else {

    // Return the local JS file path.
    return file_url_transform_relative(file_create_url($file_destination));
  }
}