-
Notifications
You must be signed in to change notification settings - Fork 144
/
Copy pathindex.js
164 lines (146 loc) · 4.16 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
152
153
154
155
156
157
158
159
160
161
162
163
164
'use strict';
/**
* Module dependencies.
*/
var integration = require('@segment/analytics.js-integration');
var del = require('obj-case').del;
var each = require('@ndhoule/each');
var when = require('do-when');
var reject = require('reject');
var find = require('obj-case').find;
/**
* Expose `MoEngage` integration.
*/
var MoEngage = (module.exports = integration('MoEngage')
.option('apiKey', '')
.option('debugMode', false)
.option('cluster', undefined)
.tag(
'<script src="https://cdn.moengage.com/webpush/moe_webSdk.min.latest.js">'
));
/**
* Initialize.
*
* @api public
*/
MoEngage.prototype.initialize = function() {
var self = this;
// @hankim - modified this shim to use `t`, `q`, `f`, and `k` as closed variables to abide by strict mode
/* eslint-disable */
(function(i, s, o, g, r, a, m, n) {
i['moengage_object'] = r;
i['moengage_q'] = []; // @hankim added this since without this it means that this queue is created without having to wait for one of their methods
var t = {};
var q = function(f) {
return function() {
(i['moengage_q'] = i['moengage_q'] || []).push({
f: f,
a: arguments
});
};
};
var f = [
'track_event',
'add_user_attribute',
'add_first_name',
'add_last_name',
'add_email',
'add_mobile',
'add_user_name',
'add_gender',
'add_birthday',
'destroy_session',
'add_unique_user_id',
'moe_events',
'call_web_push',
'track'
];
for (var k in f) {
t[f[k]] = q(f[k]);
}
i['moe'] =
i['moe'] ||
function() {
n = arguments[0];
return t;
};
})(window, document, 'script', null, 'Moengage');
/* eslint-enable */
this.load(function() {
when(self.loaded, function() {
self._client = window.moe({
app_id: self.options.apiKey,
debug_logs: self.options.debugMode ? 1 : 0,
cluster: self.options.cluster
});
// we need to store the current anonymousId for later use in `.identify()` to check if it's a new/existing user
self.initializedAnonymousId = self.analytics.user().anonymousId();
self.ready();
});
});
};
/**
* Loaded?
*
* @api public
* @return {boolean}
*/
MoEngage.prototype.loaded = function() {
return !!window.moeBannerText;
};
/**
* Identify
*
* @api public
*/
MoEngage.prototype.identify = function(identify) {
var self = this;
if (identify.userId()) this._client.add_unique_user_id(identify.userId());
// send common traits
// the partner sdk throws TypeErrors/Uncaughts if you pass `undefined`
var traitsMethodMap = {
firstName: 'first_name',
lastName: 'last_name',
email: 'email',
phone: 'mobile',
name: 'user_name',
username: 'user_name',
gender: 'gender',
birthday: 'birthday',
id: null // just to delete this from identify.traits()
};
var traits = reject(identify.traits()); // strip undefined/null
each(function(value, key) {
// handle username and name especially
if (key === 'name') {
// MoEngage asked to map `name` to `add_user_name` for their existing user base
if (identify.name()) self._client.add_user_name(identify.name());
if (identify.name() && identify.username())
return self._client.add_user_attribute('username', identify.username()); // if they are sending `traits.name` as a semantic trait, there's no other way to get username other than as a custom user attribute
}
// check if there are sendable semantic traits
if (find(traitsMethodMap, key)) {
var method = 'add_' + traitsMethodMap[key];
var trait = identify[key]();
self._client[method](trait);
}
}, traits);
// send custom traits so remove all semantic traits
each(function(value, key) {
del(traits, key);
}, traitsMethodMap);
each(function(value, key) {
self._client.add_user_attribute(key, value);
}, traits);
};
/**
* Track
*
* @api public
*/
MoEngage.prototype.track = function(track) {
this._client.track_event(track.event(), track.properties());
};
MoEngage.prototype.alias = function(alias) {
if (alias.to()) this._client.update_unique_user_id(alias.to());
};