Skip to content

Commit d08ce88

Browse files
authored
Merge pull request #4 from AddSearch/stats-v2
Stats v2
2 parents 0ade932 + f1f2f93 commit d08ce88

File tree

3 files changed

+133
-79
lines changed

3 files changed

+133
-79
lines changed

README.md

+93-61
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,12 @@ client.suggestions('a', callback);
7070
client.setSuggestionsSize(20);
7171
```
7272

73-
#### Fetch custom field autocompletion
73+
#### Custom field autocompletion
7474
Custom fields autocomplete can be used for predictive search. For example, product names or categories can be
7575
suggested as the keyword is being typed in.
7676
```js
77-
// Fetch custom field values starting with a specific prefix. In this example, results could be "adidas, apple, azure"
77+
// Fetch custom field values starting with a specific prefix In this example, fetch records
78+
// starting with *a* from the *custom_fields.brand* field. Results could be "adidas, apple, azure"
7879
client.autocomplete('custom_fields.brand', 'a', callback);
7980
```
8081

@@ -86,57 +87,36 @@ client.setAutocompleteSize(20);
8687

8788
#### Search with fuzzy matching
8889
```js
89-
// Enable/disable fuzzy matching. Possible values true/false/"auto" (default: "auto")
90-
client.setFuzzyMatch(false);
90+
// Control fuzzy matching used for typo-tolerance
91+
// Possible values true/false/"auto" (default: "auto")
92+
client.setFuzzyMatch(false);
9193
```
9294

93-
#### Collect analytics
95+
### Pagination
96+
Set page number, page size and sorting parameters. It's possible to order results by:
97+
- relevance (descending)
98+
- date (ascending or descending)
99+
- custom field value (ascending or descending. E.g. *custom_fields.price*)
94100
```js
95-
// Control whether search queries are sent to your AddSearch Analytics Dashboard or not (default: true)
96-
client.setCollectAnalytics(false);
101+
// Defaults: page "1", pageSize "10", sortBy "relevance", sortOrder "desc"
102+
client.setPaging(page, pageSize, sortBy, sortOrder);
97103
```
98104

99-
#### Send click event to analytics
100-
When a search results is clicked, send the event to your AddSearch Analytics Dashboard. Information on clicks is used
101-
in your statistics and in the self-learning search algorithm.
102-
```js
103-
// Docid is the 32-character long id that is part of each hit in search results
104-
// Position is the position of the document that was clicked, the first result being 1
105-
client.searchResultClicked(docid, position);
106-
```
105+
Other functions.
107106

108-
#### Set JSON Web Token (for authentication)
109107
```js
110-
// Add JWT to the search request (if protected search index)
111-
client.setJWT(token);
112-
```
108+
// Next page (call search function to fetch results)
109+
client.nextPage();
113110

114-
#### Set user token (for personalized search results)
115-
```js
116-
// Add a user token to the search request (if personalization in use)
117-
client.setUserToken('uuid');
111+
// Previous page
112+
client.previousPage();
118113
```
119114

120-
#### Send personalization events with search query
121-
In personalized search, user events are typically sent to AddSearch via API and a user token
122-
is passed with the search query (see setUserToken function).
123-
An alternative way is to send user events needed for personalization with the search query.
124-
125-
```js
126-
// Events depend on the personalization strategy
127-
// Contact AddSearch for more information
128-
var events = [
129-
{favorite_genre: 'rock'},
130-
{favorite_band: 'Red Hot Chili Peppers'},
131-
{least_favorite_genre: 'country'}
132-
];
133-
134-
client.setPersonalizationEvents(events);
135-
```
115+
### Filters
136116

137117
#### Define language filter
138118
```js
139-
// Documents in specific language (e.g. "en" or "de")
119+
// Fetch documents in specific language (e.g. "en" or "de")
140120
client.setLanguage('en');
141121
```
142122

@@ -194,34 +174,14 @@ var filter = {
194174
client.setFilterObject(filter);
195175
```
196176

197-
#### Manage paging
198-
Set page number, page size and sorting parameters. It's possible to order results by:
199-
- relevance (descending)
200-
- date (ascending or descending)
201-
- custom field value (ascending or descending. E.g. *custom_fields.price*)
202-
```js
203-
// Defaults: page "1", pageSize "10", sortBy "relevance", sortOrder "desc"
204-
client.setPaging(page, pageSize, sortBy, sortOrder);
205-
```
206-
207-
Other functions.
208-
209-
```js
210-
// Next page (call search function to fetch results)
211-
client.nextPage();
212-
213-
// Previous page
214-
client.previousPage();
215-
```
216-
217177
#### Set result type
218178
```js
219179
// By default, fetch all search results
220180
// If "organic", Pinned results and Promotions are left out
221181
client.setResultType('organic');
222182
```
223183

224-
#### Facets
184+
### Facets
225185
```js
226186
// Declare fields for faceting. Number of hits found from
227187
// these fields will be returned
@@ -234,6 +194,72 @@ Use the following function to get more or less facets.
234194
client.setNumberOfFacets(20);
235195
```
236196

