Fix Google Analytics site search tracking with Google Tag Manager
Tracking site search is pretty difficult if there are no query parameters in the URL.
In this tutorial, I give you three "classic" site search tracking problems, and a solution for each:1) Search term as a folder in URL
, which retrieves the search term from the URL path, and appends it as a query parameter to the Document Path of your pageview tag.2) Search term in a DOM element on the page
In this scenario, there is no search term in the URL. However, the keyword is displayed in an on-page element, such as a DIV or a SPAN with an ID attribute. Here I use the DOM Element Macro
to retrieve the search term and append it as a query parameter in the Document Path of the pageview tag.3) No search term anywhere on the page
This sucks, but I've seen it. The trick is to use a Form Submit Listener
and a DOM Element Macro
to grab the search term from the form field, then to save it as a browser cookie
using a Custom HTML Tag
, then to use this cookie to append the Document Path with a query parameter in the pageview tag, and finally to delete the cookie using a callback
macro in the pageview tag.
Even if you have no need for site search fixing, the tips in this post should highlight the flexibility of Google Tag Manager, once again. For example, you will never need a second pageview tag for pages with the virtual document path, since you can utilize the "return;" statement in your Custom JS macros to not send the Document Path if a search was not made. Thus the original path of the page is preserved if no search is made, meaning you only need one pageview tag! #gde #googleanalytics #blogpost