Google Analytics site search feature is very useful and it is based on search query params. But what if your app does not use search query param to perform search operation?

Here we will explain how to use Google Analytics search tracking by using dataLayer and Google tag manager.

 

 

 

 

Site search is very valuable data which can give you more insight what your users are searching on your site and it can help you to decide which content you can create in order to give your users what they are searching for.

 

 

In order to track what are the search terms used by our visitors we will perform these steps:

  •  create custom event every time when search is fired in our app and push custom event to dataLayer
  • create GTM trigger based on that custom event and fire GA tag
  • Enable Site search Tracking in Google Analytics and use our event to track the search

 

OK, first thing first. Let's create our search event and push data to dataLayer.

In your JS app when user enters the search, or when you get the results from backend (it is up to you) place this piece of code:

                    window.dataLayer.push({
      'event' : 'Search',
      'searchTerm' : value
    });
                  

where value is search term entered by the user.

 

Next, let's collect this value and make a trigger in Google tag manager.

Open your GTM and go to triggers, click on new, select Data Layer variable and give the name to your trigger: dlv-searchTerm

In trigger settings enter searchTerm  in Data Layer Variable Name field. This value comes from our custom event push to dataLayer.

 

 

 

Next let's create trigger based on this trigger. Go to GTM Triggers section, click on new and select custom event.

Enter the name searchEvent and in event name enter Search  this is our event name pushed by JS to dataLayer (in first code example above).

 

 

OK, now we can create our Google Analytics tag to be fired based on this event and use variable which will collect search term from dataLayer and pass it to GA.

 

Go to Tags section in GTM click on new and select  Google Analytics: Universal Analytics and give it  a name: GA search page view

Select Page view  as Track Type, enable checkbox next to Enable overriding settings in this tag  and in section Filed to Set set new field named page and set  the value

/pagesearch?search={{dlv-searchTerm}}  where dlv-searchTerm is the variable defined in second step. This is telling GA that we are sending custom page url with query param with our search value.

 

Last step is to enable Site search tracking in Google Analytics. Go to GA admin section, select View Settings and click on Site search Tracking to enable it. In query param enter search which is our query param name defined in Google tag manager, save your settings and you are done.

 

Now you can get the search data in Google Analytics which you can find in Behavior->Site Search section