197+
### Search analytics
198+
#### Send search event to analytics
199+
When search is executed, send the event to your AddSearch Analytics Dashboard.
200+
```js
201+
// If the numberOfResults is 0, the search is shown in the list of "queries with no hits"
202+
client.sendStatsEvent('search', keyword, {numberOfResults: n});
203+
```
204+
205+
#### Send click event to analytics
206+
When a search results is clicked, send the event to your AddSearch Analytics Dashboard. Click information is shown
207+
in your statistics and used by the self-learning search algorithm.
208+
```js
209+
// documentId is the 32-character long id that is part of each hit in search results.
210+
// position is the position of the document that was clicked, the first result being 1
211+
client.sendStatsEvent('click', keyword, {documentId: id, position: n});
212+
```
213+
214+
#### Set or get stats session ID
215+
Control the search session ID manually. Search queries with the same ID are grouped on the Analytics Dashboard.
216+
For example, in a search-as-you-type implementation the final keyword of a given session is shown.
217+
```js
218+
client.getStatsSessionId();
219+
client.setStatsSessionId(id);
220+
```
221+
222+
#### Collect search events automatically
223+
Send search events automatically to the Analytics Dashboard. Not recommended in search-as-you-type implementations,
224+
as every keystroke would fire a statistics event
225+
```js
226+
// Control whether search queries are sent to your AddSearch Analytics Dashboard automatically or not (default: true)
227+
client.setCollectAnalytics(false);
228+
```
229+
230+
### Personalization
231+
232+
#### Set user token (for personalized search results)
233+
```js
234+
// Add a user token to the search request (if personalization in use)
235+
client.setUserToken('uuid');
236+
```
237+
238+
#### Send personalization events with search query
239+
In personalized search, user events are typically sent to AddSearch via API and a user token
240+
is passed with the search query (see setUserToken function).
241+
An alternative way is to send user events needed for personalization with the search query.
242+
243+
```js
244+
// Events depend on the personalization strategy
245+
// Contact AddSearch for more information
246+
var events = [
247+
{favorite_genre: 'rock'},
248+
{favorite_band: 'Red Hot Chili Peppers'},
249+
{least_favorite_genre: 'country'}
250+
];
251+
252+
client.setPersonalizationEvents(events);
253+
```
254+
255+
### Other
256+
257+
#### Set JSON Web Token (for authentication)
258+
```js
259+
// Add JWT to the search request (if protected search index)
260+
client.setJWT(token);
261+
```
262+
237263
## Supported web browsers and node.js versions
238264
The client is tested on
239265
- Chrome
@@ -251,6 +277,12 @@ To modify this client library, clone this repository to your computer and execut
251277
npm install
252278
```
253279

280+
#### Code
281+
Re-compile automatically when source files are changed
282+
```sh
283+
npm run watch
284+
```
285+
254286
#### Run tests
255287
```sh
256288
npm test

package.json

+8-8
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,16 @@
4141
},
4242
"devDependencies": {
4343
"@babel/cli": "^7.8.4",
44-
"@babel/core": "^7.8.4",
45-
"@babel/preset-env": "^7.8.4",
46-
"@babel/register": "^7.7.7",
44+
"@babel/core": "^7.8.6",
45+
"@babel/preset-env": "^7.8.6",
46+
"@babel/register": "^7.8.6",
4747
"babel-loader": "^8.0.6",
4848
"esm": "^3.2.25",
49-
"fetch-mock": "^8.3.2",
50-
"mocha": "^6.2.2",
49+
"fetch-mock": "^9.0.0",
50+
"mocha": "^7.1.0",
5151
"node-fetch": "^2.6.0",
52-
"uglify-js": "^3.7.7",
53-
"webpack": "^4.41.5",
54-
"webpack-cli": "^3.3.10"
52+
"uglify-js": "^3.8.0",
53+
"webpack": "^4.42.0",
54+
"webpack-cli": "^3.3.11"
5555
}
5656
}

src/index.js

+32-10
Original file line numberDiff line numberDiff line change
@@ -101,19 +101,41 @@ var client = function(sitekey) {
101101
this.setShuffleAndLimitTo = function(shuffleAndLimitTo) { this.settings.setShuffleAndLimitTo(shuffleAndLimitTo); }
102102
this.setFuzzyMatch = function(fuzzy) { this.settings.setFuzzyMatch(fuzzy); }
103103
this.setCollectAnalytics = function(collectAnalytics) { this.settings.setCollectAnalytics(collectAnalytics); }
104-
this.searchResultClicked = function(documentId, position) {
105-
var data = {
106-
action: 'click',
107-
session: this.sessionId,
108-
keyword: this.settings.getSettings().keyword,
109-
docid: documentId,
110-
position: position
111-
};
112-
sendStats(this.sitekey, data);
104+
this.setStatsSessionId = function(id) { this.sessionId = id; }
105+
this.getStatsSessionId = function() { return this.sessionId; }
106+
107+
this.sendStatsEvent = function(type, keyword, data) {
108+
if (type === 'search') {
109+
var data = {
110+
action: 'search',
111+
session: this.sessionId,
112+
keyword: keyword,
113+
numberOfResults: data.numberOfResults
114+
};
115+
sendStats(this.sitekey, data);
116+
}
117+
118+
else if (type === 'click') {
119+
var data = {
120+
action: 'click',
121+
session: this.sessionId,
122+
keyword: keyword,
123+
docid: data.documentId,
124+
position: data.position
125+
};
126+
sendStats(this.sitekey, data);
127+
}
128+
129+
else {
130+
throw "Illegal sendStatsEvent type parameters. Should be search or click)";
131+
}
113132
}
114133

134+
115135
// Deprecated
116-
this.useFuzzyMatch = function(use) { this.settings.setFuzzyMatch(use); }
136+
this.searchResultClicked = function(documentId, position) {
137+
this.sendStatsEvent('click', this.settings.getSettings().keyword, {documentId: documentId, position: position});
138+
}
117139
}
118140

119141
module.exports = client;

0 commit comments

Comments
 (0)