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

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

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



<pre class="lang-twig prettyprint-override"><code>{% set js %}
    ga('send', 'event', 'Search', 'Search', '{{ query|e("js") }}', {'nonInteraction': 1});
{% endset %}
{% includeJs js %}
</code></pre>

<p>If you’re still on the old ga.js, use this:</p>

<pre class="lang-twig prettyprint-override"><code>{% set js %}
    _gaq.push(['_trackEvent', 'Search', 'Search', '{{ query|e("js") }}', null, true]);
{% endset %}
{% includeJs js %}
</code></pre>

<p>There are other ways to include the JS, of course. (In our case we actually have a blank <code>analytics</code> 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 <code>{% includeJs %}</code> route is just the most unassuming.</p>

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

CC BY-SA 3.0
2014-11-05T01:13:27.823Z
2014-11-05T01:13:27.823Z
Brandon Kelly
<p>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.</p>

CC BY-SA 3.0
2014-11-04T23:26:44.673Z
2014-11-04T23:26:44.673Z
Douglas McDonald
<p>Completely untested, but may work in theory... <strong>Take a look into <a href="http://buildwithcraft.com/classreference/services/TemplatesService#hook-detail" rel="nofollow">template hooks</a>.</strong></p>

<p>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.</p>

CC BY-SA 3.0
2014-11-04T23:35:12.130Z
2014-11-04T23:35:12.130Z
Lindsey D
<p>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].(<a href="https://support.google.com/analytics/answer/1012264?hl=en" rel="nofollow noreferrer">https://support.google.com/analytics/answer/1012264?hl=en</a>)</p>

<p><a href="https://i.sstatic.net/dh84f.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/dh84f.png" alt="enter image description here"></a></p>

CC BY-SA 3.0
2018-01-11T23:13:25.653Z
2018-01-11T23:13:25.653Z
Ben Parizek
<p>Pretty much like the title says - What is the best way to track, record or view search terms when using the built in <a href="http://buildwithcraft.com/docs/templating/search-form" rel="noreferrer">Search form</a>?</p>

CC BY-SA 4.0
2020-10-04T17:56:40.807Z
We have been asked about this to the option to show recent searches and top searches. Anyone get anywhere with it?
Green17
<search><analytics>
What is the best way to track, record or view search terms?
# What is the best way to track, record or view search terms?

(Asked by: username_0 on 2014-11-04)

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](http://buildwithcraft.com/docs/templating/search-form)?

**Tags:** search, analytics
> **Comments:**
> **username_1**: We have been asked about this to the option to show recent searches and top searches. Anyone get anywhere with it?  

---

## 4 answers

### Answer by username_2 on 2014-11-05. Score: 9

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.


---

### Answer by username_3 on 2014-11-04. Score: 4

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.


---

### Answer by username_4 on 2014-11-04. Score: 3

Completely untested, but may work in theory... **Take a look into [template hooks](http://buildwithcraft.com/classreference/services/TemplatesService#hook-detail).**

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.


---

### Answer by username_5 on 2018-01-11. Score: 2

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](https://i.sstatic.net/dh84f.png)](https://i.sstatic.net/dh84f.png)


---

