This repository has been archived by the owner on Dec 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
151 lines (131 loc) · 3.39 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
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
'use strict';
/**
* Module dependencies.
*/
var cookie = require('component-cookie');
var integration = require('@segment/analytics.js-integration');
var push = require('global-queue')('_veroq');
/**
* Expose `Vero` integration.
*/
var Vero = module.exports = integration('Vero')
.global('_veroq')
.option('apiKey', '')
.tag('<script src="//d3qxef4rp70elm.cloudfront.net/m.js">');
/**
* Initialize.
*
* https://github.com/getvero/vero-api/blob/master/sections/js.md
*
* @api public
*/
Vero.prototype.initialize = function() {
// clear default cookie so vero parses correctly.
// this is for the tests.
// basically, they have window.addEventListener('unload')
// which then saves their "command_store", which is an array.
// so we just want to create that initially so we can reload the tests.
if (!cookie('__veroc4')) cookie('__veroc4', '[]');
push('init', { api_key: this.options.apiKey });
this.load(this.ready);
};
/**
* Loaded?
*
* @api private
* @return {boolean}
*/
Vero.prototype.loaded = function() {
return !!(window._veroq && window._veroq.push !== Array.prototype.push);
};
/**
* Page.
*
* https://www.getvero.com/knowledge-base#/questions/71768-Does-Vero-track-pageviews
*
* @api public
* @param {Page} page
*/
Vero.prototype.page = function(page) {
push('trackPageview');
var tags = page.options('Vero').tags;
if (tags) this.addOrRemoveTags(tags);
};
/**
* Identify.
*
* https://www.getvero.com/api/http/#users
* https://github.com/getvero/vero-api/blob/master/sections/js.md#user-identification
*
* @api public
* @param {Identify} identify
*/
Vero.prototype.identify = function(identify) {
var traits = identify.traits();
var email = identify.email();
var id = identify.userId();
// userId OR email address are required by Vero's API. When userId isn't present,
// email will be used as the userId.
if (!id && !email) return;
push('user', traits);
// check for tags and either add or remove.
var tags = identify.options('Vero').tags;
if (tags) this.addOrRemoveTags(tags);
};
/**
* Track.
*
* https://www.getvero.com/api/http/#actions
* https://github.com/getvero/vero-api/blob/master/sections/js.md#tracking-events
*
* @api public
* @param {Track} track
*/
Vero.prototype.track = function(track) {
var regex = /[uU]nsubscribe/;
if (track.event().match(regex)) {
push('unsubscribe', { id: track.properties().id });
} else {
push('track', track.event(), track.properties(), { source: 'segment' });
}
// check for tags and either add or remove.
var tags = track.options('Vero').tags;
if (tags) this.addOrRemoveTags(tags);
};
/**
* Alias.
*
* https://www.getvero.com/api/http/#users
* https://github.com/getvero/vero-api/blob/master/sections/api/users.md
*
* @api public
* @param {Alias} alias
*/
Vero.prototype.alias = function(alias) {
var to = alias.to();
if (alias.from()) {
push('reidentify', to, alias.from());
} else {
push('reidentify', to);
}
var tags = alias.options('Vero').tags;
if (tags) this.addOrRemoveTags(tags);
};
/**
* AddOrRemoveTags.
*
* http://developers.getvero.com/?javascript#tags
*
* @api public
* @param {Object} tags
*/
Vero.prototype.addOrRemoveTags = function(tags) {
var payload = {};
if (!tags.action || !tags.values) return;
var action = tags.action;
payload[action] = tags.values;
if (tags.id) {
payload.id = tags.id;
}
push('tags', payload);
};