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:


<li>Go to <B>Admin</B></li>

<li>Go to <B>View Settings</B></li>

<li>Go to <B>Site Search Setting</B></li>

<li>Tick <B>Site Search Tracking>On</B></li>

<li>Add your query parameter</li>

<li>Optional: Add categories if your site search uses categories</li>



<B>PROTIP: Create a Lowercase Filter for Search Term, it means that people searching for “SEO” and

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

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 <B>Variables>User-Defined Variables>New</B> to create a new variable.

Create a <B>Dom Element</B> variable with the Selection Method <B>CSS Selector</B>, 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 <B>Lookup Table</B>. 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 <B>URL</B> variable with the <B>Query</B> 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 <B>Triggers>New</B> to create a new trigger. You need to choose a <B>Page View – Dom Ready</B> trigger. Choose <B>Some DOM Ready Events</B>. Create a rule for “url contains ”. 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 <B>Tags>New</B> to create a new tag. You need to choose a <B>Universal Analytics</B> tag (if you’ve not migrated to Universal Analytics… please do, I wrote a guide). You need to choose Track Type <B>Event</B>. I’ve given the events the Category: Search Used, the Action: (this is the lookup table we created earlier) and Label: (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 <B>Behaviour>Events>Top Events</B> 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!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s