Remi Sture
Remi Sture

Reputation: 12998

How to measure search query and hits?

I have a page with search functionality and would like to use Google Analytics to measure both the search query/term and the number of hits found. What would be the best way of doing that?

Example:

If a user searches for "Football" and gets 32 results, I want to store both "Football" and "32", and be able to create reports like this:

# | Word | Number of searches | Total hits | Average number of hits
1 | Football | 3 | 96 | 32
2 | Basketball | 2 | 12 | 6
3 | Snowboard | 6 | 6 | 1

I have looked into custom dimensions and metrics, but it's not clear to me how to actually achieve what I want.

Any help and example code would be much appreciated.

Remi


Solution:

function measureSearchResult(query, hits) {
    if (query) {
        ga('send', 'event', {
            'eventCategory': 'Search',
            'eventAction': 'Hits',
            'eventLabel': query,
            'eventValue': hits,
            'hitCallback': function () {
                console.log('*** Google Analytics - search logged: "' + query + '", ' + hits + ' hits');
            }
        });
    }
}

Upvotes: 1

Views: 255

Answers (2)

Linda Lawton - DaImTo
Linda Lawton - DaImTo

Reputation: 117146

You could enable the site search in your google analytics account.

You can find it under admin -> View (profile) -> view settings -> site search settings.

You will need to enable it and tell it what the query parameter is for your sight.

https://support.google.com/customsearch/answer/1321536?hl=en

Once you have done that you will be able to Track the search terms used as visits and page views in the Behavior -> site search -> usage reports. Just remember its going to take about 24 hours before you will be able to see the Site search data in these reports after you enable it.


Update in response to comment below

If you are using Univrsal analytics you can log it as a custom dimension. Which is probably the best way of going. @mrSponge's solution uses eventTracking and his example of event tracking isn't using Universal analytics. I sugest you figure out first if you are runing Clasic analytics or Universal Analytics. Then you can decided which option will be better suited to your needs. I have a short tutorial on Custom Dimensions here http://www.daimto.com/custom-dimensions-and-metrics/

Fastest way to tell if you are running Clasic analytics or Universal analytics is to search for .push if you see that then your running classic analytics. If your analytics tracking code starts with UA- then you could run universal analytics you just need to change from using the ga.js to the analytics.js code in your site.

If your intersted in checking out the event tracking for universal analytics you can read about it here: https://developers.google.com/analytics/devguides/collection/analyticsjs/events

Upvotes: 2

MrSponge
MrSponge

Reputation: 868

You could capture the number of results as an event and then put together a customized report to reflect what you are trying to achieve.

I am not aware on what type of language you are using and how you'd exactly code it, but for Google Analytics to see the event, you'd use something like:

_gaq.push(['_trackEvent', 'Search', 'Hits', '','value']);

Or if you do not want to add a value, you could populate the third one with the number instead.

_gaq.push(['_trackEvent', 'Search', 'Hits', 'value-here-instead']);

The anatomy of event tracking in the example above, would capture an event where it has Category set to Search, Action set as Hits and its label would be set to the value you have.

Please note that the example above is for the Classic Analytics, ga.js, and doesn't work with Universal Analytics, analytics.js. The syntax is slightly different, but the idea stays the same.

Upvotes: 1

Related Questions