You are here

function _matomo_cache in Matomo Analytics 7.2

Same name and namespace in other branches
  1. 8 matomo.module \_matomo_cache()

Download/Synchronize/Cache tracking code file locally.

Parameters

$location: The full URL to the external javascript file.

$sync_cached_file: Synchronize tracking code and update if remote file have 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_alter in ./matomo.module
Implements hook_page_alter() to insert JavaScript to the appropriate scope/region of the page.

File

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

Code

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

    // 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 = drupal_hash_base64(file_get_contents($file_destination));
        $data_hash_remote = drupal_hash_base64($result->data);

        // Check that the files directory is writable.
        if ($data_hash_local != $data_hash_remote && file_prepare_directory($path)) {

          // Save updated tracking code file to disk.
          file_unmanaged_save_data($result->data, $file_destination, FILE_EXISTS_REPLACE);

          // Based on Drupal Core drupal_build_css_cache().
          if (variable_get('css_gzip_compression', TRUE) && variable_get('clean_url', 0) && extension_loaded('zlib')) {
            file_unmanaged_save_data(gzencode($result->data, 9, FORCE_GZIP), $file_destination . '.gz', FILE_EXISTS_REPLACE);
          }
          watchdog('matomo', '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_prepare_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_unmanaged_save_data($result->data, $file_destination, FILE_EXISTS_REPLACE);
          if (variable_get('css_gzip_compression', TRUE) && variable_get('clean_url', 0) && extension_loaded('zlib')) {
            file_unmanaged_save_data(gzencode($result->data, 9, FORCE_GZIP), $file_destination . '.gz', FILE_EXISTS_REPLACE);
          }
          watchdog('matomo', 'Locally cached tracking code file has been saved.', array(), WATCHDOG_INFO);

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

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