@mparticle/web-taplytics-kit 2.0.1 → 3.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/TaplyticsKit.common.js +502 -431
- package/package.json +39 -26
|
@@ -28,307 +28,298 @@ function isObject(val) {
|
|
|
28
28
|
// See the License for the specific language governing permissions and
|
|
29
29
|
// limitations under the License.
|
|
30
30
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
var name = 'Taplytics',
|
|
34
|
-
moduleId = 129,
|
|
35
|
-
MessageType = {
|
|
36
|
-
SessionStart: 1,
|
|
37
|
-
SessionEnd: 2,
|
|
38
|
-
PageView: 3,
|
|
39
|
-
PageEvent: 4,
|
|
40
|
-
CrashReport: 5,
|
|
41
|
-
OptOut: 6,
|
|
42
|
-
Commerce: 16
|
|
43
|
-
};
|
|
44
|
-
|
|
45
|
-
var constructor = function () {
|
|
46
|
-
var self = this,
|
|
47
|
-
isInitialized = false,
|
|
48
|
-
reportingService,
|
|
49
|
-
eventQueue = [],
|
|
50
|
-
settings = {},
|
|
51
|
-
initUserAttributes = {},
|
|
52
|
-
initUserIdentities = [];
|
|
53
31
|
|
|
54
|
-
self.name = name;
|
|
55
32
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
33
|
+
var name = 'Taplytics',
|
|
34
|
+
moduleId = 129,
|
|
35
|
+
MessageType = {
|
|
36
|
+
SessionStart: 1,
|
|
37
|
+
SessionEnd: 2,
|
|
38
|
+
PageView: 3,
|
|
39
|
+
PageEvent: 4,
|
|
40
|
+
CrashReport: 5,
|
|
41
|
+
OptOut: 6,
|
|
42
|
+
Commerce: 16,
|
|
43
|
+
};
|
|
61
44
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
45
|
+
var constructor = function() {
|
|
46
|
+
var self = this,
|
|
47
|
+
isInitialized = false,
|
|
48
|
+
reportingService,
|
|
49
|
+
eventQueue = [],
|
|
50
|
+
settings = {},
|
|
51
|
+
initUserAttributes = {},
|
|
52
|
+
initUserIdentities = [];
|
|
53
|
+
|
|
54
|
+
self.name = name;
|
|
55
|
+
|
|
56
|
+
function initForwarder(
|
|
57
|
+
forwarderSettings,
|
|
58
|
+
service,
|
|
59
|
+
testMode,
|
|
60
|
+
trackerId,
|
|
61
|
+
userAttributes,
|
|
62
|
+
userIdentities
|
|
63
|
+
) {
|
|
64
|
+
reportingService = service;
|
|
65
|
+
settings = forwarderSettings;
|
|
66
|
+
initUserAttributes = userAttributes;
|
|
67
|
+
initUserIdentities = userIdentities;
|
|
68
|
+
|
|
69
|
+
try {
|
|
70
|
+
if (!testMode && !window.Taplytics) {
|
|
71
|
+
var taplyticsScript = document.createElement('script');
|
|
72
|
+
taplyticsScript.type = 'text/javascript';
|
|
73
|
+
taplyticsScript.async = true;
|
|
74
|
+
taplyticsScript.src = getTaplyticsSourceLink();
|
|
75
|
+
(
|
|
76
|
+
document.getElementsByTagName('head')[0] ||
|
|
77
|
+
document.getElementsByTagName('body')[0]
|
|
78
|
+
).appendChild(taplyticsScript);
|
|
79
|
+
taplyticsScript.onload = function() {
|
|
86
80
|
isInitialized = true;
|
|
87
|
-
|
|
88
|
-
|
|
81
|
+
|
|
82
|
+
// On load, if the clientsdk exists and there are events
|
|
83
|
+
// in the eventQueue, process each event
|
|
84
|
+
if (window.Taplytics && eventQueue.length > 0) {
|
|
85
|
+
// Process any events that may have been queued up
|
|
86
|
+
// while forwarder was being initialized.
|
|
87
|
+
eventQueue.forEach(function(event) {
|
|
88
|
+
processEvent(event);
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
eventQueue = [];
|
|
89
92
|
}
|
|
93
|
+
};
|
|
94
|
+
} else {
|
|
95
|
+
isInitialized = true;
|
|
96
|
+
if (testMode) {
|
|
97
|
+
Taplytics.src = getTaplyticsSourceLink();
|
|
90
98
|
}
|
|
91
|
-
|
|
92
|
-
return 'Taplytics successfully loaded';
|
|
93
99
|
}
|
|
94
|
-
catch (e) {
|
|
95
|
-
return 'Failed to initialize: ' + e;
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
100
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
function processEvent(event) {
|
|
105
|
-
var reportEvent = false;
|
|
106
|
-
if (isInitialized) {
|
|
107
|
-
try {
|
|
108
|
-
if (event.EventDataType === MessageType.PageView) {
|
|
109
|
-
reportEvent = logPageView(event);
|
|
110
|
-
} else if (event.EventDataType === MessageType.Commerce) {
|
|
111
|
-
if (event.EventCategory === mParticle.CommerceEventType.ProductPurchase) {
|
|
112
|
-
reportEvent = logPurchaseEvent(event);
|
|
113
|
-
} else {
|
|
114
|
-
reportEvent = logCommerceEvent(event);
|
|
115
|
-
}
|
|
116
|
-
} else if (event.EventDataType === MessageType.PageEvent) {
|
|
117
|
-
reportEvent = logEvent(event);
|
|
118
|
-
}
|
|
101
|
+
return 'Taplytics successfully loaded';
|
|
102
|
+
} catch (e) {
|
|
103
|
+
return 'Failed to initialize: ' + e;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
119
106
|
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
107
|
+
/**
|
|
108
|
+
* Called whenever an event occurs. Used to log different types of events.
|
|
109
|
+
* @param event
|
|
110
|
+
* @returns {string}
|
|
111
|
+
*/
|
|
112
|
+
function processEvent(event) {
|
|
113
|
+
var reportEvent = false;
|
|
114
|
+
if (isInitialized) {
|
|
115
|
+
try {
|
|
116
|
+
if (event.EventDataType === MessageType.PageView) {
|
|
117
|
+
reportEvent = logPageView(event);
|
|
118
|
+
} else if (event.EventDataType === MessageType.Commerce) {
|
|
119
|
+
if (
|
|
120
|
+
event.EventCategory ===
|
|
121
|
+
mParticle.CommerceEventType.ProductPurchase
|
|
122
|
+
) {
|
|
123
|
+
reportEvent = logPurchaseEvent(event);
|
|
124
124
|
} else {
|
|
125
|
-
|
|
125
|
+
reportEvent = logCommerceEvent(event);
|
|
126
126
|
}
|
|
127
|
+
} else if (event.EventDataType === MessageType.PageEvent) {
|
|
128
|
+
reportEvent = logEvent(event);
|
|
127
129
|
}
|
|
128
|
-
|
|
129
|
-
|
|
130
|
+
|
|
131
|
+
// leave the below alone
|
|
132
|
+
if (reportEvent === true && reportingService) {
|
|
133
|
+
reportingService(self, event);
|
|
134
|
+
return 'Successfully sent to ' + name;
|
|
135
|
+
} else {
|
|
136
|
+
return (
|
|
137
|
+
'Error logging event or event type not supported - ' +
|
|
138
|
+
reportEvent.error
|
|
139
|
+
);
|
|
130
140
|
}
|
|
141
|
+
} catch (e) {
|
|
142
|
+
return 'Failed to send to: ' + name + ' ' + e;
|
|
131
143
|
}
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
return 'Can\'t send to forwarder ' + name + ', not initialized. Event added to queue.';
|
|
144
|
+
} else {
|
|
145
|
+
eventQueue.push(event);
|
|
137
146
|
}
|
|
138
147
|
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
148
|
+
return (
|
|
149
|
+
"Can't send to forwarder " +
|
|
150
|
+
name +
|
|
151
|
+
', not initialized. Event added to queue.'
|
|
152
|
+
);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Helper method to check if object is empty
|
|
157
|
+
* @param obj
|
|
158
|
+
* @returns {boolean}
|
|
159
|
+
*/
|
|
160
|
+
function isEmpty(obj) {
|
|
161
|
+
for (var prop in obj) {
|
|
162
|
+
return false;
|
|
147
163
|
}
|
|
164
|
+
return true;
|
|
165
|
+
}
|
|
148
166
|
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
}
|
|
167
|
+
/**
|
|
168
|
+
* Helper method to merge obj 2 into obj1
|
|
169
|
+
* @param obj1
|
|
170
|
+
* @param obj2
|
|
171
|
+
*/
|
|
172
|
+
function mergeObjects(obj1, obj2) {
|
|
173
|
+
var obj = {};
|
|
174
|
+
for (var key in obj1) {
|
|
175
|
+
if (obj1[key]) {
|
|
176
|
+
obj[key] = obj1[key];
|
|
160
177
|
}
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
178
|
+
}
|
|
179
|
+
for (var key in obj2) {
|
|
180
|
+
if (obj2[key]) {
|
|
181
|
+
obj[key] = obj2[key];
|
|
165
182
|
}
|
|
166
|
-
return obj;
|
|
167
183
|
}
|
|
184
|
+
return obj;
|
|
185
|
+
}
|
|
168
186
|
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
187
|
+
/**
|
|
188
|
+
* Helper method to clone an object
|
|
189
|
+
* @param {*} obj
|
|
190
|
+
*/
|
|
191
|
+
function clone(obj) {
|
|
192
|
+
var copy = {};
|
|
193
|
+
for (var key in obj) {
|
|
194
|
+
if (obj.hasOwnProperty(key)) {
|
|
177
195
|
copy[key] = obj[key];
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
return copy;
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
/**
|
|
184
|
-
* tracks Taplytics events
|
|
185
|
-
* @param {*} event
|
|
186
|
-
* @param {*} revenue
|
|
187
|
-
* @param {*} attributes
|
|
188
|
-
*/
|
|
189
|
-
function trackEvent(event, revenue, attributes) {
|
|
190
|
-
if (revenue) {
|
|
191
|
-
if (attributes && !isEmpty(attributes)) {
|
|
192
|
-
Taplytics.track(event, revenue, attributes);
|
|
193
|
-
} else {
|
|
194
|
-
Taplytics.track(event, revenue);
|
|
195
|
-
}
|
|
196
|
-
} else {
|
|
197
|
-
if (attributes && !isEmpty(attributes)) {
|
|
198
|
-
Taplytics.track(event, null, attributes);
|
|
199
|
-
} else {
|
|
200
|
-
Taplytics.track(event);
|
|
201
|
-
}
|
|
202
196
|
}
|
|
203
197
|
}
|
|
198
|
+
return copy;
|
|
199
|
+
}
|
|
204
200
|
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
if (timeout) {
|
|
218
|
-
query = query + 'timeout=' + timeout;
|
|
201
|
+
/**
|
|
202
|
+
* tracks Taplytics events
|
|
203
|
+
* @param {*} event
|
|
204
|
+
* @param {*} revenue
|
|
205
|
+
* @param {*} attributes
|
|
206
|
+
*/
|
|
207
|
+
function trackEvent(event, revenue, attributes) {
|
|
208
|
+
if (revenue) {
|
|
209
|
+
if (attributes && !isEmpty(attributes)) {
|
|
210
|
+
Taplytics.track(event, revenue, attributes);
|
|
211
|
+
} else {
|
|
212
|
+
Taplytics.track(event, revenue);
|
|
219
213
|
}
|
|
220
|
-
|
|
221
|
-
if (
|
|
222
|
-
|
|
214
|
+
} else {
|
|
215
|
+
if (attributes && !isEmpty(attributes)) {
|
|
216
|
+
Taplytics.track(event, null, attributes);
|
|
217
|
+
} else {
|
|
218
|
+
Taplytics.track(event);
|
|
223
219
|
}
|
|
220
|
+
}
|
|
221
|
+
}
|
|
224
222
|
|
|
225
|
-
|
|
223
|
+
/**
|
|
224
|
+
* Construct Taplytics src link to load the SDK
|
|
225
|
+
* @returns {string}
|
|
226
|
+
*/
|
|
227
|
+
function getTaplyticsSourceLink() {
|
|
228
|
+
var token = settings.apiKey;
|
|
229
|
+
var cookieDomain = settings.taplyticsOptionCookieDomain;
|
|
230
|
+
var timeout = settings.taplyticsOptionTimeout;
|
|
226
231
|
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
}
|
|
232
|
+
var src = 'https://js.taplytics.com/jssdk/' + token + '.min.js';
|
|
233
|
+
var query = '';
|
|
230
234
|
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
var identity = initUserIdentities[0];
|
|
235
|
-
var type = identity.Type;
|
|
236
|
-
var id = identity.Identity;
|
|
237
|
-
switch (type) {
|
|
238
|
-
case 1:
|
|
239
|
-
user_attributes['user_id'] = id;
|
|
240
|
-
break;
|
|
241
|
-
case 7:
|
|
242
|
-
user_attributes['email'] = id;
|
|
243
|
-
break;
|
|
244
|
-
}
|
|
245
|
-
}
|
|
235
|
+
if (timeout) {
|
|
236
|
+
query = query + 'timeout=' + timeout;
|
|
237
|
+
}
|
|
246
238
|
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
}
|
|
239
|
+
if (cookieDomain) {
|
|
240
|
+
query = query + (query ? '&' : '') + 'cookieDomain=' + cookieDomain;
|
|
241
|
+
}
|
|
251
242
|
|
|
252
|
-
|
|
253
|
-
src = src + '?' + query;
|
|
254
|
-
}
|
|
243
|
+
var userBucketing = settings.taplyticsOptionUserBucketing;
|
|
255
244
|
|
|
256
|
-
|
|
245
|
+
if (userBucketing === 'True') {
|
|
246
|
+
query = query + (query ? '&' : '') + 'user_bucketing=true';
|
|
257
247
|
}
|
|
258
248
|
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
}
|
|
273
|
-
return true;
|
|
274
|
-
}
|
|
275
|
-
catch (e) {
|
|
276
|
-
return { error: e };
|
|
249
|
+
var user_attributes = initUserAttributes || {};
|
|
250
|
+
|
|
251
|
+
if (initUserIdentities.length) {
|
|
252
|
+
var identity = initUserIdentities[0];
|
|
253
|
+
var type = identity.Type;
|
|
254
|
+
var id = identity.Identity;
|
|
255
|
+
switch (type) {
|
|
256
|
+
case 1:
|
|
257
|
+
user_attributes['user_id'] = id;
|
|
258
|
+
break;
|
|
259
|
+
case 7:
|
|
260
|
+
user_attributes['email'] = id;
|
|
261
|
+
break;
|
|
277
262
|
}
|
|
278
263
|
}
|
|
279
264
|
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
productList.forEach(function(product) {
|
|
291
|
-
var product = product;
|
|
292
|
-
var attributes = {};
|
|
293
|
-
var productAttributes = product;
|
|
294
|
-
if (product.Attributes) {
|
|
295
|
-
productAttributes = mergeObjects(product.Attributes, product);
|
|
296
|
-
}
|
|
297
|
-
|
|
298
|
-
if (event.EventAttributes) {
|
|
299
|
-
attributes['EventAttributes'] = event.EventAttributes;
|
|
300
|
-
}
|
|
301
|
-
|
|
302
|
-
if (event.CurrencyCode) {
|
|
303
|
-
attributes['CurrencyCode'] = event.CurrencyCode;
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
attributes['ProductAttributes'] = productAttributes;
|
|
307
|
-
|
|
308
|
-
Taplytics.track(event.EventName, parseFloat(product.TotalAmount), attributes);
|
|
309
|
-
});
|
|
310
|
-
return true;
|
|
311
|
-
}
|
|
312
|
-
catch (e) {
|
|
313
|
-
return {error: e};
|
|
314
|
-
}
|
|
315
|
-
}
|
|
265
|
+
if (!isEmpty(user_attributes)) {
|
|
266
|
+
user_attributes = encodeURIComponent(
|
|
267
|
+
JSON.stringify(user_attributes)
|
|
268
|
+
);
|
|
269
|
+
query =
|
|
270
|
+
query +
|
|
271
|
+
(query ? '&' : '') +
|
|
272
|
+
'user_attributes=' +
|
|
273
|
+
user_attributes;
|
|
274
|
+
}
|
|
316
275
|
|
|
317
|
-
|
|
276
|
+
if (query) {
|
|
277
|
+
src = src + '?' + query;
|
|
318
278
|
}
|
|
319
279
|
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
280
|
+
return src;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/**
|
|
284
|
+
* Logs page view event to Taplytics
|
|
285
|
+
* @param event
|
|
286
|
+
* @returns {*}
|
|
287
|
+
*/
|
|
288
|
+
function logPageView(event) {
|
|
289
|
+
// Details on the `event` object schema in the README
|
|
290
|
+
try {
|
|
291
|
+
if (event.EventAttributes) {
|
|
292
|
+
Taplytics.page(event.EventName, event.EventAttributes);
|
|
293
|
+
} else {
|
|
294
|
+
trackEvent(event.EventName);
|
|
295
|
+
}
|
|
296
|
+
return true;
|
|
297
|
+
} catch (e) {
|
|
298
|
+
return { error: e };
|
|
299
|
+
}
|
|
300
|
+
}
|
|
327
301
|
|
|
328
|
-
|
|
302
|
+
/**
|
|
303
|
+
* Logs purchase events as revenue events to Taplytics
|
|
304
|
+
* @param event
|
|
305
|
+
* @returns {*}
|
|
306
|
+
*/
|
|
307
|
+
function logPurchaseEvent(event) {
|
|
308
|
+
var reportEvent = false;
|
|
309
|
+
if (event.ProductAction.ProductList) {
|
|
310
|
+
try {
|
|
311
|
+
var productList = event.ProductAction.ProductList;
|
|
312
|
+
productList.forEach(function(product) {
|
|
313
|
+
var product = product;
|
|
314
|
+
var attributes = {};
|
|
315
|
+
var productAttributes = product;
|
|
316
|
+
if (product.Attributes) {
|
|
317
|
+
productAttributes = mergeObjects(
|
|
318
|
+
product.Attributes,
|
|
319
|
+
product
|
|
320
|
+
);
|
|
321
|
+
}
|
|
329
322
|
|
|
330
|
-
if (event) {
|
|
331
|
-
try {
|
|
332
323
|
if (event.EventAttributes) {
|
|
333
324
|
attributes['EventAttributes'] = event.EventAttributes;
|
|
334
325
|
}
|
|
@@ -336,217 +327,297 @@ function isObject(val) {
|
|
|
336
327
|
if (event.CurrencyCode) {
|
|
337
328
|
attributes['CurrencyCode'] = event.CurrencyCode;
|
|
338
329
|
}
|
|
339
|
-
|
|
340
|
-
if (event.EventCategory === mParticle.CommerceEventType.PromotionClick) {
|
|
341
|
-
var promotionList = event.PromotionAction.PromotionList;
|
|
342
|
-
if (promotionList) {
|
|
343
|
-
promotionList.forEach(function(promotion) {
|
|
344
|
-
attributes["Promotion"] = promotion;
|
|
345
|
-
trackEvent(event.EventName, null, attributes);
|
|
346
|
-
});
|
|
347
|
-
}
|
|
348
|
-
} else if (event.EventCategory === mParticle.CommerceEventType.ProductImpression) {
|
|
349
|
-
var impressions = event.ProductImpressions;
|
|
350
|
-
if (impressions) {
|
|
351
|
-
impressions.forEach(function(impression) {
|
|
352
|
-
var productList = impression.ProductList;
|
|
353
|
-
var impressionName = impression.ProductImpressionList;
|
|
354
|
-
if (productList) {
|
|
355
|
-
productList.forEach(function(product) {
|
|
356
|
-
var productAttributes = product;
|
|
357
|
-
var attributeCopy = clone(attributes);
|
|
358
|
-
if (product.Attributes) {
|
|
359
|
-
productAttributes = mergeObjects(product, product.Attributes);
|
|
360
|
-
}
|
|
361
|
-
|
|
362
|
-
attributeCopy["ProductImpression"] = impressionName;
|
|
363
|
-
attributeCopy["ProductImpressionProduct"] = productAttributes;
|
|
364
|
-
trackEvent(event.EventName, null, attributeCopy);
|
|
365
|
-
});
|
|
366
|
-
}
|
|
367
|
-
});
|
|
368
|
-
}
|
|
369
|
-
} else if (event.EventCategory === mParticle.CommerceEventType.ProductAddToCart ||
|
|
370
|
-
event.EventCategory === mParticle.CommerceEventType.ProductRemoveFromCart ||
|
|
371
|
-
event.ProductAction) {
|
|
372
|
-
|
|
373
|
-
if (event.ProductAction.ProductList) {
|
|
374
|
-
var productList = event.ProductAction.ProductList;
|
|
375
|
-
productList.forEach(function(product) {
|
|
376
|
-
var productAttributes = product;
|
|
377
|
-
if (product.Attributes) {
|
|
378
|
-
productAttributes = mergeObjects(product, product.Attributes);
|
|
379
|
-
}
|
|
380
|
-
attributes["ProductAttributes"] = productAttributes;
|
|
381
|
-
if (event.ShoppingCart && event.ShoppingCart.ProductList) {
|
|
382
|
-
attributes["ShoppingCart"] = event.ShoppingCart.ProductList;
|
|
383
|
-
}
|
|
384
|
-
trackEvent(event.EventName, null, attributes);
|
|
385
|
-
});
|
|
386
|
-
}
|
|
387
|
-
} else {
|
|
388
|
-
trackEvent(event.EventName, null, attributes);
|
|
389
|
-
}
|
|
390
|
-
|
|
391
|
-
return true;
|
|
392
|
-
}
|
|
393
|
-
catch (e) {
|
|
394
|
-
return {error: e};
|
|
395
|
-
}
|
|
396
|
-
}
|
|
397
330
|
|
|
398
|
-
|
|
399
|
-
}
|
|
331
|
+
attributes['ProductAttributes'] = productAttributes;
|
|
400
332
|
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
trackEvent(event.EventName, null, event.EventAttributes);
|
|
333
|
+
Taplytics.track(
|
|
334
|
+
event.EventName,
|
|
335
|
+
parseFloat(product.TotalAmount),
|
|
336
|
+
attributes
|
|
337
|
+
);
|
|
338
|
+
});
|
|
408
339
|
return true;
|
|
409
|
-
}
|
|
410
|
-
catch (e) {
|
|
340
|
+
} catch (e) {
|
|
411
341
|
return { error: e };
|
|
412
342
|
}
|
|
413
343
|
}
|
|
414
344
|
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
345
|
+
return reportEvent;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
/**
|
|
349
|
+
* Logs all other commerce events as regular events with metadata to Taplytics
|
|
350
|
+
* @param event
|
|
351
|
+
* @returns {*}
|
|
352
|
+
*/
|
|
353
|
+
function logCommerceEvent(event) {
|
|
354
|
+
var reportEvent = false;
|
|
355
|
+
|
|
356
|
+
var attributes = {};
|
|
357
|
+
|
|
358
|
+
if (event) {
|
|
359
|
+
try {
|
|
360
|
+
if (event.EventAttributes) {
|
|
361
|
+
attributes['EventAttributes'] = event.EventAttributes;
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
if (event.CurrencyCode) {
|
|
365
|
+
attributes['CurrencyCode'] = event.CurrencyCode;
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
if (
|
|
369
|
+
event.EventCategory ===
|
|
370
|
+
mParticle.CommerceEventType.PromotionClick
|
|
371
|
+
) {
|
|
372
|
+
var promotionList = event.PromotionAction.PromotionList;
|
|
373
|
+
if (promotionList) {
|
|
374
|
+
promotionList.forEach(function(promotion) {
|
|
375
|
+
attributes['Promotion'] = promotion;
|
|
376
|
+
trackEvent(event.EventName, null, attributes);
|
|
432
377
|
});
|
|
433
378
|
}
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
379
|
+
} else if (
|
|
380
|
+
event.EventCategory ===
|
|
381
|
+
mParticle.CommerceEventType.ProductImpression
|
|
382
|
+
) {
|
|
383
|
+
var impressions = event.ProductImpressions;
|
|
384
|
+
if (impressions) {
|
|
385
|
+
impressions.forEach(function(impression) {
|
|
386
|
+
var productList = impression.ProductList;
|
|
387
|
+
var impressionName =
|
|
388
|
+
impression.ProductImpressionList;
|
|
389
|
+
if (productList) {
|
|
390
|
+
productList.forEach(function(product) {
|
|
391
|
+
var productAttributes = product;
|
|
392
|
+
var attributeCopy = clone(attributes);
|
|
393
|
+
if (product.Attributes) {
|
|
394
|
+
productAttributes = mergeObjects(
|
|
395
|
+
product,
|
|
396
|
+
product.Attributes
|
|
397
|
+
);
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
attributeCopy[
|
|
401
|
+
'ProductImpression'
|
|
402
|
+
] = impressionName;
|
|
403
|
+
attributeCopy[
|
|
404
|
+
'ProductImpressionProduct'
|
|
405
|
+
] = productAttributes;
|
|
406
|
+
trackEvent(
|
|
407
|
+
event.EventName,
|
|
408
|
+
null,
|
|
409
|
+
attributeCopy
|
|
410
|
+
);
|
|
411
|
+
});
|
|
412
|
+
}
|
|
437
413
|
});
|
|
438
414
|
}
|
|
415
|
+
} else if (
|
|
416
|
+
event.EventCategory ===
|
|
417
|
+
mParticle.CommerceEventType.ProductAddToCart ||
|
|
418
|
+
event.EventCategory ===
|
|
419
|
+
mParticle.CommerceEventType.ProductRemoveFromCart ||
|
|
420
|
+
event.ProductAction
|
|
421
|
+
) {
|
|
422
|
+
if (event.ProductAction.ProductList) {
|
|
423
|
+
var productList = event.ProductAction.ProductList;
|
|
424
|
+
productList.forEach(function(product) {
|
|
425
|
+
var productAttributes = product;
|
|
426
|
+
if (product.Attributes) {
|
|
427
|
+
productAttributes = mergeObjects(
|
|
428
|
+
product,
|
|
429
|
+
product.Attributes
|
|
430
|
+
);
|
|
431
|
+
}
|
|
432
|
+
attributes['ProductAttributes'] = productAttributes;
|
|
433
|
+
if (
|
|
434
|
+
event.ShoppingCart &&
|
|
435
|
+
event.ShoppingCart.ProductList
|
|
436
|
+
) {
|
|
437
|
+
attributes['ShoppingCart'] =
|
|
438
|
+
event.ShoppingCart.ProductList;
|
|
439
|
+
}
|
|
440
|
+
trackEvent(event.EventName, null, attributes);
|
|
441
|
+
});
|
|
442
|
+
}
|
|
443
|
+
} else {
|
|
444
|
+
trackEvent(event.EventName, null, attributes);
|
|
439
445
|
}
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
else {
|
|
445
|
-
return 'Can\'t call setUserIdentity on forwarder ' + name + ', not initialized';
|
|
446
|
+
|
|
447
|
+
return true;
|
|
448
|
+
} catch (e) {
|
|
449
|
+
return { error: e };
|
|
446
450
|
}
|
|
447
451
|
}
|
|
448
452
|
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
453
|
+
return reportEvent;
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
/**
|
|
457
|
+
* Log regular Taplytics events
|
|
458
|
+
* @param {*} event
|
|
459
|
+
*/
|
|
460
|
+
function logEvent(event) {
|
|
461
|
+
try {
|
|
462
|
+
trackEvent(event.EventName, null, event.EventAttributes);
|
|
463
|
+
return true;
|
|
464
|
+
} catch (e) {
|
|
465
|
+
return { error: e };
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
/**
|
|
470
|
+
* Identifies user id or email with Taplytics
|
|
471
|
+
* @param id
|
|
472
|
+
* @param type
|
|
473
|
+
* @returns {string}
|
|
474
|
+
*/
|
|
475
|
+
function setUserIdentity(id, type) {
|
|
476
|
+
if (isInitialized) {
|
|
477
|
+
try {
|
|
478
|
+
// Some integrations have primary ids that they use
|
|
479
|
+
// (ie. CustomerId, or Email), you may have special methods
|
|
480
|
+
// to call for these. mParticle allows for several other
|
|
481
|
+
// types of userIds to be set. To view all user identity
|
|
482
|
+
// types, navigate to - https://github.com/mParticle/mparticle-sdk-javascript/blob/master-v2/src/types.js#L88-L101
|
|
483
|
+
if (type === window.mParticle.IdentityType.CustomerId) {
|
|
484
|
+
Taplytics.identify({
|
|
485
|
+
user_id: id,
|
|
486
|
+
});
|
|
487
|
+
} else if (type === window.mParticle.IdentityType.Email) {
|
|
488
|
+
Taplytics.identify({
|
|
489
|
+
email: id,
|
|
490
|
+
});
|
|
465
491
|
}
|
|
492
|
+
} catch (e) {
|
|
493
|
+
return 'Failed to call setUserIdentity on ' + name + ' ' + e;
|
|
466
494
|
}
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
495
|
+
} else {
|
|
496
|
+
return (
|
|
497
|
+
"Can't call setUserIdentity on forwarder " +
|
|
498
|
+
name +
|
|
499
|
+
', not initialized'
|
|
500
|
+
);
|
|
470
501
|
}
|
|
502
|
+
}
|
|
471
503
|
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
504
|
+
/**
|
|
505
|
+
* Sets user attributes to Taplytics
|
|
506
|
+
* @param key
|
|
507
|
+
* @param value
|
|
508
|
+
* @returns {string}
|
|
509
|
+
*/
|
|
510
|
+
function setUserAttribute(key, value) {
|
|
511
|
+
if (isInitialized) {
|
|
512
|
+
try {
|
|
513
|
+
var attributes = {};
|
|
514
|
+
attributes[key] = value;
|
|
515
|
+
Taplytics.identify(attributes);
|
|
516
|
+
return 'Successfully called setUserAttribute API on ' + name;
|
|
517
|
+
} catch (e) {
|
|
518
|
+
return (
|
|
519
|
+
'Failed to call SET setUserAttribute on ' + name + ' ' + e
|
|
520
|
+
);
|
|
521
|
+
}
|
|
522
|
+
} else {
|
|
523
|
+
return (
|
|
524
|
+
"Can't call setUserAttribute on forwarder " +
|
|
525
|
+
name +
|
|
526
|
+
', not initialized'
|
|
527
|
+
);
|
|
478
528
|
}
|
|
529
|
+
}
|
|
479
530
|
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
531
|
+
/**
|
|
532
|
+
* Remove user attribute by setting it to null
|
|
533
|
+
* @param key
|
|
534
|
+
*/
|
|
535
|
+
function removeUserAttribute(key) {
|
|
536
|
+
setUserAttribute(key, null);
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
/**
|
|
540
|
+
* V2 version of setting user identity
|
|
541
|
+
* @param user
|
|
542
|
+
* @returns {string}
|
|
543
|
+
*/
|
|
544
|
+
function onUserIdentified(user) {
|
|
545
|
+
if (isInitialized) {
|
|
546
|
+
var identities = user.getUserIdentities().userIdentities;
|
|
547
|
+
var attributes = {};
|
|
548
|
+
if (identities.customerid) {
|
|
549
|
+
attributes.user_id = identities.customerid;
|
|
498
550
|
}
|
|
499
|
-
|
|
500
|
-
|
|
551
|
+
if (identities.email) {
|
|
552
|
+
attributes.email = identities.email;
|
|
501
553
|
}
|
|
554
|
+
if (!isEmpty(attributes)) {
|
|
555
|
+
Taplytics.identify(attributes);
|
|
556
|
+
}
|
|
557
|
+
} else {
|
|
558
|
+
return (
|
|
559
|
+
"Can't call onUserIdentified on forwarder " +
|
|
560
|
+
name +
|
|
561
|
+
', not initialized'
|
|
562
|
+
);
|
|
502
563
|
}
|
|
564
|
+
}
|
|
503
565
|
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
566
|
+
this.init = initForwarder;
|
|
567
|
+
this.process = processEvent;
|
|
568
|
+
this.setUserIdentity = setUserIdentity;
|
|
569
|
+
this.setUserAttribute = setUserAttribute;
|
|
570
|
+
this.removeUserAttribute = removeUserAttribute;
|
|
571
|
+
this.onUserIdentified = onUserIdentified;
|
|
572
|
+
};
|
|
511
573
|
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
574
|
+
function getId() {
|
|
575
|
+
return moduleId;
|
|
576
|
+
}
|
|
515
577
|
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
578
|
+
function register(config) {
|
|
579
|
+
if (!config) {
|
|
580
|
+
console.log(
|
|
581
|
+
'You must pass a config object to register the kit ' + name
|
|
582
|
+
);
|
|
583
|
+
return;
|
|
584
|
+
}
|
|
521
585
|
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
586
|
+
if (!isObject(config)) {
|
|
587
|
+
console.log(
|
|
588
|
+
"'config' must be an object. You passed in a " + typeof config
|
|
589
|
+
);
|
|
590
|
+
return;
|
|
591
|
+
}
|
|
526
592
|
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
}
|
|
537
|
-
window.console.log('Successfully registered ' + name + ' to your mParticle configuration');
|
|
593
|
+
if (isObject(config.kits)) {
|
|
594
|
+
config.kits[name] = {
|
|
595
|
+
constructor: constructor,
|
|
596
|
+
};
|
|
597
|
+
} else {
|
|
598
|
+
config.kits = {};
|
|
599
|
+
config.kits[name] = {
|
|
600
|
+
constructor: constructor,
|
|
601
|
+
};
|
|
538
602
|
}
|
|
603
|
+
console.log(
|
|
604
|
+
'Successfully registered ' + name + ' to your mParticle configuration'
|
|
605
|
+
);
|
|
606
|
+
}
|
|
539
607
|
|
|
608
|
+
if (typeof window !== 'undefined') {
|
|
540
609
|
if (window && window.mParticle && window.mParticle.addForwarder) {
|
|
541
610
|
window.mParticle.addForwarder({
|
|
542
611
|
name: name,
|
|
543
612
|
constructor: constructor,
|
|
544
|
-
getId: getId
|
|
613
|
+
getId: getId,
|
|
545
614
|
});
|
|
546
615
|
}
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
var TaplyticsKit = {
|
|
619
|
+
register: register,
|
|
620
|
+
};
|
|
550
621
|
var TaplyticsKit_1 = TaplyticsKit.register;
|
|
551
622
|
|
|
552
623
|
exports.default = TaplyticsKit;
|
package/package.json
CHANGED
|
@@ -1,28 +1,41 @@
|
|
|
1
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
|
-
|
|
2
|
+
"name": "@mparticle/web-taplytics-kit",
|
|
3
|
+
"version": "3.1.0",
|
|
4
|
+
"author": "mParticle Developers <developers@mparticle.com> (https://www.mparticle.com)",
|
|
5
|
+
"description": "mParticle integration sdk for Taplytics",
|
|
6
|
+
"main": "dist/TaplyticsKit.common.js",
|
|
7
|
+
"browser": "dist/TaplyticsKit.common.js",
|
|
8
|
+
"files": [
|
|
9
|
+
"dist/TaplyticsKit.common.js"
|
|
10
|
+
],
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "https://github.com/mParticle/mparticle-web-sdk",
|
|
14
|
+
"directory": "kits/taplytics"
|
|
15
|
+
},
|
|
16
|
+
"scripts": {
|
|
17
|
+
"build": "rollup --config rollup.config.js",
|
|
18
|
+
"watch": "rollup --config rollup.config.js -w",
|
|
19
|
+
"test": "node test/boilerplate/test-karma.js"
|
|
20
|
+
},
|
|
21
|
+
"devDependencies": {
|
|
22
|
+
"chai": "^4.2.0",
|
|
23
|
+
"karma": "^5.1.0",
|
|
24
|
+
"karma-chai": "^0.1.0",
|
|
25
|
+
"karma-chrome-launcher": "^3.1.0",
|
|
26
|
+
"karma-firefox-launcher": "^1.3.0",
|
|
27
|
+
"karma-mocha": "^2.0.1",
|
|
28
|
+
"mocha": "^2.5.3",
|
|
29
|
+
"rollup": "^1.13.1",
|
|
30
|
+
"rollup-plugin-commonjs": "^10.0.0",
|
|
31
|
+
"rollup-plugin-node-resolve": "^5.0.1",
|
|
32
|
+
"shelljs": "^0.8.4",
|
|
33
|
+
"should": "^7.1.0",
|
|
34
|
+
"watchify": "^3.11.1"
|
|
35
|
+
},
|
|
36
|
+
"dependencies": {
|
|
37
|
+
"isobject": "^4.0.0",
|
|
38
|
+
"@mparticle/web-sdk": "^3.0.0"
|
|
39
|
+
},
|
|
40
|
+
"license": "Apache-2.0"
|
|
28
41
|
}
|