Skip to content

Commit f7e7ce8

Browse files
authored
Merge branch 'master' into autocomplete
2 parents 59d7ba8 + 7bab037 commit f7e7ce8

File tree

5 files changed

+49
-5
lines changed

5 files changed

+49
-5
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,15 @@ client.setFuzzyMatch(false);
9696
client.setCollectAnalytics(false);
9797
```
9898

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+
```
107+
99108
#### Set JSON Web Token (for authentication)
100109
```js
101110
// Add JWT to the search request (if protected search index)

dist/addsearch-js-client.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "addsearch-js-client",
3-
"version": "0.2.2",
3+
"version": "0.2.3",
44
"description": "AddSearch API JavaScript client",
55
"main": "index.js",
66
"jsdelivr": "./dist/addsearch-js-client.min.js",

src/index.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
'use strict';
22

33
var executeApiFetch = require('./apifetch');
4+
var sendStats = require('./stats');
45
var Settings = require('./settings');
56
var util = require('./util');
6-
//var sendClickHit = require('./stats');
77

88
var client = function(sitekey) {
99
this.sitekey = sitekey;
1010
this.settings = new Settings();
11+
this.sessionId = ('a-' + (Math.random() * 100000000)).substring(0, 10);
1112

1213
/**
1314
* Fetch search results
@@ -100,7 +101,16 @@ var client = function(sitekey) {
100101
this.setShuffleAndLimitTo = function(shuffleAndLimitTo) { this.settings.setShuffleAndLimitTo(shuffleAndLimitTo); }
101102
this.setFuzzyMatch = function(fuzzy) { this.settings.setFuzzyMatch(fuzzy); }
102103
this.setCollectAnalytics = function(collectAnalytics) { this.settings.setCollectAnalytics(collectAnalytics); }
103-
//this.hitClicked = function(docid, position) { sendClickHit(this.sitekey, this.settings.getSettings().keyword, docid, position); }
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);
113+
}
104114

105115
// Deprecated
106116
this.useFuzzyMatch = function(use) { this.settings.setFuzzyMatch(use); }

src/stats.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
'use strict';
2+
3+
require('es6-promise').polyfill();
4+
require('isomorphic-fetch');
5+
6+
var sendStats = function(sitekey, data) {
7+
8+
// Beacon in browsers
9+
if (typeof window !== 'undefined' && window.navigator && window.navigator.sendBeacon) {
10+
navigator.sendBeacon('https://api.addsearch.com/v1/stats/' + sitekey + '/', JSON.stringify(data));
11+
}
12+
13+
// POST in node
14+
else {
15+
fetch('https://api.addsearch.com/v1/stats/' + sitekey + '/', {
16+
method: 'POST',
17+
headers: {
18+
'Content-Type': 'text/plain',
19+
},
20+
body: JSON.stringify(data)
21+
});
22+
}
23+
};
24+
25+
module.exports = sendStats;

0 commit comments

Comments
 (0)