Skip to content

Commit 8171597

Browse files
committed
[sc-9487] support modifying stats requests
1 parent 309be61 commit 8171597

File tree

5 files changed

+43
-15
lines changed

5 files changed

+43
-15
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,12 +415,17 @@ client.setApiHostname('api.addsearch.com');
415415

416416
#### Set API request interceptor
417417
`configurationObject` contains 2 keys: <string>`url` and <object>`headers`. Modify the `configurationObject` before it is sent.
418+
419+
`option` is an object with the following properties, all of which are optional. If `option` is not defined, the interceptor will be used for all requests.
420+
- **searchApiRequestOnly**: If true, the interceptor is only used for searchApi requests (default: false)
421+
- **statsApiRequestOnly**: If true, the interceptor is only used for statsApi requests (default: false)
422+
418423
```js
419424
function callback(configurationObject) {
420425
configurationObject.headers['X-Api-Key'] = 'YOUR API KEY';
421426
return configurationObject;
422427
}
423-
client.setApiRequestInterceptor(callback);
428+
client.setApiRequestInterceptor(callback, option);
424429
```
425430

426431

src/api.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
const axios = require('axios').default;
44
const apiInstance = axios.create();
5+
const statsInstance = axios.create();
56

6-
const setRequestInterceptor = (callback) => {
7-
apiInstance.interceptors.request.use( (config) => {
7+
const setRequestInterceptor = (callback, requestType) => {
8+
const axiosInstance = requestType === 'searchApi' ? apiInstance : statsInstance;
9+
axiosInstance.interceptors.request.use( (config) => {
810
const updatedConfig = callback({
911
url: config.url,
1012
headers: config.headers
@@ -18,5 +20,6 @@ const setRequestInterceptor = (callback) => {
1820

1921
module.exports = {
2022
apiInstance,
23+
statsInstance,
2124
setRequestInterceptor
2225
};

src/index.js

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ var client = function(sitekey, privatekey) {
228228
numberOfResults: data.numberOfResults,
229229
tag: this.getSettings().analyticsTag
230230
};
231-
sendStats(this.apiHostname, this.sitekey, payload);
231+
sendStats(this.apiHostname, this.sitekey, payload, this.settings.getSettings().statsRequestIntercepted);
232232
}
233233

234234
else if (type === 'click') {
@@ -240,7 +240,7 @@ var client = function(sitekey, privatekey) {
240240
position: data.position,
241241
tag: this.getSettings().analyticsTag
242242
};
243-
sendStats(this.apiHostname, this.sitekey, payload);
243+
sendStats(this.apiHostname, this.sitekey, payload, this.settings.getSettings().statsRequestIntercepted);
244244
}
245245

246246
else {
@@ -273,11 +273,26 @@ var client = function(sitekey, privatekey) {
273273
/*
274274
* API interceptor
275275
*/
276-
this.setApiRequestInterceptor = function(callback) {
277-
if (typeof callback === 'function') {
278-
setRequestInterceptor(callback);
279-
} else {
276+
this.setApiRequestInterceptor = function(callback, option = {}) {
277+
if (typeof callback !== 'function') {
280278
window.console.error('API interceptor must be a function');
279+
return;
280+
}
281+
282+
const { searchApiRequestOnly = false, statsApiRequestOnly = false } = option;
283+
284+
if (!searchApiRequestOnly && !statsApiRequestOnly) {
285+
setRequestInterceptor(callback, 'searchApi');
286+
setRequestInterceptor(callback, 'statsApi');
287+
this.settings.setStatsRequestIntercepted(true);
288+
} else {
289+
if (searchApiRequestOnly) {
290+
setRequestInterceptor(callback, 'searchApi');
291+
}
292+
if (statsApiRequestOnly) {
293+
setRequestInterceptor(callback, 'statsApi');
294+
this.settings.setStatsRequestIntercepted(true);
295+
}
281296
}
282297
};
283298

src/settings.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ var settings = function() {
2222
},
2323
searchOperator: null,
2424
enableLogicalOperators: false,
25-
cacheResponseTime: null
25+
cacheResponseTime: null,
26+
statsRequestIntercepted: false,
2627
};
2728

2829
this.getSettings = function() {
@@ -232,6 +233,10 @@ var settings = function() {
232233
}
233234
this.settings.searchOperator = operator;
234235
}
236+
237+
this.setStatsRequestIntercepted = function(isIntercepted) {
238+
this.settings.statsRequestIntercepted = isIntercepted;
239+
};
235240
}
236241

237242
module.exports = settings;

src/stats.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
'use strict';
22

33
require('es6-promise').polyfill();
4-
const axios = require('axios').default;
4+
const statsInstance = require('./api').statsInstance;
55

6-
var sendStats = function(apiHostname, sitekey, payload) {
6+
var sendStats = function(apiHostname, sitekey, payload, statsRequestIntercepted) {
77

88
// Beacon in browsers
9-
if (typeof window !== 'undefined' && window.navigator && window.navigator.sendBeacon) {
9+
if (typeof window !== 'undefined' && window.navigator && window.navigator.sendBeacon && !statsRequestIntercepted) {
1010
navigator.sendBeacon('https://' + apiHostname + '/v1/stats/' + sitekey + '/', JSON.stringify(payload));
1111
}
1212

1313
// POST in node
1414
else {
15-
axios.post('https://' + apiHostname + '/v1/stats/' + sitekey + '/', payload, {
15+
statsInstance.post('https://' + apiHostname + '/v1/stats/' + sitekey + '/', payload, {
1616
headers: {
1717
'Content-Type': 'text/plain',
1818
}
1919
});
2020
}
2121
};
2222

23-
module.exports = sendStats;
23+
module.exports = sendStats;

0 commit comments

Comments
 (0)