diff --git a/google_tag.module b/google_tag.module
index afcba66..6ec35a4 100644
--- a/google_tag.module
+++ b/google_tag.module
@@ -35,12 +35,17 @@ function google_tag_help($route_name, RouteMatchInterface $route_match) {
 }
 
 /**
+ * Make sure the tag snippet files are (re)created when the cache is rebuild.
+ *
+ * (Re)creating the snippets files may, for example, be needed when a deploy to
+ * a different environment is done.
+ *
  * Implements hook_rebuild().
  */
 function google_tag_rebuild() {
   $rebuild_snippets = \Drupal::config('google_tag.settings')->get('rebuild_snippets');
-  if ($rebuild_snippets) {
-    _google_tag_assets_create();
+  if (!empty($rebuild_snippets)) {
+    google_tag_save_snippets();
   }
 }
 
@@ -124,18 +129,23 @@ function _google_tag_file_tag($type, $weight) {
  *   The tag array.
  */
 function _google_tag_inline_tag($type, $weight) {
   $uri = "public://google_tag/google_tag.$type.js";
   $url = \Drupal::service('file_system')->realpath($uri);
-  $contents = @file_get_contents($url);
-  $attachment = $contents ? [
+
+  // Be sure that the tags exist before fetching them.
+  if (!file_exists($url)) {
+    google_tag_save_snippets();
+  }
+
+  $attachment = [
     [
       '#type' => 'html_tag',
       '#tag' => 'script',
-      '#value' => $contents,
+      '#value' => file_get_contents($url),
       '#weight' => $weight,
     ],
     "google_tag_{$type}_tag",
-  ] : [];
+  ];
   return $attachment;
 }
 
@@ -299,3 +309,34 @@ function _google_tag_role_check() {
   }
   return $satisfied;
 }
+
+/**
+ * Saves JS snippet files based on current settings.
+ *
+ * @return bool
+ *   Whether the files were saved.
+ *
+ * @throws Exception
+ *   Thrown when there are critical problems creating a snippet.
+ */
+function google_tag_save_snippets() {
+  module_load_include('inc', 'google_tag', 'includes/snippet');
+  $path = 'public://google_tag';
+
+  if ($result = file_prepare_directory($path, FILE_CREATE_DIRECTORY)) {
+    $snippets = google_tag_snippets();
+    foreach ($snippets as $type => $snippet) {
+      $file_path = file_unmanaged_save_data($snippet, $path . "/google_tag.$type.js", FILE_EXISTS_REPLACE);
+      $result = (!$result || !$file_path) ? FALSE : TRUE;
+    }
+  }
+
+  if (!$result) {
+    throw new Exception(t('An error occurred saving one or more snippet files. Please try again or contact the site administrator if it persists.'));
+  }
+  else {
+    \Drupal::service('asset.js.collection_optimizer')->deleteAll();
+    _drupal_flush_css_js();
+    return TRUE;
+  }
+}
diff --git a/src/Form/GoogleTagSettingsForm.php b/src/Form/GoogleTagSettingsForm.php
index c0183f6..3c590c5 100644
--- a/src/Form/GoogleTagSettingsForm.php
+++ b/src/Form/GoogleTagSettingsForm.php
@@ -366,28 +366,14 @@ class GoogleTagSettingsForm extends ConfigFormBase {
 
   /**
    * Saves JS snippet files based on current settings.
-   *
-   * @return bool
-   *   Whether the files were saved.
    */
   public function saveSnippets() {
-    // 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/google_tag.$type.js", FILE_EXISTS_REPLACE);
-      $result = !$path ? FALSE : $result;
-    }
-    if (!$result) {
-      drupal_set_message($this->t('An error occurred saving one or more snippet files. Please try again or contact the site administrator if it persists.'));
-    }
-    else {
+    try {
+      google_tag_save_snippets();
       drupal_set_message($this->t('Created three snippet files based on configuration.'));
-      \Drupal::service('asset.js.collection_optimizer')->deleteAll();
-      _drupal_flush_css_js();
+    } catch (\Exception $exception) {
+      drupal_set_message(t('An error occurred saving one or more snippet files. Please try again and make sure your files directory is writable.'));
     }
-    return $result;
   }
 
   /**
