search mobile facets autocomplete spellcheck crawler rankings weights synonyms analytics engage api customize documentation install setup technology content domains user history info home business cart chart contact email activate analyticsalt analytics autocomplete cart contact content crawling custom documentation domains email engage faceted history info install mobile person querybuilder search setup spellcheck synonyms weights engage_search_term engage_related_content engage_next_results engage_personalized_results engage_recent_results success add arrow-down arrow-left arrow-right arrow-up caret-down caret-left caret-right caret-up check close content conversions-small conversions details edit grid help small-info error live magento minus move photo pin plus preview refresh search settings small-home stat subtract text trash unpin wordpress x alert case_deflection advanced-permissions keyword-detection predictive-ai sso

How to use Swiftype with Google Analytics

Google Analytics is a popular tool for tracking site-search analytics, and this tutorial will take you through the steps necessary to make sure searches through your Site Search powered search engine are tracked properly within your existing Google Analytics account.

Step 1: Make sure you're using the latest Google Analytics install code

Google Analytics has used a couple of different JavaScript-based install codes over the past few years.

You should make sure that you're using the latest "asynchronous loading" code, because it allows Swiftype to easily integrate with your analytics account. Also make sure that your Google Analytics install code is loaded before (above) your Swiftype install code on the page.

Beyond that, the asynchronous loading feature can improve the speed of your client-side page loads, so it generally provides an overall better experience for your users.

The latest version of the install code should look something like the following:


(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),

ga('create', 'UA-XXXX-Y', 'auto');  // Replace with your property ID.
ga('send', 'pageview');

Note: Swiftype will also work with the original ga.js.

For more information on using the Google Analytics tracking code, see Google's documentation here.

Step 2: Configure Site Search tracking for your Google Analytics account

Follow this sequence of steps to configure site-search tracking in your Analytics account. You can find additional details in Google's help center here (click on Setting Up Site Search for a View).

  1. Login to Google Analytics.
  2. Switch to the correct dashboard.
  3. Click the gear icon located in the lower left corner to open up ADMIN.
  4. Under the third column, click View Settings.
  5. Enable Site Search Tracking.
  6. In the Query Parameter field:
    • If you use the Swiftype install code: enter stq
    • If you use the Swiftype WordPress plugin: enter s
  7. Click Done!

When you are finished, your profile settings screen should look like the following. Keep in mind that Google Analytics site-search tracking can take up to 48 hours to start appearing in your dashboard.

Using a Client Library?

If you are using a client library, you will need to pass a virtual pageview to Google Analytics. You can read the documentation from Google here.

This example JavaScript might be enough to get you started:

var url = window.location.href;
var re = new RegExp("\#stq=(.+)");
var m = url.match(re);

if (m != null) {
    var track = "/search?stq=" + m[1];
    ga('send', 'pageview', track);
} else {
    ga('send', 'pageview');

You may need to modify the regular expression within the re variable as well as the root path within the track variable. Do note that the above example is not compatible with Google Tag Manager.