Site Search Tracking without Query String Parameters

One incredible thing that you can (and should) track within Google Analytics is what your visitors are typing into your internal site search. Collecting and then giving thoughtful consideration to this data can teach you important lessons about how to improve your site and/or where it’s falling short.

For many sites, setting up internal site search tracking is easy.

  1. Log in to Google Analytics and navigate to the “View” that you want to set up site search tracking for.
  2. Turn “Site search Tracking” ON
  3. Enter the “Query parameter” that your site uses when performing a search. To find this, just search for something in your site’s internal search. The URL of the results page should look something like mysite.com/search?q=portland. “q” is what you put into Google Analytics.

google analytics site search tracking config with query parameter

Websites Without a Site Search Query Parameter

But one thing we’re seeing at UpBuild, is that more and more sites are being built (intentionally on unintentionally) with an internal site search function that doesn’t use a query parameter. What’s an analyst to do?!

Here’s what our site search URLs look like on upbuild.io.

http://www.upbuild.io/search/portland

Since we don’t have any query parameters, we have nothing to drop into the Google Analytics configuration. Does that mean we can’t track site searches?

Not at all! Google is kind enough to provide some insights for how to configure POST-based search engines to work with their site search tracking feature. The options they suggest are these:

  1. Configure the application to append query parameters to the end of internal search URLs
  2. Customize (i.e., hack) the basic Google Analytics tracking code on your search results page so that it sends a virtual URL (rather than the original) that provides the search term in a query string format.

I’m not a fan of these two options for two reasons. First, either option is going to require time or money in development work (unless you’re the one running analytics AND site development and you have free reign to change whatever you want whenever you want). Second, hacking the basic Google Analytics tracking code isn’t an option if you’re using Google Tag Manager to deliver your page view tracking code.

Even though I’m not sold on either of the options that Google spells out, I really like the idea behind the second one: creating a virtual URL that uses a query parameter for each internal SERP and sending that to Google Analytics in lieu of the actual URL. If we could find an elegant way to do this with Google Tag Manager, we wouldn’t need to spend any time or money with extra dev work and we wouldn’t need to modify/hack the GA page view tracking code on internal SERPs. Win win.

Site Search Tracking in Google Tag Manager Using Virtual Page views

So now, our goal is to use Google Tag Manager to track page views for internal SERPs in a special way; one where we’re sending a URL like /search/?q=portland to Google Analytics rather than /search/portland. Furthermore, we need to set it up so that page views still track normally on the rest of the site. Here’s our plan of attack:

  1. Create a Trigger in Google Tag Manager that returns “TRUE” if the current page is an internal search results page. This can be used to trigger special tracking. Let’s call it “Internal SERP Trigger”.
  2. Add an “Exception” to our existing page view tracking tag. Use “Internal SERP Trigger” as a negative condition of firing the tracking tag. IF “Internal SERP Trigger” = TRUE, exclude the normal page view tracking tag and include the special internal SERP tracking.
  3. Create a new JavaScript Variable in Google Tag Manager that dissects the current URL and pulls out the query parameter. Let’s call this “Internal SERP Query Parameter”.
  4. Create a new page view tracking tag that overwrites the page view URL with a URL that incorporates “Internal SERP Query Parameter”. (Optional) While we’re doing this, we can also manually set a page title for our SERP page like “Internal Search”.
  5. Preview, QA/debug, and Publish.
  6. Configure Site Search in GA and watch the data come pouring in.

Now that we know what we need to do, let’s dig into how to perform each step.

1) Create the Internal SERP Trigger

Create a new Trigger in Google Tag Manager called “Internal SERP Trigger”. This Trigger is basically a condition that we want to be “TRUE” whenever we’re on an internal SERP. Select “Page View” as the event, “Page View” as the trigger type, and set it to fire when “Page Path starts with /search/”.

gtm internal serp trigger

2) Configure an Exception on Standard Page View Tracking

Under Tags, find your existing page view tracking tag. Edit and, all the way at the bottom, click on “Create Exceptions” and select “Internal SERP Trigger” from the list of options. This will allow your current tracking to fire on every page EXCEPT the internal search pages.

gtm exception trigger

3) Create the Internal SERP Query Parameter Variable

