@mparticle/web-localytics-kit 2.1.0 → 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.
@@ -25,341 +25,395 @@ function isObject(val) {
25
25
  // See the License for the specific language governing permissions and
26
26
  // limitations under the License.
27
27
 
28
-
29
-
30
- var name = 'LocalyticsEventForwarder',
31
- moduleId = 84,
32
- maxAllowedDimensions = 10,
33
- trackerCount = 1,
34
- MessageType = {
35
- PageView : 3,
36
- PageEvent : 4,
37
- Commerce : 16
38
- },
39
- EventType = {
40
- Unknown : 0,
41
- Navigation : 1,
42
- Location : 2,
43
- Search : 3,
44
- Transaction : 4,
45
- UserContent : 5,
46
- UserPreference: 6,
47
- Social : 7,
48
- Other : 8,
49
- Media : 9
50
- };
51
-
52
- var constructor = function () {
53
- var self = this,
54
- isInitialized = false,
55
- forwarderSettings = null,
56
- reportingService = null,
57
- customDimensions = [],
58
- initOptions = {},
59
- trackerId = null;
60
-
61
- self.name = name;
62
-
63
- function createTrackerId() {
64
- return 'mpllTracker' + trackerCount++;
65
- }
66
28
 
67
- function createCmd(cmd) {
68
- // Prepends the specified command with the tracker id
69
- return cmd + '.' + trackerId;
70
- }
71
-
72
- function initForwarder(settings, service, testMode, tid) {
73
- forwarderSettings = settings;
74
- reportingService = service;
75
- initOptions = getInitOptions();
76
- customDimensions = getOrderedCustomDimensions();
77
-
78
- if (!tid) {
79
- trackerId = createTrackerId();
80
- }
81
- else {
82
- trackerId = tid;
83
- }
84
-
85
- try {
86
- if (!testMode) {
87
- !function(l,y,t,i,c,s) {
88
- l['LocalyticsGlobal'] = i;
89
- l[i] = function() { (l[i].q = l[i].q || []).push(arguments); };
90
- l[i].t = +new Date;
91
- (s = y.createElement(t)).type = 'text/javascript';
92
- s.src = '//web.localytics.com/v4/localytics.min.js';
93
- (c = y.getElementsByTagName(t)[0]).parentNode.insertBefore(s, c);
94
- }(window, document, 'script', 'll');
95
-
96
- window.ll(createCmd('init'), settings.appKey, initOptions);
97
- }
98
29
 
99
- isInitialized = true;
30
+ var name = 'LocalyticsEventForwarder',
31
+ moduleId = 84,
32
+ maxAllowedDimensions = 10,
33
+ trackerCount = 1,
34
+ MessageType = {
35
+ PageView: 3,
36
+ PageEvent: 4,
37
+ Commerce: 16,
38
+ },
39
+ EventType = {
40
+ Unknown: 0,
41
+ Navigation: 1,
42
+ Location: 2,
43
+ Search: 3,
44
+ Transaction: 4,
45
+ UserContent: 5,
46
+ UserPreference: 6,
47
+ Social: 7,
48
+ Other: 8,
49
+ Media: 9,
50
+ };
51
+
52
+ var constructor = function() {
53
+ var self = this,
54
+ isInitialized = false,
55
+ forwarderSettings = null,
56
+ reportingService = null,
57
+ customDimensions = [],
58
+ initOptions = {},
59
+ trackerId = null;
100
60
 
101
- return 'Successfully initialized: ' + name;
61
+ self.name = name;
102
62
 
103
- } catch (e) {
104
- return 'Can\'t initialize forwarder: '+ name +':' + e;
105
- }
63
+ function createTrackerId() {
64
+ return 'mpllTracker' + trackerCount++;
65
+ }
66
+
67
+ function createCmd(cmd) {
68
+ // Prepends the specified command with the tracker id
69
+ return cmd + '.' + trackerId;
70
+ }
71
+
72
+ function initForwarder(settings, service, testMode, tid) {
73
+ forwarderSettings = settings;
74
+ reportingService = service;
75
+ initOptions = getInitOptions();
76
+ customDimensions = getOrderedCustomDimensions();
77
+
78
+ if (!tid) {
79
+ trackerId = createTrackerId();
80
+ } else {
81
+ trackerId = tid;
106
82
  }
107
-
108
- function getInitOptions() {
109
- var options = {};
110
-
111
- if(forwarderSettings.appVersion){
112
- options.appVersion = forwarderSettings.appVersion;
113
- }
114
83
 
115
- if(forwarderSettings.sessionTimeout){
116
- options.sessionTimeout = forwarderSettings.sessionTimeout;
84
+ try {
85
+ if (!testMode) {
86
+ !(function(l, y, t, i, c, s) {
87
+ l['LocalyticsGlobal'] = i;
88
+ l[i] = function() {
89
+ (l[i].q = l[i].q || []).push(arguments);
90
+ };
91
+ l[i].t = +new Date();
92
+ (s = y.createElement(t)).type = 'text/javascript';
93
+ s.src = '//web.localytics.com/v4/localytics.min.js';
94
+ (c = y.getElementsByTagName(t)[0]).parentNode.insertBefore(
95
+ s,
96
+ c
97
+ );
98
+ })(window, document, 'script', 'll');
99
+
100
+ window.ll(createCmd('init'), settings.appKey, initOptions);
117
101
  }
118
102
 
119
- if(forwarderSettings.domain){
120
- options.domain = forwarderSettings.domain;
121
- }
103
+ isInitialized = true;
122
104
 
123
- options.trackPageView = forwarderSettings.hasOwnProperty('trackPageView') &&
124
- forwarderSettings.trackPageView.toLowerCase() === 'true';
125
-
126
- return options;
105
+ return 'Successfully initialized: ' + name;
106
+ } catch (e) {
107
+ return "Can't initialize forwarder: " + name + ':' + e;
127
108
  }
109
+ }
128
110
 
129
- function processEvent(event) {
130
- var reportEvent = false;
131
-
132
- if(!isInitialized) {
133
- return 'Can\'t send forwarder '+ name + ', not initialized';
134
- }
111
+ function getInitOptions() {
112
+ var options = {};
135
113
 
136
- try {
137
- if(event.EventDataType == MessageType.PageView) {
138
- reportEvent = true;
139
- logScreenViewEvent(event);
140
- } else if (event.EventDataType == MessageType.Commerce) {
141
- logCommerce(event);
142
- reportEvent = true;
143
- }
144
- else if (event.EventDataType == MessageType.PageEvent) {
145
- reportEvent = true;
146
- logEvent(event);
147
- }
114
+ if (forwarderSettings.appVersion) {
115
+ options.appVersion = forwarderSettings.appVersion;
116
+ }
148
117
 
149
- if(reportEvent && reportingService) {
150
- reportingService(self, event);
151
- }
118
+ if (forwarderSettings.sessionTimeout) {
119
+ options.sessionTimeout = forwarderSettings.sessionTimeout;
120
+ }
152
121
 
153
- return 'Successfully sent to forwarder ' + name;
154
- } catch (e) {
155
- return 'Can\'t send to forwarder: ' + name + ' ' + e;
156
- }
122
+ if (forwarderSettings.domain) {
123
+ options.domain = forwarderSettings.domain;
157
124
  }
158
125
 
159
- function logScreenViewEvent(event) {
160
- var screenName = event.EventName;
161
-
162
- if(event.hasOwnProperty('CustomFlags') && event.CustomFlags.hasOwnProperty("Localytics.ScreenName")) {
163
- screenName = event.CustomFlags["Localytics.ScreenName"];
164
- }
165
-
166
- window.ll(createCmd('tagScreen'), screenName);
126
+ options.trackPageView =
127
+ forwarderSettings.hasOwnProperty('trackPageView') &&
128
+ forwarderSettings.trackPageView.toLowerCase() === 'true';
129
+
130
+ return options;
131
+ }
132
+
133
+ function processEvent(event) {
134
+ var reportEvent = false;
135
+
136
+ if (!isInitialized) {
137
+ return "Can't send forwarder " + name + ', not initialized';
167
138
  }
168
139
 
169
- function logEvent(event) {
170
- var ltv = null;
171
- if(event.hasOwnProperty('EventCategory') && event.EventCategory === EventType.Transaction) {
172
- if(event.hasOwnProperty('EventAttributes') && event.EventAttributes.hasOwnProperty("$Amount")) {
173
- var ltvVal = event.EventAttributes["$Amount"];
174
- if(!isNaN(parseFloat(ltvVal, 10))) {
175
- ltv = parseFloat(ltvVal, 10).toFixed(2);
176
- }
177
- }
140
+ try {
141
+ if (event.EventDataType == MessageType.PageView) {
142
+ reportEvent = true;
143
+ logScreenViewEvent(event);
144
+ } else if (event.EventDataType == MessageType.Commerce) {
145
+ logCommerce(event);
146
+ reportEvent = true;
147
+ } else if (event.EventDataType == MessageType.PageEvent) {
148
+ reportEvent = true;
149
+ logEvent(event);
178
150
  }
179
-
180
- if(ltv !== null) {
181
- var multiplier = forwarderSettings.trackClvAsRawValue ? 1 : 100;
182
- window.ll(createCmd('tagEvent'), event.EventName, event.EventAttributes, ltv * multiplier);
183
- } else {
184
- window.ll(createCmd('tagEvent'), event.EventName, event.EventAttributes);
151
+
152
+ if (reportEvent && reportingService) {
153
+ reportingService(self, event);
185
154
  }
155
+
156
+ return 'Successfully sent to forwarder ' + name;
157
+ } catch (e) {
158
+ return "Can't send to forwarder: " + name + ' ' + e;
186
159
  }
160
+ }
187
161
 
188
- function logCommerce(data) {
189
-
190
- if(!data.ProductAction)
191
- return 'Can\'t forward commerce event: No production action found';
192
-
193
- switch (data.ProductAction.ProductActionType) {
194
- case mParticle.ProductActionType.Purchase:
195
- case mParticle.ProductActionType.Refund:
196
- var total = null,
197
- multiplier = forwarderSettings.trackClvAsRawValue ? 1 : 100;
198
-
199
- if (data.ProductAction.ProductActionType == mParticle.ProductActionType.Refund) {
200
- multiplier *= -1;
201
- }
202
-
203
- if(!isNaN(parseFloat(data.ProductAction.TotalAmount, 10))) {
204
- total = parseFloat(data.ProductAction.TotalAmount, 10).toFixed(2);
205
- }
206
-
207
- if(total != null) {
208
- window.ll(createCmd('tagEvent'), data.EventName, data.EventAttributes, total * multiplier);
209
- } else {
210
- return 'Can\'t forward commerce event: TotalAmount is set incorrectly';
211
- }
212
- break;
213
-
214
- default:
215
- logEvent(data);
216
- break;
217
- }
162
+ function logScreenViewEvent(event) {
163
+ var screenName = event.EventName;
164
+
165
+ if (
166
+ event.hasOwnProperty('CustomFlags') &&
167
+ event.CustomFlags.hasOwnProperty('Localytics.ScreenName')
168
+ ) {
169
+ screenName = event.CustomFlags['Localytics.ScreenName'];
218
170
  }
219
-
220
- function setUserAttribute(key, value) {
221
- if (!key) {
222
- return 'Can\'t call setUserAttribute on forwarder: No key provided';
223
- }
224
-
225
- if(key === 'Localytics.CustomerName') {
226
- try {
227
- window.ll(createCmd('setCustomerName'), value);
228
- }
229
- catch(e) {
230
- return 'Can\'t call setCustomerName on forwarder: ' + name + ': ' + e;
231
- }
232
- } else {
233
- if(customDimensions.length > 0) {
234
- updateCustomDimension(key, value);
171
+
172
+ window.ll(createCmd('tagScreen'), screenName);
173
+ }
174
+
175
+ function logEvent(event) {
176
+ var ltv = null;
177
+ if (
178
+ event.hasOwnProperty('EventCategory') &&
179
+ event.EventCategory === EventType.Transaction
180
+ ) {
181
+ if (
182
+ event.hasOwnProperty('EventAttributes') &&
183
+ event.EventAttributes.hasOwnProperty('$Amount')
184
+ ) {
185
+ var ltvVal = event.EventAttributes['$Amount'];
186
+ if (!isNaN(parseFloat(ltvVal, 10))) {
187
+ ltv = parseFloat(ltvVal, 10).toFixed(2);
235
188
  }
236
189
  }
237
190
  }
238
-
239
- function removeUserAttribute(key) {
240
- if(customDimensions.length > 0) {
241
- updateCustomDimension(key, null);
242
- }
191
+
192
+ if (ltv !== null) {
193
+ var multiplier = forwarderSettings.trackClvAsRawValue ? 1 : 100;
194
+ window.ll(
195
+ createCmd('tagEvent'),
196
+ event.EventName,
197
+ event.EventAttributes,
198
+ ltv * multiplier
199
+ );
200
+ } else {
201
+ window.ll(
202
+ createCmd('tagEvent'),
203
+ event.EventName,
204
+ event.EventAttributes
205
+ );
243
206
  }
244
-
245
- function setUserIdentity(id, type) {
246
- if (!id) {
247
- return 'Can\'t call setUserIdentity on forwarder: ' + name + ' without ID';
248
- }
207
+ }
249
208
 
250
- if (!isInitialized) {
251
- return 'Can\'t call setUserIdentity on forwarder: ' + name + ', not initialized';
252
- }
209
+ function logCommerce(data) {
210
+ if (!data.ProductAction)
211
+ return "Can't forward commerce event: No production action found";
212
+
213
+ switch (data.ProductAction.ProductActionType) {
214
+ case mParticle.ProductActionType.Purchase:
215
+ case mParticle.ProductActionType.Refund:
216
+ var total = null,
217
+ multiplier = forwarderSettings.trackClvAsRawValue ? 1 : 100;
218
+
219
+ if (
220
+ data.ProductAction.ProductActionType ==
221
+ mParticle.ProductActionType.Refund
222
+ ) {
223
+ multiplier *= -1;
224
+ }
253
225
 
254
- try {
255
- if(window.mParticle.IdentityType.Email == type) {
256
- window.ll(createCmd('setCustomerEmail'), id);
226
+ if (!isNaN(parseFloat(data.ProductAction.TotalAmount, 10))) {
227
+ total = parseFloat(
228
+ data.ProductAction.TotalAmount,
229
+ 10
230
+ ).toFixed(2);
257
231
  }
258
- else {
259
- window.ll(createCmd('setCustomerId'), id);
232
+
233
+ if (total != null) {
234
+ window.ll(
235
+ createCmd('tagEvent'),
236
+ data.EventName,
237
+ data.EventAttributes,
238
+ total * multiplier
239
+ );
240
+ } else {
241
+ return "Can't forward commerce event: TotalAmount is set incorrectly";
260
242
  }
261
-
262
- return 'Successfully called identify on forwarder: ' + name;
263
- }
264
- catch (e) {
265
- return 'Can\'t call identify on forwarder: ' + name + ': ' + e;
266
- }
243
+ break;
244
+
245
+ default:
246
+ logEvent(data);
247
+ break;
267
248
  }
268
-
269
- function updateCustomDimension(key, value) {
270
- if(customDimensions.length === 0)
271
- return;
272
-
273
- var index = -1,
274
- keyLower = key.toLowerCase();
275
-
276
- for (var i = 0; i < customDimensions.length; i++) {
277
- if(customDimensions[i].toLowerCase() === keyLower) {
278
- index = i;
279
- }
280
- }
281
-
282
- if(index >= 0) {
283
- window.ll(createCmd('setCustomDimension'), index, value);
284
- }
249
+ }
250
+
251
+ function setUserAttribute(key, value) {
252
+ if (!key) {
253
+ return "Can't call setUserAttribute on forwarder: No key provided";
285
254
  }
286
-
287
- function getOrderedCustomDimensions() {
288
- var dimensions = null,
289
- orderedDimensions = [];
290
-
255
+
256
+ if (key === 'Localytics.CustomerName') {
291
257
  try {
292
- dimensions = JSON.parse(forwarderSettings.customDimensions.replace(/&quot;/g,'"'));
258
+ window.ll(createCmd('setCustomerName'), value);
293
259
  } catch (e) {
294
- return orderedDimensions;
260
+ return (
261
+ "Can't call setCustomerName on forwarder: " +
262
+ name +
263
+ ': ' +
264
+ e
265
+ );
295
266
  }
296
-
297
- if(dimensions.length > 0) {
298
- for (var index = 0; index < maxAllowedDimensions; index++) {
299
- for (var j = 0; j < dimensions.length; j++) {
300
- if(dimensions[j].maptype.toLowerCase() !== 'userattributeclass.name' ||
301
- !dimensions[j].value ||
302
- !dimensions[j].map)
303
- continue;
304
-
305
- if(dimensions[j].value.toLowerCase() === 'dimension ' + index) {
306
- orderedDimensions.push(dimensions[j].map);
307
- }
308
- }
309
- }
267
+ } else {
268
+ if (customDimensions.length > 0) {
269
+ updateCustomDimension(key, value);
310
270
  }
311
-
312
- return orderedDimensions;
313
271
  }
314
-
315
- this.init = initForwarder;
316
- this.process = processEvent;
317
- this.setUserAttribute = setUserAttribute;
318
- this.setUserIdentity = setUserIdentity;
319
- this.removeUserAttribute = removeUserAttribute;
320
- };
272
+ }
321
273
 
322
- function getId() {
323
- return moduleId;
274
+ function removeUserAttribute(key) {
275
+ if (customDimensions.length > 0) {
276
+ updateCustomDimension(key, null);
277
+ }
324
278
  }
325
279
 
326
- function register(config) {
327
- if (!config) {
328
- console.log('You must pass a config object to register the kit ' + name);
329
- return;
280
+ function setUserIdentity(id, type) {
281
+ if (!id) {
282
+ return (
283
+ "Can't call setUserIdentity on forwarder: " +
284
+ name +
285
+ ' without ID'
286
+ );
330
287
  }
331
288
 
332
- if (!isObject(config)) {
333
- console.log('\'config\' must be an object. You passed in a ' + typeof config);
334
- return;
289
+ if (!isInitialized) {
290
+ return (
291
+ "Can't call setUserIdentity on forwarder: " +
292
+ name +
293
+ ', not initialized'
294
+ );
335
295
  }
336
296
 
337
- if (isObject(config.kits)) {
338
- config.kits[name] = {
339
- constructor: constructor
340
- };
341
- } else {
342
- config.kits = {};
343
- config.kits[name] = {
344
- constructor: constructor
345
- };
297
+ try {
298
+ if (window.mParticle.IdentityType.Email == type) {
299
+ window.ll(createCmd('setCustomerEmail'), id);
300
+ } else {
301
+ window.ll(createCmd('setCustomerId'), id);
302
+ }
303
+
304
+ return 'Successfully called identify on forwarder: ' + name;
305
+ } catch (e) {
306
+ return "Can't call identify on forwarder: " + name + ': ' + e;
346
307
  }
347
- console.log('Successfully registered ' + name + ' to your mParticle configuration');
348
308
  }
349
309
 
350
- if (typeof window !== 'undefined') {
351
- if (window && window.mParticle && window.mParticle.addForwarder) {
352
- window.mParticle.addForwarder({
353
- name: name,
354
- constructor: constructor,
355
- getId: getId
356
- });
310
+ function updateCustomDimension(key, value) {
311
+ if (customDimensions.length === 0) return;
312
+
313
+ var index = -1,
314
+ keyLower = key.toLowerCase();
315
+
316
+ for (var i = 0; i < customDimensions.length; i++) {
317
+ if (customDimensions[i].toLowerCase() === keyLower) {
318
+ index = i;
319
+ }
320
+ }
321
+
322
+ if (index >= 0) {
323
+ window.ll(createCmd('setCustomDimension'), index, value);
357
324
  }
358
325
  }
359
326
 
360
- var LocalyticsEventForwarder = {
361
- register: register
362
- };
327
+ function getOrderedCustomDimensions() {
328
+ var dimensions = null,
329
+ orderedDimensions = [];
330
+
331
+ try {
332
+ dimensions = JSON.parse(
333
+ forwarderSettings.customDimensions.replace(/&quot;/g, '"')
334
+ );
335
+ } catch (e) {
336
+ return orderedDimensions;
337
+ }
338
+
339
+ if (dimensions.length > 0) {
340
+ for (var index = 0; index < maxAllowedDimensions; index++) {
341
+ for (var j = 0; j < dimensions.length; j++) {
342
+ if (
343
+ dimensions[j].maptype.toLowerCase() !==
344
+ 'userattributeclass.name' ||
345
+ !dimensions[j].value ||
346
+ !dimensions[j].map
347
+ )
348
+ continue;
349
+
350
+ if (
351
+ dimensions[j].value.toLowerCase() ===
352
+ 'dimension ' + index
353
+ ) {
354
+ orderedDimensions.push(dimensions[j].map);
355
+ }
356
+ }
357
+ }
358
+ }
359
+
360
+ return orderedDimensions;
361
+ }
362
+
363
+ this.init = initForwarder;
364
+ this.process = processEvent;
365
+ this.setUserAttribute = setUserAttribute;
366
+ this.setUserIdentity = setUserIdentity;
367
+ this.removeUserAttribute = removeUserAttribute;
368
+ };
369
+
370
+ function getId() {
371
+ return moduleId;
372
+ }
373
+
374
+ function register(config) {
375
+ if (!config) {
376
+ console.log(
377
+ 'You must pass a config object to register the kit ' + name
378
+ );
379
+ return;
380
+ }
381
+
382
+ if (!isObject(config)) {
383
+ console.log(
384
+ "'config' must be an object. You passed in a " + typeof config
385
+ );
386
+ return;
387
+ }
388
+
389
+ if (isObject(config.kits)) {
390
+ config.kits[name] = {
391
+ constructor: constructor,
392
+ };
393
+ } else {
394
+ config.kits = {};
395
+ config.kits[name] = {
396
+ constructor: constructor,
397
+ };
398
+ }
399
+ console.log(
400
+ 'Successfully registered ' + name + ' to your mParticle configuration'
401
+ );
402
+ }
403
+
404
+ if (typeof window !== 'undefined') {
405
+ if (window && window.mParticle && window.mParticle.addForwarder) {
406
+ window.mParticle.addForwarder({
407
+ name: name,
408
+ constructor: constructor,
409
+ getId: getId,
410
+ });
411
+ }
412
+ }
413
+
414
+ var LocalyticsEventForwarder = {
415
+ register: register,
416
+ };
363
417
  var LocalyticsEventForwarder_1 = LocalyticsEventForwarder.register;
364
418
 
365
419
  exports.default = LocalyticsEventForwarder;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mparticle/web-localytics-kit",
3
- "version": "2.1.0",
3
+ "version": "3.1.0",
4
4
  "author": "mParticle Developers <developers@mparticle.com> (https://www.mparticle.com)",
5
5
  "description": "mParticle integration sdk for Localytics",
6
6
  "main": "dist/LocalyticsEventForwarder.common.js",
@@ -8,7 +8,11 @@
8
8
  "files": [
9
9
  "dist/LocalyticsEventForwarder.common.js"
10
10
  ],
11
- "repository": "https://github.com/mparticle-integrations/mparticle-javascript-integration-localytics",
11
+ "repository": {
12
+ "type": "git",
13
+ "url": "https://github.com/mParticle/mparticle-web-sdk",
14
+ "directory": "kits/localytics/localytics-4"
15
+ },
12
16
  "scripts": {
13
17
  "build": "rollup --config rollup.config.js",
14
18
  "watch": "rollup --config rollup.config.js -w",
@@ -31,7 +35,8 @@
31
35
  },
32
36
  "dependencies": {
33
37
  "isobject": "^4.0.0",
34
- "@mparticle/web-sdk": "^2.11.1"
38
+ "@mparticle/web-sdk": "^3.0.0"
35
39
  },
36
- "license": "Apache-2.0"
40
+ "license": "Apache-2.0",
41
+ "homepage": "https://github.com/mParticle/mparticle-web-sdk/tree/main/kits/localytics/localytics-4#readme"
37
42
  }