You are here

function _google_tag_snippets_save in GoogleTagManager 7

Saves JS snippet files based on current settings.

Return value

bool Whether the files were saved.

1 call to _google_tag_snippets_save()
_google_tag_assets_create in includes/admin.inc
Saves snippet files and data layer classes based on current settings.

File

includes/admin.inc, line 320
Contains the administrative page and form callbacks.

Code

function _google_tag_snippets_save($realm_name = '', $realm_key = '') {
  $realm_string = $realm_name ? "{$realm_name}:{$realm_key}" : 'default';
  $realm_name .= $realm_name ? '/' : '';
  $realm_key .= $realm_key ? '.' : '';

  // Save the altered snippets after hook_google_tag_snippets_alter().
  module_load_include('inc', 'google_tag', 'includes/snippet');
  $result = TRUE;
  $snippets = google_tag_snippets();
  foreach ($snippets as $type => $snippet) {
    $path = file_unmanaged_save_data($snippet, "public://google_tag/{$realm_name}google_tag.{$realm_key}{$type}.js", FILE_EXISTS_REPLACE);
    $result = !$path ? FALSE : $result;
  }
  $args = array(
    '@count' => count($snippets),
  );
  if ($realm_name) {
    $args += array(
      '@realm' => $realm_string,
    );
    $message = $result ? 'Created @count snippet files for @realm realm based on configuration.' : 'An error occurred saving @count snippet files for @realm realm. Contact the site administrator if this persists.';
  }
  else {
    $message = $result ? 'Created @count snippet files based on configuration.' : 'An error occurred saving @count snippet files. Contact the site administrator if this persists.';
  }
  _google_tag_message_display($message, $args, $result ? 'status' : 'error');
  if (!$result) {
    watchdog('google_tag', $message, $args, WATCHDOG_ERROR);
  }
}