Google Analytics

To track searchphrases entered by your customers, the analytics option should be enabled by us.

Alternatively, you can override the configuration of the JS Implementation using JavaScript in your storefront using the analytics option.

Every time a search term is entered, Tweakwise JS will send an event to Google Tag Manager. Google Tag Manager can be configured to send this event to Google Analytics.

🚧

This article assumes you already have Google Tag Manager integrated in your platform. For more information on how to do this click here.

Configuring Google Tag Manager

Follow the steps below to configure Google Tag Manager to send searchphrases to Google Analytics,

Step 1: Create a variable

In Google Tag Manager, create a new variable called SearchTerm. This variable should be of type Data Layer Variable and have query as its Data Layer Variable Name.

Step 2: Create a trigger

Next, create a new trigger called Search. This trigger should be of type Custom Event and have Search as its Event name.

Step 3: Create a second trigger

A second trigger should be created with the name Search.timed. It should be of type Custom Event and have Search.timed as its Event name.

Step 4: Create a tag

Next, create a new tag called Trigger search after 3s. This tag should be of type Custom HTML and have the (previously created) Search trigger as its firing trigger. The content of the HTML should be as follows:

<script>
  (function() {
    clearTimeout(window.twSearchTimer);

    window.twSearchTimer = window.setTimeout(function() {
      window.dataLayer.push({
        'event': 'Search.timed',
        'query': {{SearchTerm}}
      });  
    }, 3000);
  })();
</script>

The final tag should look like this:

Step 5: Create a second tag

Lastly, you need to create a new tag called Search. It should have a type of Google Analytics: GA4 Event.

Set a configuration tag or the measurement ID to connect it to your Google Analytics account.

The tag should have an event name of search and it should contain a parameter called search_term that has the value of the (earlier created) SearchTerm variable.

Step 6: Publish

The only thing that is left is publishing your Google Tag Manager environment. After that, every time a user enters a search term a search event will be sent to your Google Analytics environment. This event will have a parameter called search_term which can be used to keep track of the search phrases.