Javascript Tagging Library: Track Queries
Overview
Instructions to send queries information using the Javascript Tagging Library.
Step-by-step guide
You can send query data to the dashboard using the trackQuery function.
trackQuery(terms, results, page, scope, options, callback)
Parameters
Parameter | Description | Data Type | Mandatory |
|---|---|---|---|
terms | The query typed by the user | String | Yes |
results | Number of results returned by the search engine | Integer | Yes |
page | The page number | Integer | Yes |
scope | This parameter is used for separate analytic data between two or more different scenarios | String | No* |
options | Object that may contain information about filters and other required data | Object | No |
callback | Function that will be executed after sending data | Function | No |
*This parameters are not mandatory for the service but necessary for the statistics | |||
The callback function receives an object with the following content:
{
totalHits: results,
page: page,
q: terms,
scope: scope,
referrer: document.referrer
}The callback also receives the parameter lang, user, session and the other params sent in options.
NOTE: The values for scope and options parameters will be provided by the EmpathyBroker Team.
Code samples
Normal Usage
Specifying a callback parameter
/**
* This example suppose that the provided scope is 'testscope' and the parameter to be included in the options parameter is the lang
* parameter with value 'en'
**/
var options = {
lang:'en'
}
empathyTAG.trackQuery('test', 10, 1, 'testscope', options,function(responseData){
alert('test');
});Without a callback (No functions will be executed after tracking the data)
/**
* This example suppose that the provided scope is 'testscope' and the parameter to be included in the options parameter is the lang
* parameter with value 'en'
**/
var options = {
lang:'en'
}
empathyTAG.trackQuery('test', 10, 1, 'testscope', options);Best practices
In most of the cases, you should implement one of these solutions to prevent receiving partial queries:
Prevent partial queries applying a delay before sending the trackQuery information, the delay time in the following example will be 2000 ms (2 seconds).
var _timeoutID; var options = { lang:'en' } window.clearTimeout(_timeoutID); _timeoutID = window.setTimeout(function(){ empathyTAG.trackQuery('test', 10, 1, 'testscope', options, function(responseData){ alert('test'); }); },2000);
Prevent partial queries filtering the query length to be considered as valid. For this example, it will be used 3 characters as minimal length.
//This variable should be filled with the query typed by the user var userQuery = 'test'; var _timeoutID; var options = { lang:'en' } if (userQuery.length > 3){ window.clearTimeout(_timeoutID); _timeoutID = window.setTimeout(function(){ empathyTAG.trackQuery('test', 10, 1, 'testscope',options, function(responseData){ alert('test'); }); },2000); }
Remember send queries when search engine returns 0 results. It's very important for future actions in search engine.
/** * This example suppose that the provided scope is 'testscope' and the parameter to be included in the options parameter is the lang * parameter with value 'en' **/ var options = { lang:'en' } empathyTAG.trackQuery('test', 10, 0, 'testscope',options);
Blank queries: Do not send a query without characters. Usually when users click in search button without write nothing in search box.
In this case be careful to not send the placeholder text (i.e. "Search...")