How to extract site search terms from subfolders in Google Analytics

Author avatar

Emma Barnes

Google Analytics has a lot of really nice features. This post will combine two of them: filters and internal site search. I am a big fan of monitoring internal site search queries. Google gives good guidance for query parameter-based search and for post-based searches, but there is little to no guidance on how to extract site search data for site searches that generate brand new subfolders (or subdirectories, if you’d rather).

You will need:

  1. Admin access to Google Analytics
  2. A site whose internal site search generates subfolders
  3. A testing profile
  4. Use of advanced filters (example below)
  5. An understanding of RegEx (example below)

‘A site whose internal site search generates subfolders’: What does that even mean?

I got the idea for this blog post because one of our clients has an internal site search that generates URLs that look like these, where the search query (“foobar”) is within its own subfolder:

https://www.example.com/search-results/foobar/1/

This kind of site search generation is not supported by Google Analytics by default, so this post is a tutorial for how to generate a report that looks like this:

Search Results

Write RegEx to replicate your internal site search URL structure

Using the example above, their URL structure for internal site searches looks like this:

/search-results/[search_term]/[page_number]/

Our end goal is to extract the search term from the second subfolder. To do this, we need to write some RegEx (I have a few good references in another post) that separates the non-search components from the search terms. This can be done by using round brackets () to group parts of the URL together.

So the example above translates to:
(^/search-results/)(.*)(/[0-9]*/$)

Which says:

  • The first section begins with “/search-results/” (denoted by ^)
  • The second section can be any string of characters (denoted by .*)
  • The final section is a number (denoted by [0-9]*/$)

This can modified depending on how your internal site search folder structure looks.

Use an advanced filter to extract the search term

In Google Analytics, you need to create a new advanced filter to extract the search term from the URL:

Untitled.jpgGoogle Analytics Filters

PRO TIP: Always test a new filter on a testing profile before adding it to your main reporting profile.

An advanced filter in Google Analytics works using one or two fields to extract data from (input fields) and combine it into a final field (output field).

Google Analytics reads input fields as RegEx groups, which is why we wrote the RegEx as we did above. Google will then translate your groups in “A1, A2, A3…” and “B1, B2, B3…”. In this example, “/search-results/” becomes “A1”, the search term becomes “A2” and the page depth becomes “A3”. We do not have any “B” terms as we are only interested in extracting data from one dimension.

You then specify what you want to extract, and in this case it is that second group within the RegEx. We want to extract “A2” and implement it as a search term.

This results in a filter that looks like this:

Advanced Filter Google analytics

This says:

    1. Look for URLs (request URI) structured as defined in the RegEx [Field A->Extract A]
    2. Extract the second part of that URL [$A2]
    3. Place this as your search term [Output to ->Constructor]

Save your filter.

Now if you go to your Behaviour>Site Search>Search Terms report, you should have a list of terms that people are searching for on your site.

Bath

+44 (0) 1225 480 480

20 Manvers Street

Bath

BA1 1JW

Leeds

+44 (0) 113 260 4010

2nd floor, 2180 Century Way,

Thorpe Park,

Leeds, LS15 8ZB.

London

+44 (0) 113 260 4010

5th Floor, Cordy House,

91 Curtain Road

London, EC2A 3BS

Part of the St. Ives Group

  • By pressing submit you consent for Edit to contact you via your email or telephone number for purposes relevant to your request for our goods or services. Your contact details, including your name, company, telephone number and email address will be used by Edit. By contacting you are agreeing to Edit’s Privacy Policy. If you have any questions, please ensure you review this section before submitting.

  • This field is for validation purposes and should be left unchanged.

© 2018 Edit. St Ives Group. Company reg. no. 3624881, All rights reserved. VAT Registered GB 927458295 Privacy Policy | Terms & Conditions | Cookie Policy