 google_analytics.module | 49 +++++++++++--------------------------------------
 1 file changed, 11 insertions(+), 38 deletions(-)

diff --git a/google_analytics.module b/google_analytics.module
index 7135bed..953066c 100644
--- a/google_analytics.module
+++ b/google_analytics.module
@@ -136,10 +136,10 @@ function google_analytics_page_attachments(array &$page) {
 
     // Site search tracking support.
     if (\Drupal::moduleHandler()->moduleExists('search') && $config->get('track.site_search') && (strpos(\Drupal::routeMatch()->getRouteName(), 'search.view') === 0) && $keys = google_analytics_search_get_keys()) {
-      // hook_item_list__search_results() is not executed if search result is
-      // empty. Make sure the counter is set to 0 if there are no results.
+      // The pager item number can tell the number of search results.
+      global $pager_total_items;
       $entity_id = \Drupal::routeMatch()->getParameter('entity')->id();
-      $url_custom = '(window.google_analytics_search_results) ? ' . Json::encode(Url::fromRoute('search.view_' . $entity_id, array(), array('query' => array('search' => $keys)))->toString()) . ' : ' . Json::encode(Url::fromRoute('search.view_' . $entity_id, array('query' => array('search' => 'no-results:' . $keys, 'cat' => 'no-results')))->toString());
+      $url_custom = (intval($pager_total_items[0]) > 0) ? Json::encode(Url::fromRoute('search.view_' . $entity_id, array(), array('query' => array('search' => $keys)))->toString()) : Json::encode(Url::fromRoute('search.view_' . $entity_id, array('query' => array('search' => 'no-results:' . $keys, 'cat' => 'no-results')))->toString());
     }
 
     // If this node is a translation of another node, pass the original
@@ -318,18 +318,16 @@ function google_analytics_page_attachments(array &$page) {
       // Custom tracking. Prepend before all other JavaScript.
       // @TODO: https://support.google.com/adsense/answer/98142
       // sounds like it could be appended to $script.
-      $page['#attached']['js'][] = array(
-        'data' => $googleanalytics_adsense_script,
-        'group' => JS_LIBRARY-1,
-        'type' => 'inline',
-      );
+      $script = $googleanalytics_adsense_script . $script;
     }
 
-    $page['#attached']['js'][] = array(
-      'data' => $script,
-      'scope' => 'header',
-      'type' => 'inline',
-    );
+    $page['#attached']['html_head'][] = [
+      [
+        '#tag' => 'script',
+        '#value' => $script,
+      ],
+      'ga_scripts'
+    ];
   }
 }
 
@@ -420,31 +418,6 @@ function google_analytics_cron() {
 }
 
 /**
- * Implements hook_preprocess_item_list__search_results().
- *
- * Collects and adds the number of search results to the head.
- */
-//function google_analytics_item_list__search_results(&$variables) {
-function google_analytics_preprocess_item_list(&$variables) {
-  $config = \Drupal::config('google_analytics.settings');
-
-  // Only run on search results list.
-  if ($config->get('track.site_search') && $variables['theme_hook_original'] == 'item_list__search_results') {
-    // There is no search result $variable available that hold the number of items
-    // found. The variable $variables['items'] has the current page items only.
-    // But the pager item mumber can tell the number of search results.
-    global $pager_total_items;
-
-    $page['#attached']['js'][] = array(
-      'data' => 'window.google_analytics_search_results = ' . intval($pager_total_items[0]) . ';',
-      'type' => 'inline',
-      'group' => JS_LIBRARY-1,
-    );
-    drupal_render($page);
-  }
-}
-
-/**
  * Helper function for grabbing search keys. Function is missing in D8.
  *
  * http://api.drupal.org/api/function/search_get_keys/6
