-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathOptimizelyClient.js
More file actions
851 lines (830 loc) · 26.6 KB
/
OptimizelyClient.js
File metadata and controls
851 lines (830 loc) · 26.6 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
/**
* @fileOverview Optimizely Client for Node
* @name Optimizely Client
* @author Arun
* @version 0.6.2
*/
/** @access private */
var Promise = require("bluebird");
var rest = require('restler');
/** @const*/
var methodNamesToPromisify =
"get post put del head patch json postJson putJson".split(" ");
var EventEmitterPromisifier = function(originalMethod) {
// return a function
return function promisified() {
var args = [].slice.call(arguments);
// Needed so that the original method can be called with the correct receiver
var self = this;
// which returns a promise
return new Promise(function(resolve, reject) {
// We call the originalMethod here because if it throws,
// it will reject the returned promise with the thrown error
var emitter = originalMethod.apply(self, args);
emitter
.on("success", function(data) {
resolve(data);
})
.on("fail", function(data) {
//Failed Responses including 400 status codes
reject(data);
})
.on("error", function(err) {
//Internal Error
reject(err);
})
.on("abort", function() {
reject(new Promise.CancellationError());
})
.on("timeout", function() {
reject(new Promise.TimeoutError());
});
});
};
};
Promise.promisifyAll(rest, {
filter: function(name) {
return methodNamesToPromisify.indexOf(name) > -1;
},
promisifier: EventEmitterPromisifier
});
////////////////
//0. Constructor
////////////////
/**
* @public
* @Constructor
* @name OptimizelyClient
* @since 0.0.1
* @description Optimizely Client Constructor
* @param {string} apiToken The Optimizely API Token
* @param {object} options to define custom {string} 'url' or {boolean} OAuth2
* @return {OptimizelyClient} The newly created optimizely client.
* @throws {error} Throws an error if apiToken is not provided
* @example
* var apiToken = "*";//Get token from www.optimizely.com/tokens
* var oc = new OptimizelyClient(API_TOKEN);
*/
var OptimizelyClient = function(apiToken, options) {
//initialize
if (!apiToken) throw new Error("Required: apiToken");
this.apiToken = String(apiToken);
this.baseUrl = (options && options.url) ? options.url : 'https://www.optimizelyapis.com/experiment/v1/';
if(options && options.OAuth2){
this.baseHeaders = {
'Authorization': 'Bearer ' + this.apiToken,
'Content-Type': 'application/json'
}
} else {
this.baseHeaders = {
'Token': this.apiToken,
'Content-Type': 'application/json'
}
}
}
////////////////
//1. Projects
////////////////
/**
* @pubilc
* @name OptimizelyClient#createProject
* @since 0.0.1
* @description Create a project in Optimizely
* @param {object} options An object with the following properties:
* {
* @param project_name
* @param {string} [status = "Archived|Active"]
* @param {boolean} [include_jquery = false]
* @param {string} [ip_filter=""]
* }
* @returns {promise} A promise fulfilled with the created project
* @example
* oc.createProject({
* project_name:"sample project name",
* project_status:"Active",
* include_jquery:false,
* ip_filter:""
* })
* .then(function(createdProject){
* //...do something with created project
* })
* .then(null,function(error){
* //handle error
* })
*/
OptimizelyClient.prototype.createProject = function(options) {
options = options || {};
options.project_name = options.project_name || "";
options.project_status = options.project_status || "Active";
options.include_jquery = !!options.include_jquery;
//TODO:Check for truthiness
options.ip_filter = options.ip_filter || "";
var postUrl = this.baseUrl + 'projects/';
return rest.postAsync(postUrl, {
method: 'post',
headers: this.baseHeaders,
data: JSON.stringify(options)
})
}
/**
* @pubilc
* @name OptimizelyClient#getProject
* @since 0.0.1
* @description Retrieve a project from Optimizely
* @param {object} options An object with the following properties:
* {
* @param {string} id
* }
* @note the id may be passed as a string instead of a member of an object
*/
OptimizelyClient.prototype.getProject = function(options) {
if (typeof options === "string" || typeof options === "number") options = {
id: options
};
options = options || {};
options.id = String(options.id || "");
if (!options.id) throw new Error("required: options.id");
var theUrl = this.baseUrl + 'projects/' + options.id;
return rest.getAsync(theUrl, {
method: 'get',
headers: this.baseHeaders
});
}
/**
* @public
* @name OptimizelyClient#updateProject
* @since 0.2.0
* @description Update an Existing Project in Optimizely
* @param {object} options object with the following properties:
* {
* @param {String} id
* @param {String} [project_name]
* @param {String} [project_status = "Active|Archived"]
* @param {Boolean} [include_jquery]
* @param {String} [project_javascript]
* @param {Boolean} [enable_force_variation]
* @param {Boolean} [exclude_disabled_experiments]
* @param {Boolean} [exclude_names]
* @param {Boolean} [ip_anonymization]
* @param {String} [ip_filtering]
* }
* @return {promise} A promise fulfilled with the updated project
*/
OptimizelyClient.prototype.updateProject = function(options) {
options = options || {};
options.id = options.id || false;
if(!options.id) throw new Error('required: options.id');
var putUrl = this.baseUrl + 'projects/' + options.id;
return rest.putAsync(putUrl, {
method: 'put',
headers: this.baseHeaders,
data: JSON.stringify(options)
});
}
/**
* @public
* @name OptimizelyClient#getProjectList
* @since 0.1.0
* @description Retrieves a list of projects from Optimizely
* @return {promise} A promise fulfilled with an array of all projects
*
*/
OptimizelyClient.prototype.getProjects = function(){
var theUrl = this.baseUrl + 'projects/';
return rest.getAsync(theUrl, {
method: 'get',
headers: this.baseHeaders
});
}
////////////////
//2. Experiments
////////////////
/**
*@pubilc
*@name OptimizelyClient#createExperiment
*@since 0.0.1
*@description create an experiment in Optimizely
*/
OptimizelyClient.prototype.createExperiment = function(options) {
options = options || {};
options.description = options.description || "";
options.project_id = options.project_id || "";
options.edit_url = options.edit_url || "";
options.custom_css = options.custom_css || "";
options.custom_js = options.custom_js || "";
if (!options.edit_url) throw new Error("Required: options.edit_url");
if (!options.project_id) throw new Error("Required: options.project_id");
var postUrl = this.baseUrl + 'projects/' + options.project_id +
'/experiments/';
delete options.project_id;
return rest.postAsync(postUrl, {
method: 'post',
headers: this.baseHeaders,
data: JSON.stringify(options)
})
}
/**
*@pubilc
*@name OptimizelyClient#getExperiment
*@since 0.0.1
*@description Retrieve an experiment by id/object.id
*@param {object} options An object with the following properties:
*{
* @param id
*}
*@note the id may be passed as a string instead of a member of an object
*/
OptimizelyClient.prototype.getExperiment = function(options) {
if (typeof options === "string" || typeof options === "number") options = {
id: options
};
options = options || {};
options.id = String(options.id || "");
if (!options.id) throw new Error("required: options.id");
var theUrl = this.baseUrl + 'experiments/' + options.id;
return rest.getAsync(theUrl, {
method: 'get',
headers: this.baseHeaders
});
}
/**
*@pubilc
*@name OptimizelyClient#updateExperiment
*@since 0.0.1
*@description Update an experiment
*@param {object} options An object with the following properties:
*{
* @param id
* @param {string} [status = "Draft|Active"]
* @param {boolean} [include_jquery = false]
* @param {string} [ip_filter=""]
*}
*/
OptimizelyClient.prototype.updateExperiment = function(options) {
options = options || {};
options.id = String(options.id || "");
options.description = options.description || "";
options.edit_url = options.edit_url || "";
options.custom_css = options.custom_css || "";
options.custom_js = options.custom_js || "";
if (!options.id) throw new Error("required: options.id");
var theUrl = this.baseUrl + 'experiments/' + options.id;
delete options.id
return rest.putAsync(theUrl, {
method: 'put',
headers: this.baseHeaders,
data: JSON.stringify(options)
})
}
/**
*@pubilc
*@name OptimizelyClient#pushExperiment
*@since 0.2.0
*@description Create or update an experiment based on presence of an id
*@param {object} options An object with the following properties:
*{
* @param See createExperiment and updateExperiment
*}
*/
OptimizelyClient.prototype.pushExperiment = function(options) {
options = options || {};
options.id = options.id || "";
return options.id ?
this.updateExperiment(options):
this.createExperiment(options);
}
/**
*@pubilc
*@name OptimizelyClient#getExperiments
*@since 0.0.1
*@description Retrieve all experiments associatd with a given project
*@param {object} options An object with the following properties:
*{
* @param {string} project_id
*}
*@note the id may be passed as a string instead of a member of an object
*/
OptimizelyClient.prototype.getExperiments = function(options) {
if (typeof options === "string" || typeof options === "number") options = {
project_id: options
};
options = options || {};
options.project_id = String(options.project_id || "");
if (!options.project_id) throw new Error("required: options.project_id");
var theUrl = this.baseUrl + 'projects/' + options.project_id +
'/experiments/';
return rest.getAsync(theUrl, {
method: 'get',
headers: this.baseHeaders
})
}
/**
*@pubilc
*@name OptimizelyClient#updateExperimentByDescription
*@since 0.0.1
*@description Get an experiment description
*@param {object} options An object with the following properties:
*{
* @param project_name
* @param {string} [status = "Draft|Active"]
* @param {boolean} [include_jquery = false]
* @param {string} [ip_filter=""]
*}
*/
OptimizelyClient.prototype.getExperimentByDescription = function(options) {
if (typeof options === "string") options = {
project_id: options
};
options = options || {};
options.project_id = String(options.project_id || "");
options.description = options.description || arguments[1];
if (!options.project_id) throw new Error("Required: options.project_id");
if (!options.description) throw new Error("Required: options.description");
return this.getExperiments(options.project_id).then(function(data) {
if (typeof data === "string") data = JSON.parse(data);
for (var i in data) {
if (data[i]['description'] ===
options.description) {
return data[i];
};
}
return null;
})
}
/**
*@pubilc
*@name OptimizelyClient#deleteExperiment
*@since 0.2.0
*@description Delete an experiment by id/object.id
*@param {object} options An object with the following properties:
*{
* @param id
*}
*@note the id may be passed as a string/number instead of a member of an object
*/
OptimizelyClient.prototype.deleteExperiment = function(options) {
if (typeof options === "string" || typeof options === "number") options = {
id: options
};
options = options || {};
options.id = String(options.id || "");
if (!options.id) throw new Error("required: options.id");
var theUrl = this.baseUrl + 'experiments/' + options.id;
return rest.delAsync(theUrl, {
method: 'delete',
headers: this.baseHeaders
});
}
/**
* @public
* @name OptimizelyClient#getResults
* @since 0.4.0
* @description get non-stats engine results
* @param {object} options An object with the following properties:
* {
* @param {String} id Experiment ID
* @param {object} [dimension = {}] An object with the following properties:
* {
* @param {String} id Dimension ID
* @param {String} value Dimension Value
* }
* }
* @note the id may be passed as a string/number instead of a member of an object
*/
OptimizelyClient.prototype.getResults = function(options) {
if (typeof options === "string" || typeof options === "number") options = {
id: options
};
options = options || {};
options.id = String(options.id || "");
if (!options.id) throw new Error("required: options.id");
var theUrl = this.baseUrl + 'experiments/' + options.id + '/results';
if (options.dimension) {
var urlParameters = "?";
if (!options.dimension.id) throw new Error("required: options.dimension.id");
if (!options.dimension.value) throw new Error("required: options.dimension.value");
urlParameters += "dimension_id=" + encodeURIComponent(options.dimension.id);
urlParameters += "&dimension_value=" + encodeURIComponent(options.dimension.value);
theUrl += urlParameters;
}
return rest.getAsync(theUrl, {
method: 'GET',
headers: this.baseHeaders
});
}
/**
* @public
* @name OptimizelyClient#getStats
* @since 0.4.0
* @description get stats engine results
* @param {object} options An object with the following properties:
* {
* @param {String} id Experiment ID
* @param {object} [dimension = {}] An object with the following properties:
* {
* @param {String} id Dimension ID
* @param {String} value Dimension Value
* }
* }
* @note the id may be passed as a string/number instead of a member of an object
*/
OptimizelyClient.prototype.getStats = function(options) {
if (typeof options === "string" || typeof options === "number") options = {
id: options
};
options = options || {};
options.id = String(options.id || "");
if (!options.id) throw new Error("required: options.id");
var theUrl = this.baseUrl + 'experiments/' + options.id + '/stats';
if (options.dimension) {
var urlParameters = "?";
if (!options.dimension.id) throw new Error("required: options.dimension.id");
if (!options.dimension.value) throw new Error("required: options.dimension.value");
urlParameters += "dimension_id=" + encodeURIComponent(options.dimension.id);
urlParameters += "&dimension_value=" + encodeURIComponent(options.dimension.value);
theUrl += urlParameters;
}
return rest.getAsync(theUrl, {
method: 'GET',
headers: this.baseHeaders
});
}
////////////////
//3. Variations
////////////////
/**
*@pubilc
*@name OptimizelyClient#getVariations
*@since 0.6.3
*@description list variations for an experiment
*@param {object} options An object with the following properties:
*{
* @param {string|number} experiment_id
*}
*/
OptimizelyClient.prototype.getVariations = function(options) {
options = options || {};
options.experiment_id = String(options.experiment_id || "");
if (!options.experiment_id) throw new Error(
"Required: options.experiment_id");
var url = this.baseUrl + 'experiments/' + options.experiment_id +
'/variations';
return rest.getAsync(url, {
method: 'get',
headers: this.baseHeaders
})
}
/**
*@pubilc
*@name OptimizelyClient#createVariation
*@since 0.0.1
*@description create an experiment in Optimizely
*@param {object} options An object with the following properties:
*{
* @param {string|number} experiment_id
* @param {string} [descriptions = ""]
*}
*/
OptimizelyClient.prototype.createVariation = function(options) {
options = options || {};
options.experiment_id = String(options.experiment_id || "");
options.description = options.description || "";
if (!options.experiment_id) throw new Error(
"Required: options.experiment_id");
var postUrl = this.baseUrl + 'experiments/' + options.experiment_id +
'/variations/';
delete options.experiment_id;
return rest.postAsync(postUrl, {
method: 'post',
headers: this.baseHeaders,
data: JSON.stringify(options)
})
}
/**
*@pubilc
*@name OptimizelyClient#getVariation
*@since 0.0.1
*@description read a variation in Optimizely
*@param {object} options An object with the following properties:
*{
* @param {string|number} id
*}
*/
OptimizelyClient.prototype.getVariation = function(options) {
if (typeof options === "string") options = {
id: options
};
if (!options.id) throw new Error("Required: options.id");
var theUrl = this.baseUrl + 'variations/' + options.id;
return rest.getAsync(theUrl, {
method: 'get',
headers: this.baseHeaders
})
}
/**
* @pubilc
* @name OptimizelyClient#updateVariation
* @since 0.0.1
* @description Update a variation in Optimizely
* @param {object} options An object with the following properties:
* {
* @param {string|number} id
* @param {string} [description]
* }
*/
OptimizelyClient.prototype.updateVariation = function(options) {
var optionsToUpdate = {};
options = options || {};
if (!options.id) throw new Error(
"Required: options.id");
optionsToUpdate.id = options.id || "";
optionsToUpdate.description = options.description || "";
optionsToUpdate.js_component = options.js_component || "";
var theUrl = this.baseUrl + 'variations/' + options.id;
return rest.putAsync(theUrl, {
method: 'put',
headers: this.baseHeaders,
data: JSON.stringify(optionsToUpdate)
})
}
/**
* @pubilc
* @name OptimizelyClient#pushVariation
* @since 0.2.0
* @description Create or update a variation based on the presence of an id
* @param {object} options An object with the following properties:
* {
* @param See createVariation and updateVariaion
* }
*/
OptimizelyClient.prototype.pushVariation = function(options) {
options = options || {};
options.id = options.id || "";
return options.id ?
this.updateVariation(options):
this.createVariation(options);
}
/**
* @pubilc
* @name OptimizelyClient#deleteVariation
* @since 0.1.0
* @description delete a variation in Optimizely
* @param {object} options An object with the following properties:
* {
* @param {string|number} id
* }
*/
OptimizelyClient.prototype.deleteVariation = function(options) {
if (typeof options === "string") options = {
id: options
};
if (!options.id) throw new Error("Required: options.id");
var theUrl = this.baseUrl + 'variations/' + options.id;
return rest.delAsync(theUrl, {
method: 'delete',
headers: this.baseHeaders
})
}
////////////////
//4. Audiences
////////////////
/**
* @pubilc
* @name OptimizelyClient#getAudience
* @since 0.4.0
* @description Read an audience in Optimizely
* @param {object} options An object with the following properties:
* {
* @param {string|number} id The Audience ID
* }
* @returns {promise} A promise fulfilled with the Audience
* @note the id may be passed as a string instead of a member of an object
*/
OptimizelyClient.prototype.getAudience = function(options) {
if (typeof options === "string") options = {
id: options
};
if (!options.id) throw new Error("Required: options.id");
var theUrl = this.baseUrl + 'audiences/' + options.id;
return rest.getAsync(theUrl, {
method: 'get',
headers: this.baseHeaders
});
}
/**
* @pubilc
* @name OptimizelyClient#createAudience
* @since 0.4.0
* @description Create an Audience in Optimizely
* @param {object} options An object with the following properties:
* {
* @param {String} id Project ID
* @param {String} name
* @param {String} [description]
* @param {Boolean} [segmentation] Only available for platinum
* @param {Array} [conditions] See http://developers.optimizely.com/rest/conditions/
* }
* @returns {promise} A promise fulfilled with the created project
*/
OptimizelyClient.prototype.createAudience = function(options) {
var optionsToSend = {};
options = options || {};
if (!options.name) throw new Error("Required: options.name");
if (!options.id) throw new Error("Required: options.id");
optionsToSend.name = options.name;
optionsToSend.id = options.id;
optionsToSend.description = options.description || "";
optionsToSend.segmentation = options.segmentation || false;
optionsToSend.conditions = options.conditions || [];
var postUrl = this.baseUrl + 'projects/' + options.id + '/audiences/';
return rest.postAsync(postUrl, {
method: 'post',
headers: this.baseHeaders,
data: JSON.stringify(optionsToSend)
})
}
/**
* @public
* @name OptimizelyClient#updateAudience
* @since 0.4.0
* @description Update an Existing Project in Optimizely
* @param {object} options object with the following properties:
* {
* @param {String} id
* @param {String} [name]
* @param {Array} [conditions] See http://developers.optimizely.com/rest/conditions/
* @param {Boolean} [segmentation] Platinum Customers only
* }
* @return {promise} A promise fulfilled with the updated audience
*/
OptimizelyClient.prototype.updateAudience = function(options) {
options = options || {};
options.id = options.id || false;
if(!options.id) throw new Error('required: options.id');
var putUrl = this.baseUrl + 'audiences/' + options.id;
return rest.putAsync(putUrl, {
method: 'put',
headers: this.baseHeaders,
data: JSON.stringify(options)
});
}
/**
* @public
* @name OptimizelyClient#getAudiences
* @since 0.4.0
* @description Retrieves a list of Audiences in a project from Optimizely
* @param {object} options An object with the following properties:
* {
* @param {string|number} id The Project ID
* }
* @return {promise} A promise fulfilled with an array of all Audiences
*
*/
OptimizelyClient.prototype.getAudiences = function(options){
if (typeof options === "string" || typeof options === "number") options = {
id: options
};
options = options || {};
options.id = options.id || "";
if (!options.id) throw new Error("required: options.id");
var theUrl = this.baseUrl + 'projects/' + options.id + '/audiences/';
return rest.getAsync(theUrl, {
method: 'get',
headers: this.baseHeaders
});
}
////////////////
//5. Dimensions
////////////////
/**
* @pubilc
* @name OptimizelyClient#getDimension
* @since 0.5.0
* @description Read a dimension in Optimizely
* @param {object} options An object with the following properties:
* {
* @param {string|number} id The Dimension ID
* }
* @returns {promise} A promise fulfilled with the Dimension
* @note the id may be passed as a string instead of a member of an object
*/
OptimizelyClient.prototype.getDimension = function(options) {
if (typeof options === "string") options = {
id: options
};
if (!options.id) throw new Error("Required: options.id");
var theUrl = this.baseUrl + 'dimensions/' + options.id;
return rest.getAsync(theUrl, {
method: 'get',
headers: this.baseHeaders
});
}
/**
* @pubilc
* @name OptimizelyClient#createDimension
* @since 0.5.0
* @description Create an Dimension in Optimizely
* @param {object} options An object with the following properties:
* {
* @param {String} id Project ID
* @param {String} name
* @param {String} [description]
* @param {Boolean} [client_api_name] A unique name to refer to this dimension
* }
* @returns {promise} A promise fulfilled with the created project
*/
OptimizelyClient.prototype.createDimension = function(options) {
var optionsToSend = {};
options = options || {};
if (!options.name) throw new Error("Required: options.name");
if (!options.id) throw new Error("Required: options.id");
optionsToSend.name = options.name;
optionsToSend.id = options.id;
optionsToSend.description = options.description || "";
optionsToSend.client_api_name = options.client_api_name || "";
var postUrl = this.baseUrl + 'projects/' + options.id + '/dimensions/';
return rest.postAsync(postUrl, {
method: 'post',
headers: this.baseHeaders,
data: JSON.stringify(optionsToSend)
})
}
/**
* @public
* @name OptimizelyClient#updateDimension
* @since 0.5.0
* @description Update an Existing Dimension in Optimizely
* @param {object} options object with the following properties:
* {
* @param {String} id
* @param {String} [name]
* @param {String} [description]
* @param {Boolean} [client_api_name] A unique name to refer to this dimension
* }
* @return {promise} A promise fulfilled with the updated audience
*/
OptimizelyClient.prototype.updateDimension = function(options) {
options = options || {};
options.id = options.id || false;
if(!options.id) throw new Error('required: options.id');
var putUrl = this.baseUrl + 'dimensions/' + options.id;
return rest.putAsync(putUrl, {
method: 'put',
headers: this.baseHeaders,
data: JSON.stringify(options)
});
}
/**
* @public
* @name OptimizelyClient#getDimensions
* @since 0.5.0
* @description Retrieves a list of Dimensions in a project from Optimizely
* @param {object} options An object with the following properties:
* {
* @param {string|number} id The Project ID
* }
* @return {promise} A promise fulfilled with an array of all Audiences
*
*/
OptimizelyClient.prototype.getDimensions = function(options){
if (typeof options === "string" || typeof options === "number") options = {
id: options
};
options = options || {};
options.id = options.id || "";
if (!options.id) throw new Error("required: options.id");
var theUrl = this.baseUrl + 'projects/' + options.id + '/dimensions/';
return rest.getAsync(theUrl, {
method: 'get',
headers: this.baseHeaders
});
}
////////////////
//6. Goals
////////////////
/**
* @public
* @name OptimizelyClient#getGoals
* @since 0.5.0
* @description Retrieves a list of Goals in a project from Optimizely
* @param {object} options An object with the following properties:
* {
* @param {string|number} id The Project ID
* }
* @return {promise} A promise fulfilled with an array of all Goals
*
*/
OptimizelyClient.prototype.getGoals = function(options){
if (typeof options === "string" || typeof options === "number") options = {
id: options
};
options = options || {};
options.id = options.id || "";
if (!options.id) throw new Error("required: options.id");
var theUrl = this.baseUrl + 'projects/' + options.id + '/goals/';
return rest.getAsync(theUrl, {
method: 'get',
headers: this.baseHeaders
});
}
module.exports = OptimizelyClient;