Create a new Variable in Google Tag Manager. Title it “Internal SERP Query Parameter” and select “Custom JavaScript” as the type. For the actual JavaScript, we’ll use the following. Hopefully, you’re familiar with JavaScript, but in case you’re not, I hope my comments in the code give you an idea of what’s going on.

function thistest() {
  // find and store the path of the current page
  var pagePath = location.pathname;
  // if the path starts with /search/, we know it's an internal SERP
  if (pagePath.indexOf("/search/") === 0) {
    // if the above checks out, let's figure out where the search term begins in the URL. Then, let's use that starting point to pull out the search term itself.
    var searchString = pagePath.substring(pagePath.lastIndexOf("/") + 1);
    // return what we found to GTM as the value of our variable
    return searchString;
  }
  // if it's not an internal SERP page, return "none"
  else {
    return "none";
  }
}

See the Pen Internal SERP Query Parameter by Mike Arnesen (@mike_arnesen) on CodePen.

Once we have this variable created, we’ll have a “puzzle piece” that we can use later on. When we’re on a search results page, the value will be whatever was just searched. If we’re not on a search page, the value will simply be “none”.

gtm search parameter javascript

4) Create the Internal Search Virtual Page View Tracking Tag

Navigate to your normal page view tracking tag once more and click “Copy” at the bottom to create a duplicate tag based on what you already have configured. I like to preface any virtual page view tags with “vpv”, so I’ll rename this tag “vpv – Internal Search Page Views”.

Under the “Configure Tag” section, click on “More Settings” to expand that section and then click on “Fields to Set”. You’ll want to add two new fields. The first will have “title” as the Field Name and “Internal Search” (or whatever you prefer) as the Value (note that this one is optional). The second field…

THIS IS WHERE THE MAGIC HAPPENS! 

The second field should set “page” as the Field Name (this is the URL that gets passed to Google Analytics). For the Value (and this is where it gets real), we’re going to type out “/search/q=” and then end with “{{Internal SERP Query Parameter}}”. The double curly braces tells GTM that we want to reference a variable. The full value would then be…

/search/q={{Internal SERP Query Parameter}}

So guess what happens. I’ll give you a second.

When this page view tag fires on an internal search page, GTM will determine the value of {{Internal SERP Query Parameter}} on the fly, add it to the static portion, and immediately send off this dynamically generated URL to Google Analytics instead of the actual URL! Without changing any code or functionality on the site itself, we now have trackable site search!

virtual page view for site search

DON’T FORGET: The last thing we need to do here is clear out our “Fire On” firing rules and add new ones. Delete what’s currently set under the “Fire On” section and then click on “Some Page” and select “Internal SERP Trigger”.

5) Preview, QA/Debug, and Publish

Previewing and debugging GTM is a whole other (and long) post, so I won’t get into it here. However, take the time to figure out how to make sure things are going to fire as expected once you click that “Publish” button. One of the coolest things about Google Tag Manager is the freedom it gives you to test and fine tune for as long as you want before ever pushing any tracking out to your site visitors. Don’t rush it. Get it right.

6) Configure and Get Data

If you haven’t already, you’ll need to set up Site Search tracking for your Google Analytics “View”. In the Admin section of Google Analytics, turn Site Search tracking on and give GA the parameter you’ve set up (“q”, in this case).

Now it’s time to cross your fingers and wait for the data to come in! It can take a while for data to show up in the site search report (Behavior > Site Search in Google Analytics), but be patient and you’ll start seeing it populate shortly!*

*Note that if you’re filtering out traffic from your IP address (which you should be doing!), you’ll need to look at an unfiltered view if you want to see your test queries. Otherwise, you’ll have to wait until actual users start using your search functionality.

That’s all there is to it. I hope this post helped you in some way or sparked some other idea in your head for how you can solve a tough problem using the web tools you have at your disposal. Drop me a comment below if you have any trouble getting this to work on your site!

Happy optimizing!

Written by
Mike founded UpBuild in 2015 and served as its CEO for seven years, before passing the torch to Ruth Burr Reedy. Mike remains with the company today as Head of Business Operations.

Related Posts

Leave a Reply

Your email address will not be published.