Skip to content

Commit b5a9917

Browse files
committed
Add searchResultClicked
1 parent d32f490 commit b5a9917

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-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); }

0 commit comments

Comments
 (0)