Site icon Search Engine People Blog

How to Track Empty Search Results in Google Tag Manager

Stay Connected with Us!

...When You Can't Edit the Site!

Level of difficulty: 3.5/5

Have you ever wondered if people are searching for things on your website and getting NO RESULTS back?

If there's anything that says to a potential customer that you can't help them, it's showing them 0 search results and giving them no hope of finding something on your site when they do a search.

Ever wanted to know what those searches are??

Google Analytics has the great "Site Search" feature which you absolutely should be using on your site. But this feature won't easily tell you which search terms returned 0 results.

 

Time for a Case Study!

 
We had a client ask us to set this up for them recently, and to make it more fun, we could do no code changes at all to the website. Luckily, the client uses Google Tag Manager, which made this tracking possible.

Below are Step by Step Instructions on how to track searches with 0 results in Google Tag Manager & Google Analytics. (For more on Google Tag Manager, see How To Implement Google Tag Manager in Less Than 15 Mins)

 

Step 1 - Understand What We're Going To Do

Now, there are at least 2 ways to track pages with 0 search results. You can record a pageview with a search category in Google Analytics and use the Site Search reports, but you will be recording page views that didn't actually happen. Or, you can record the results as Events in Analytics, which only records events.

For various reasons, we chose to track the results as Events in Analytics for this client.

That's what these instructions will walk you through - how to track the keywords on your site that show No results to your visitors as Events in Analytics.

You are going to create 4 Macros, 2 Rules and 2 Tags. If you have a fair comfort with HTML, this will be easier than it looks. Go get a coffee now, before you start. 🙂

 

Step 2 - Create Your Macros

The first thing you need to do is to create your macros. That's assuming your Google Tag Manager is set up and you're using Universal Analytics.

Set up the following 2 Macros in your GA account. Hat Tip to Chris Van Patten for his great post on this. These 2 macros allow Tag Manager to access the data in the event. Without these macros, the event tracking doesn't work. So put them in!

gaEventAction (Macro)

Macro: gaEventAction

gaEventCategory (Macro)

 
Macro: gaEventCategory

ResultsPageTitle (Macro)

Now set up this Macro, which tells Tag Manager the name of the element on the page that identifies there are 0 results on the page. You will need to edit the text in green.

Macro: ResultsPageTitle

The text in green tells Tag Manager what text on the page to search for the "No Results Found" or "Sorry" message, or something that says no results were found. Use the div ID or similar, a class name is not enough. In this case, we located the "No Results Found" message in an ID called "content". So we entered content into our Macro.

Find the ID of your "No Results" Message in your HTML

URLQueryText (Macro)

This Macro tells Tag Manager the keyword that the visitor searched for. This is the keyword that generated 0 results. You'll want to know which keywords are driving customers away, so you'll want to set up this Macro.

Macro URLQueryText

You'll need to edit the text in green. This is the parameter in the URL that shows what the visitor searched for.

For example, if you do a search for "widgets" on your site, you should see it in your URL, and there should be a word or letter to the left of the equals sign. This word or letter is your search parameter. In this case, the parameter is "q", so we enter q into our Macro.

How to identify the query parameter
 

Step 3 - Create Your Rules

Now you need to set up Rules. These Rules are going to control when the tags are fired. It's easiest to set Rules up before the Tags.

Trigger Analytics Event (Rule)

This Rule is going to trigger our Event Tag to track an event in Google Analytics. No need to change anything, just copy this in directly.

Rule: Trigger-Analytics-Event

SearchPagesOnly (Rule)

This Rule is simply going to make sure this whole cascade of tags and rules happens only when a visitor is doing a search on the site, and not reading blog posts or randomly surfing.

Rule: SearchPagesOnly

You will need to edit the text in green, to match the page. Put in the page name, in this case, we used /search.

Identify the page URL
 

Step 4 - Create Your Tags

Finally, we come to the end - creating the Tags. This is where we pull everything together so it works.

 

Google Analytics Event Trigger (Tag)

This Tag is simply going to handle the actual event tracking. All you need to customize here is your UA number. Personally, I'm a big fan of putting the UA number in a Macro, so if you ever change your ID you can do it easily, which is what you see here. You can just type in your UA number if that's easier for now.

GA Event Trigger - part 1

Now you have to set up the Firing rule. Just add in the one you already created:

GA Event Trigger - part 2

Now simply copy in the Event Macros you set up. Just copy this in, no need to customize anything

GA Event Trigger - part 3
 

Tag: Track0Results (Tag)

Finally - this is the tag that pulls everything together and makes it happen. For this tag, you will need to create a Custom HTML tag.

Track0Results - part 1

And you'll need to create a Firing Rule.

Track0Results - part 2

Once this is done, all that's left to do is just copy and paste the text below into the box. The only thing you need to customize is the text that the script should look for. This is the message that Tag Manager should look for in the ID you set up in the ResultsPageTitle Macro.

Track0Results - part 3

Don't worry - you can just copy and paste it from here:

<script>
var str = {{ResultsPageTitle}};
var patt = new RegExp("No Results Found");
var res = patt.test(str);

if (res) {
dataLayer.push({
'event': 'gaTriggerEvent',
'gaEventCategory': '0SearchResult',
'gaEventAction': '{{URLQueryText}}'
});
} 
</script>

If you set everything up correctly, you should start tracking events in Analytics immediately. You can use the Real Time search in Analytics to make sure it's working.

Summary

At the end of the day, you should have a report that looks like the following, by clicking on the E-Commerce tab in the Event report. Your Event Category will be called "0SearchResult", and your keywords will be found in your Event Action.