Track GA Onsite Search Without URL Query Strings

Google Analytics provides a super-easy method of tracking onsite search keywords: simply identify which query string (aka parameter) holds the onsite search term for your site, and enter that value at Admin > View > Site Search Settings. You can find your search query string by performing a search on your site and checking the URL.


As long as your search URLs are formatted like, you can just enter "s" (or whatever query parameter identifies search terms in your URL) into your site search settings and Google Analytics will automatically recognize your onsite search keyword and populate the search terms report.

However, some sites don't push the search term into a query string. Instead, they put the search term into the URL, for example like this: By default, Google Analytics won't recognize this format, so we need to take measures to rewrite that type of URL. This post will describe how to use Google Tag Manager to track onsite search keywords even when the search term isn't in a query string.


  1. Step 1: Navigate in GTM to Variables > Custom JS Variable and add the following script into a variable. You can name that variable whatever you like,  but for this example we'll call it JS - Onsite Search Term.
        var parts = document.documentURI.split('/');
        var lastSegment = parts.pop() || parts.pop();
        return lastSegment;

    The above will return the final piece of a URL. So if your URL is, it will return searchterm.

  2. Navigate to Tags > New > Universal Analytics > Pageview.
    Check the box for 'Enable overriding settings for this tag', then select 'Fields to Set' and click 'Add Field'. Enter 'page' as the ‘Field Name’ and then enter /search?s={{JS - Onsite Search Term}} as the value.
  3. Attach a trigger of Pageview, with the condition Fires on some pages > contains '/search' (or whatever URL pattern identifies your search pages). This means that every time a user views a URL like, the URL will be sent to Google Analytics' servers as instead.
  4. Add that same trigger as a trigger exception to your regular All Pages pageview tag, to avoid double-counting pages.
  5. Finally, back in the Google Analytics interface, navigate to Admin > View > Site Search Settings and enter an "s" into the site search settings field.
  6. Check the box for "Strip query parameters out of URL". This will prevent cluttering your Page report with unique URLs for each search term. You don't need search terms displayed in your URL because you'll now have them in your Onsite Search report!

The results of this tag setup can be viewed by navigating to the onsite search terms report, which is located at Behavior > Site Search > Search Terms.