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.

onsite-search-no-query-strings-img1

As long as your search URLs are formatted like www.site.com/?s=searchterm, 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: www.site.com/search/searchterm. 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.

REWRITE THE URL WITH GOOGLE TAG MANAGER

  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.
    function(){
        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 www.site.com/search/searchterm, 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.
    onsite-search-no-query-strings-img2
  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 www.site.com/search/searchterm, the URL will be sent to Google Analytics' servers as www.site.com/search?s=searchterm 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.
    onsite-search-no-query-strings-img3
  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.

onsite-search-no-query-strings-img4