How to Remove the Empty Search from Analytics

When you look at your analytics on the Algolia dashboard, it is likely that one of the top-ranked queries is <empty search>. This search is often the top search if you use Algolia to provide a search-as-you-type experience to your users. This experience will trigger a search even before your user starts typing to instantly show results.

The <empty search> can tell you something about the relevance of your index, depending on your use case. For example, for a webshop it could be valuable to see how people interact with an empty search: it can indicate whether the products that show up first, on an empty search, are popular or not. For a blog, however, this metric would be close to useless. Most blogs sort their articles by publication date, so the <empty search> will give little insight into your relevancy.

It’s possible to exclude <empty search> - or any other given query - from your analytics. Before you decide to do so, please make sure that this query does not have any analytical value for your project.

Exclude Searches from Your Analytics

To exclude certain queries from your analytics, you have to pass the /doc/api-reference/api-parameters/analytics/ parameter with false as a value.

The following example shows you how to remove empty query searches from analytics, but the query condition can be modified to suit your needs.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
const algoliasearch = require('algoliasearch/lite');
const instantsearch = require('instantsearch.js').default;

import { searchBox, hits, pagination } from 'instantsearch.js/es/widgets';


const algoliaClient = algoliasearch(
  'YourApplicationID',
  'YourSearchOnlyAPIKey'
);

const searchClient = {
  search(requests) {
    console.log(requests)

    const newRequests = requests.map((request)=>{

      // test for empty string and change request parameter: analytics
      if(!request.params.query || request.params.query.length===0) {
        request.params.analytics=false
      }
      return request
    });

    return algoliaClient.search(newRequests);
  },
};

const search = instantsearch({
  indexName: 'Books',
  searchClient,
});

search.addWidget(
  searchBox({
    container: '#searchbox',
);

search.addWidget(
  hits({
    container: '#hits',
    templates: {
      item: `
        {{title}}
      `,
    },
  })
);

search.addWidget(
  pagination({
    container: '#pagination',
  })
);

search.start();

If you want to prevent sending empty queries altogether, you can disable a search on empty queries.

Did you find this page helpful?