Skip to content

Commit 39f36a7

Browse files
authored
Merge pull request #2 from AddSearch/stats
Stats
2 parents 1483340 + b5a9917 commit 39f36a7

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,15 @@ client.setFuzzyMatch(false);
8181
client.setCollectAnalytics(false);
8282
```
8383

84+
#### Send click event to analytics
85+
When a search results is clicked, send the event to your AddSearch Analytics Dashboard. Information on clicks is used
86+
in your statistics and in the self-learning search algorithm.
87+
```js
88+
// Docid is the 32-character long identifier in each hit returned by the search.
89+
// Position is the position of the document that was clicked, the first result being 1
90+
client.searchResultClicked(docid, position);
91+
```
92+
8493
#### Set JSON Web Token (for authentication)
8594
```js
8695
// Add JWT to the search request (if protected search index)

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
@@ -85,7 +86,16 @@ var client = function(sitekey) {
8586
this.setShuffleAndLimitTo = function(shuffleAndLimitTo) { this.settings.setShuffleAndLimitTo(shuffleAndLimitTo); }
8687
this.setFuzzyMatch = function(fuzzy) { this.settings.setFuzzyMatch(fuzzy); }
8788
this.setCollectAnalytics = function(collectAnalytics) { this.settings.setCollectAnalytics(collectAnalytics); }
88-
//this.hitClicked = function(docid, position) { sendClickHit(this.sitekey, this.settings.getSettings().keyword, docid, position); }
89+
this.searchResultClicked = function(documentId, position) {
90+
var data = {
91+
action: 'click',
92+
session: this.sessionId,
93+
keyword: this.settings.getSettings().keyword,
94+
docid: documentId,
95+
position: position
96+
};
97+
sendStats(this.sitekey, data);
98+
}
8999

90100
// Deprecated
91101
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)