aboutsummaryrefslogtreecommitdiff
path: root/node_modules/algoliasearch/src/createAnalyticsClient.js
blob: d2bf8005574b0927d2e3a707c2177bb4a678089f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
module.exports = createAnalyticsClient;

var algoliasearch = require('../index.js');

function createAnalyticsClient(appId, apiKey, opts) {
  var analytics = {};

  opts = opts || {};
  // there need to be 4 hosts, like on the client, since if requests fail,
  // the counter goes up by 1, so we need to have the same amount of hosts
  // 4 because: -dsn, -1, -2, -3
  // This is done because the APPID used for search will be the same for the analytics client created,
  // and since the state of available hosts is shared by APPID globally for the module, we had issues
  // where the hostIndex would be 1 while the array was only one entry (you got an empty host)
  opts.hosts = opts.hosts || [
    'analytics.algolia.com',
    'analytics.algolia.com',
    'analytics.algolia.com',
    'analytics.algolia.com'
  ];
  opts.protocol = opts.protocol || 'https:';

  analytics.as = algoliasearch(appId, apiKey, opts);

  analytics.getABTests = function(_params, callback) {
    var params = params || {};
    var offset = params.offset || 0;
    var limit = params.limit || 10;

    return this.as._jsonRequest({
      method: 'GET',
      url: '/2/abtests?offset=' + encodeURIComponent(offset) + '&limit=' + encodeURIComponent(limit),
      hostType: 'read',
      forceAuthHeaders: true,
      callback: callback
    });
  };

  analytics.getABTest = function(abTestID, callback) {
    return this.as._jsonRequest({
      method: 'GET',
      url: '/2/abtests/' + encodeURIComponent(abTestID),
      hostType: 'read',
      forceAuthHeaders: true,
      callback: callback
    });
  };

  analytics.addABTest = function(abTest, callback) {
    return this.as._jsonRequest({
      method: 'POST',
      url: '/2/abtests',
      body: abTest,
      hostType: 'read',
      forceAuthHeaders: true,
      callback: callback
    });
  };

  analytics.stopABTest = function(abTestID, callback) {
    return this.as._jsonRequest({
      method: 'POST',
      url: '/2/abtests/' + encodeURIComponent(abTestID) + '/stop',
      hostType: 'read',
      forceAuthHeaders: true,
      callback: callback
    });
  };

  analytics.deleteABTest = function(abTestID, callback) {
    return this.as._jsonRequest({
      method: 'DELETE',
      url: '/2/abtests/' + encodeURIComponent(abTestID),
      hostType: 'write',
      forceAuthHeaders: true,
      callback: callback
    });
  };

  analytics.waitTask = function(indexName, taskID, callback) {
    return this.as.initIndex(indexName).waitTask(taskID, callback);
  };

  return analytics;
}