TransWikia.com

What is the best way to track, record or view search terms?

Craft CMS Asked by Adam McCombs on December 15, 2020

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?

4 Answers

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.

Answered by Douglas McDonald on December 15, 2020

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.

Answered by Lindsey D on December 15, 2020

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.

Answered by Brandon Kelly on December 15, 2020

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

Answered by Ben Parizek on December 15, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP