-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
109 lines (99 loc) · 3.2 KB
/
index.js
File metadata and controls
109 lines (99 loc) · 3.2 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
/**
* angular-segment - AngularJS module for Segment
* (C) 2016 Wong Yong Jie
*/
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as anonymous module
define(['angular'], function (angular) {
return factory(root, angular);
});
} else if (typeof exports === 'object' && typeof exports.nodeName !== 'string') {
// CommonJS
module.exports = factory({}, require('angular'));
} else if (angular) {
factory(root, root.angular);
}
}(this, function (global, angular) {
angular.module('ngSegment', [])
/**
* @ngdoc service
* @module ngSegment
* @name SegmentProvider
*/
.provider('Segment', function SegmentProvider() {
var writeKey = null;
/**
* @ngdoc method
* @methodOf SegmentProvider
* @name setWriteKey
* @param {String} key The write key to be used
* @description
* Sets the write key to be used with Segment. See Segment's documentation
* for more information:
* https://segment.com/docs/libraries/analytics.js/quickstart/
*/
this.setWriteKey = function setWriteKey(key) {
writeKey = key;
};
/**
* @ngdoc service
* @module ngSegment
* @name Segment
* See Segment's Analytics.js documentation for more info:
* https://segment.com/docs/libraries/analytics.js/
*/
this.$get = function SegmentFactory($window) {
var analytics = $window.analytics = $window.analytics || [];
analytics.methods = [
'trackSubmit', 'trackClick', 'trackLink', 'trackForm', 'pageview',
'identify', 'reset', 'group', 'track', 'ready', 'alias', 'page', 'once',
'off', 'on'
];
analytics.factory = function (method) {
return function () {
var args = Array.prototype.slice.call(arguments);
args.unshift(method);
analytics.push(args);
return analytics;
};
};
for (var i = 0; i < analytics.methods.length; i++) {
var key = analytics.methods[i];
analytics[key] = analytics.factory(key);
}
analytics.load = function (key) {
var script = $window.document.createElement('script');
script.type = 'text/javascript';
script.async = true;
script.src = '//cdn.segment.com/analytics.js/v1/' + key +'/analytics.min.js';
script.onload = function () {
angular.copy($window.analytics, analytics);
};
var first = $window.document.getElementsByTagName('script')[0];
first.parentNode.insertBefore(script, first);
};
analytics.SNIPPET_VERSION = '3.1.0';
analytics.load(writeKey);
return analytics;
};
})
/**
* @ngdoc directive
* @restrict A
*/
.directive('segmentTrack', function (Segment) {
return {
restrict: 'A',
scope: {
event: '@segmentTrack',
options: '<?segmentTrackOptions'
},
link: function link(scope, elem) {
elem.click(function () {
Segment.track(scope.event, scope.options);
});
}
};
});
}));