How to track internal site search using Google Tag Manager

I hope you all enjoyed Search Leeds 2017. I had a fun time, too. One talk in particular stood out, Stephen Kenwright’s How to be Amazon (and beat Google at Search).

Guess I need to get cracking.

To be fair to him, I have written a few blog posts about this topic…

How to double conversion by using Site Search

How to automatically track events with Google Tag Manager

The time I did an investigation as to why everyone searches for “Stardoll” on the B3 site”

How to extract site search terms from subfolders in Google Analytics

So, he’s not wrong, I’ve sort of done this already. Just not as one piece.

Forget about GTM for a minute, let’s focus on Google Analytics

By default, Google Analytics does not track any internal site search for you, meaning you have to tell it “please can you tell me what keywords people type into my search bar”. This is how to track internal site search in Google Analytics:

  • Go to Admin
  • Go to View Settings
  • Go to Site Search Setting
  • Tick Site Search Tracking>On
  • Add your query parameter
  • Optional: Add categories if your site search uses categories
  • Save

PROTIP: Create a Lowercase Filter for Search Term, it means that people searching for “SEO” and “seo” are both tracked under the search term “seo”. Without this, they’re treated as separate keywords.

To view the internal site search report it’s in Behaviour>Site Search.

And you can see all sorts of delights and wonders:

Percentage of people who use site search on your site

Search terms

Tracking “No Results” with Google Tag Manager

By default, you don’t get to see whether the searches returned any results. Tracking searches with “no results” is a really good way to generate ideas for content, among other things.

Every website’s site search is configured differently, so this explanation is for Branded3 – hopefully you can use the same methods for your own website. If you’d like additional  help, please contact us.

Search your website for a query you know will return no results

If you can’t think of a phrase, just mash your keyboard and hit enter.

Take note of the message that displays

In the above, you can see that “No Results found” displays. In Google Analytics, I want to see the amount of pages where this message fires. Inspect the element to find the element ID.

In this case, it’s #search-results, which funnily enough is our motto (or close enough).

Create a Variable in Google Tag Manager to extract the element

In GTM, go to Variables>User-Defined Variables>New to create a new variable.
Create a Dom Element variable with the Selection Method CSS Selector, and input your selector name. I’ve used #search-results in this case.

Create a lookup table variable to sort between “No Results” and “Results”

Create another new variable, this time choosing Lookup Table. Give it the Input Variable that you just created (I called it “Search Result”). Here, I am able to create a rule that says “if the CSS Selector #search-results says “No results found”, call this “No Results”, and if it doesn’t say that, call it “Results””. I do this by giving it one lookup value (match No results found to -> No results) and a default value of “Results”. Save your query

Bonus round – Create a variable to track the keyword used

You don’t have to do this, but it will add value to your report. I created a variable to extract the search query from the URL. You need to create a URL variable with the Query component type. Add your Query Key (which should be the same as the Query Parameter from Google Analytics). Save your query.

Create a trigger to fire on search results pages

Go to Triggers>New to create a new trigger. You need to choose a Page View – Dom Ready trigger. Choose Some DOM Ready Events. Create a rule for “url contains [search results page string]”. In my case, this is any page including /?s=.

Create an event tag to fire search results as an event to Google Analytics

Go to Tags>New to create a new tag. You need to choose a Universal Analytics tag (if you’ve not migrated to Universal Analytics… please do, I wrote a guide). You need to choose Track Type Event. I’ve given the events the Category: Search Used, the Action: {{Search Value Results}} (this is the lookup table we created earlier) and Label: {{page query}} (the keyword extractor variable). Set Non-Interaction Hit to True and configure your Google Analytics as you normally would.

Set the Trigger to the Search results trigger that we just created and save the tag.

PROTIP: Use the GTM Debugger to see if this works, and amend as required! Every website will be different.

Once tested, push the tag live.

Analyse your results using the event report

To see your new events, go to the Google Analytics report called Behaviour>Events>Top Events and search for the “Search Used” event category (or whatever you named it).
Click it and you can see what searches had results or not:

You can then click into Results or No Results to analyse which keywords came back with or without results.

And there you have it! Please enjoy your new data!

Related Posts