Pretty much like the title says - What is the best way to track, record or view search terms when using the built in Search form?

  • We have been asked about this to the option to show recent searches and top searches. Anyone get anywhere with it?
    – Green17
    Commented Oct 4, 2020 at 17:56

4 Answers 4


We track search terms on buildwithcraft.com the same way we track all other site activity: using Google Analytics.

All you have to do is throw GA event tracking code into your search results template.

If you’ve upgraded to Universal Analytics/analytics.js, use this:

{% set js %}
    ga('send', 'event', 'Search', 'Search', '{{ query|e("js") }}', {'nonInteraction': 1});
{% endset %}
{% includeJs js %}

If you’re still on the old ga.js, use this:

{% set js %}
    _gaq.push(['_trackEvent', 'Search', 'Search', '{{ query|e("js") }}', null, true]);
{% endset %}
{% includeJs js %}

There are other ways to include the JS, of course. (In our case we actually have a blank analytics block right alongside other Google Analytics instantiating code in the layout template, and set this JS code by overriding that block on templates that have custom things to track.) This {% includeJs %} route is just the most unassuming.

With that code in place, you’ll be able to start tracking “Search” events from Behavior > Events in your Google Analytics dashboard.


No built in way that I am aware of, but you could send the search query off to a custom plugin controller from your search template using ajax, which could either save it to a custom table (i.e. search_cache), or log them to a log file.


Completely untested, but may work in theory... Take a look into template hooks.

When the template loads, it will ping your hook and provide whatever Twig variables have already been set. Your custom plugin will have a chance to then record your search terms as needed.


If your search page uses a query string, it's possible to enable search tracking from directly within Google Analytics. More details on Site Search [here].(https://support.google.com/analytics/answer/1012264?hl=en)

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.