-
Notifications
You must be signed in to change notification settings - Fork 12
/
googleExperiments.js
55 lines (52 loc) · 2.08 KB
/
googleExperiments.js
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
angular.module('googleExperiments', ['angularLoad']);
;
angular.module('googleExperiments').directive(
'variation',
['googleExperiments', function(googleExperiments) {
return function(scope, element, attr) {
element.addClass('ng-cloak');
scope.$watch(attr.variation, function googleExperimentsVariationWatchAction(value) {
googleExperiments.getVariation().then(function (variation) {
if (variation == value) {
element.removeClass('ng-cloak');
element.removeClass('ng-hide');
} else {
element.addClass('ng-hide');
}
});
});
};
}]
);
;
angular.module('googleExperiments').provider(
'googleExperiments', [function googleExperimentsProvider() {
var variation;
this.configure = function(conf) {
this.config = conf;
};
this.$get = ['$q', '$timeout', 'angularLoad', function($q, $timeout, angularLoad) {
var variationDeferred = $q.defer();
// dynamically load external javascript file and wait for it to load
//var s = document.createElement('script');
//$timeout(function() {
// console.log('resolving: ' + cxApi.chooseVariation());
// variationDeferred.resolve(cxApi.chooseVariation());
//}, 1000, false);
//
//s.async = false;
//s.src = '//www.google-analytics.com/cx/api.js?experiment=' + this.config.experimentId;
//angular.element('body').append(s);
angularLoad.loadScript('//www.google-analytics.com/cx/api.js?experiment=' + this.config.experimentId).then(function() {
variationDeferred.resolve(cxApi.chooseVariation());
}).catch(function() {
//error loading script
});
return {
getVariation: function() {
return variationDeferred.promise;
}
};
}];
}]
);