onUniversalSearch
The onUniversalSearch attribute accepts a function. It allows you to send an analytics event each time a search is run on a universal page. This function should take in one parameter, searchParams, which contains information about the search, and return the desired analytics event.
Like all Answers JavaScript API Library analytics, this will only work if there is a businessId in the ANSWERS.init.
Usage
The search information exposed in searchParams is shown below.
function (searchParams) => {
/**
* The string being searched for.
* @type {string}
*/
const queryString = searchParams.queryString;
/**
* The number of verticals returned.
* @type {number}
*/
const sectionsCount = searchParams.sectionsCount;
/**
* A map containing entries of the form:
* { totalResultsCount: 150, displayedResultsCount: 10}
* for each returned vertical. The totalResultsCount indicates how many results
* are present in the vertical. The displayResultsCount indicates how many of
* those results are actually displayed.
* @type {Object<string,Object>}
*/
const resultsCountByVertical = searchParams.resultsCountByVertical;
let analyticsEvent = new ANSWERS.AnalyticsEvent('ANALYTICS_EVENT_TYPE');
analyticsEvent.addOptions({
type: 'ANALYTICS_EVENT_TYPE',
label: 'Sample analytics event',
searcher: 'UNIVERSAL',
query: queryString,
sectionsCount: sectionsCount,
});
return analyticsEvent;
},
}),
onVerticalSearch
The onVerticalSearch configuration accepts a function. It allows you to send an analytics event each time a search is run on a vertical page. This function should take in one parameter, searchParams, which contains information about the search, and return the desired analytics event.
Like all Answers JavaScript API Library analytics, this will only work if there is a businessId in the ANSWERS.init.
Note:
queryStringwill be undefined for searches initiated by pagination or by applying facets, filters, or sorts — even if a query is being used.
Usage
The search information exposed in searchParams is shown below.
function (searchParams) => {
/**
* Vertical key used for the search.
* @type {string}
*/
const verticalKey = searchParams.verticalKey;
/**
* The string being searched for.
* @type {string}
*/
const queryString = searchParams.queryString;
/**
* The total number of results found.
* @type {number}
*/
const resultsCount = searchParams.resultsCount;
/**
* Either 'normal' or 'no-results'.
* @type {string}
*/
const resultsContext = searchParams.resultsContext;
let analyticsEvent = new ANSWERS.AnalyticsEvent('ANALYTICS_EVENT_TYPE');
analyticsEvent.addOptions({
label: 'Sample analytics event',
searcher: 'VERTICAL',
query: queryString,
resultsCount: resultsCount,
resultsContext: resultsContext,
});
return analyticsEvent;
},
}),