-
Notifications
You must be signed in to change notification settings - Fork 126
/
Copy pathindex.js
110 lines (92 loc) · 2.4 KB
/
index.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
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
/**
* Module dependencies.
*/
var Identify = require('facade').Identify;
var alias = require('alias');
var convertDates = require('convert-dates');
var integration = require('analytics.js-integration');
/**
* Expose `Customerio` integration.
*/
var Customerio = module.exports = integration('Customer.io')
.global('_cio')
.option('siteId', '')
.tag('<script id="cio-tracker" src="https://assets.customer.io/assets/track.js" data-site-id="{{ siteId }}">');
/**
* Initialize.
*
* http://customer.io/docs/api/javascript.html
*
* @api public
*/
Customerio.prototype.initialize = function() {
window._cio = window._cio || [];
/* eslint-disable */
(function(){var a,b,c; a = function(f){return function(){window._cio.push([f].concat(Array.prototype.slice.call(arguments,0))); }; }; b = ['identify', 'track']; for (c = 0; c < b.length; c++) {window._cio[b[c]] = a(b[c]); } })();
/* eslint-enable */
this.load(this.ready);
};
/**
* Loaded?
*
* @api private
* @return {boolean}
*/
Customerio.prototype.loaded = function() {
return !!(window._cio && window._cio.push !== Array.prototype.push);
};
/**
* Identify.
*
* http://customer.io/docs/api/javascript.html#section-Identify_customers
*
* @api public
* @param {Identify} identify
*/
Customerio.prototype.identify = function(identify) {
if (!identify.userId()) return this.debug('user id required');
var traits = identify.traits({ createdAt: 'created' });
traits = alias(traits, { created: 'created_at' });
traits = convertDates(traits, convertDate);
window._cio.identify(traits);
};
/**
* Group.
*
* @api public
* @param {Group} group
*/
Customerio.prototype.group = function(group) {
var traits = group.traits();
var user = this.analytics.user();
traits = alias(traits, function(trait) {
return 'Group ' + trait;
});
this.identify(new Identify({
userId: user.id(),
traits: traits
}));
};
/**
* Track.
*
* http://customer.io/docs/api/javascript.html#section-Track_a_custom_event
*
* @api public
* @param {Track} track
*/
Customerio.prototype.track = function(track) {
var properties = track.properties();
properties = convertDates(properties, convertDate);
window._cio.track(track.event(), properties);
};
/**
* Convert a date to the format Customer.io supports.
*
* @api private
* @param {Date} date
* @return {number}
*/
function convertDate(date) {
return Math.floor(date.getTime() / 1000);
}