Skip to content

Commit c7653ea

Browse files
committed
Update rollup to build IIFE and CJS, refactor register
1 parent 3618841 commit c7653ea

9 files changed

Lines changed: 1209 additions & 820 deletions

LocalyticsEventForwarder.js

Lines changed: 367 additions & 356 deletions
Large diffs are not rendered by default.
Lines changed: 374 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,374 @@
1+
Object.defineProperty(exports, '__esModule', { value: true });
2+
3+
/*!
4+
* isobject <https://github.com/jonschlinkert/isobject>
5+
*
6+
* Copyright (c) 2014-2017, Jon Schlinkert.
7+
* Released under the MIT License.
8+
*/
9+
10+
function isObject(val) {
11+
return val != null && typeof val === 'object' && Array.isArray(val) === false;
12+
}
13+
14+
var isobject = /*#__PURE__*/Object.freeze({
15+
'default': isObject
16+
});
17+
18+
function getCjsExportFromNamespace (n) {
19+
return n && n['default'] || n;
20+
}
21+
22+
var isobject$1 = getCjsExportFromNamespace(isobject);
23+
24+
// Copyright 2015 mParticle, Inc.
25+
//
26+
// Licensed under the Apache License, Version 2.0 (the "License");
27+
// you may not use this file except in compliance with the License.
28+
// You may obtain a copy of the License at
29+
//
30+
// http://www.apache.org/licenses/LICENSE-2.0
31+
//
32+
// Unless required by applicable law or agreed to in writing, software
33+
// distributed under the License is distributed on an "AS IS" BASIS,
34+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
35+
// See the License for the specific language governing permissions and
36+
// limitations under the License.
37+
38+
39+
40+
var name = 'LocalyticsEventForwarder',
41+
moduleId = 84,
42+
maxAllowedDimensions = 10,
43+
trackerCount = 1,
44+
MessageType = {
45+
PageView : 3,
46+
PageEvent : 4,
47+
Commerce : 16
48+
},
49+
EventType = {
50+
Unknown : 0,
51+
Navigation : 1,
52+
Location : 2,
53+
Search : 3,
54+
Transaction : 4,
55+
UserContent : 5,
56+
UserPreference: 6,
57+
Social : 7,
58+
Other : 8,
59+
Media : 9
60+
};
61+
62+
var constructor = function () {
63+
var self = this,
64+
isInitialized = false,
65+
forwarderSettings = null,
66+
reportingService = null,
67+
customDimensions = [],
68+
initOptions = {},
69+
trackerId = null;
70+
71+
self.name = name;
72+
73+
function createTrackerId() {
74+
return 'mpllTracker' + trackerCount++;
75+
}
76+
77+
function createCmd(cmd) {
78+
// Prepends the specified command with the tracker id
79+
return cmd + '.' + trackerId;
80+
}
81+
82+
function initForwarder(settings, service, testMode, tid) {
83+
forwarderSettings = settings;
84+
reportingService = service;
85+
initOptions = getInitOptions();
86+
customDimensions = getOrderedCustomDimensions();
87+
88+
if (!tid) {
89+
trackerId = createTrackerId();
90+
}
91+
else {
92+
trackerId = tid;
93+
}
94+
95+
try {
96+
if (!testMode) {
97+
!function(l,y,t,i,c,s) {
98+
l['LocalyticsGlobal'] = i;
99+
l[i] = function() { (l[i].q = l[i].q || []).push(arguments); };
100+
l[i].t = +new Date;
101+
(s = y.createElement(t)).type = 'text/javascript';
102+
s.src = '//web.localytics.com/v3/localytics.js';
103+
(c = y.getElementsByTagName(t)[0]).parentNode.insertBefore(s, c);
104+
}(window, document, 'script', 'll');
105+
106+
window.ll(createCmd('init'), settings.appKey, initOptions);
107+
}
108+
109+
isInitialized = true;
110+
111+
return 'Successfully initialized: ' + name;
112+
113+
} catch (e) {
114+
return 'Can\'t initialize forwarder: '+ name +':' + e;
115+
}
116+
}
117+
118+
function getInitOptions() {
119+
var options = {};
120+
121+
if(forwarderSettings.appVersion){
122+
options.appVersion = forwarderSettings.appVersion;
123+
}
124+
125+
if(forwarderSettings.sessionTimeout){
126+
options.sessionTimeout = forwarderSettings.sessionTimeout;
127+
}
128+
129+
if(forwarderSettings.domain){
130+
options.domain = forwarderSettings.domain;
131+
}
132+
133+
options.trackPageView = forwarderSettings.hasOwnProperty('trackPageView') &&
134+
forwarderSettings.trackPageView.toLowerCase() === 'true';
135+
136+
return options;
137+
}
138+
139+
function processEvent(event) {
140+
var reportEvent = false;
141+
142+
if(!isInitialized) {
143+
return 'Can\'t send forwarder '+ name + ', not initialized';
144+
}
145+
146+
try {
147+
if(event.EventDataType == MessageType.PageView) {
148+
reportEvent = true;
149+
logScreenViewEvent(event);
150+
} else if (event.EventDataType == MessageType.Commerce) {
151+
logCommerce(event);
152+
reportEvent = true;
153+
}
154+
else if (event.EventDataType == MessageType.PageEvent) {
155+
reportEvent = true;
156+
logEvent(event);
157+
}
158+
159+
if(reportEvent && reportingService) {
160+
reportingService(self, event);
161+
}
162+
163+
return 'Successfully sent to forwarder ' + name;
164+
} catch (e) {
165+
return 'Can\'t send to forwarder: ' + name + ' ' + e;
166+
}
167+
}
168+
169+
function logScreenViewEvent(event) {
170+
var screenName = event.EventName;
171+
172+
if(event.hasOwnProperty('CustomFlags') && event.CustomFlags.hasOwnProperty("Localytics.ScreenName")) {
173+
screenName = event.CustomFlags["Localytics.ScreenName"];
174+
}
175+
176+
window.ll(createCmd('tagScreen'), screenName);
177+
}
178+
179+
function logEvent(event) {
180+
var ltv = null;
181+
if(event.hasOwnProperty('EventCategory') && event.EventCategory === EventType.Transaction) {
182+
if(event.hasOwnProperty('EventAttributes') && event.EventAttributes.hasOwnProperty("$Amount")) {
183+
var ltvVal = event.EventAttributes["$Amount"];
184+
if(!isNaN(parseFloat(ltvVal, 10))) {
185+
ltv = parseFloat(ltvVal, 10).toFixed(2);
186+
}
187+
}
188+
}
189+
190+
if(ltv !== null) {
191+
var multiplier = forwarderSettings.trackClvAsRawValue ? 1 : 100;
192+
window.ll(createCmd('tagEvent'), event.EventName, event.EventAttributes, ltv * multiplier);
193+
} else {
194+
window.ll(createCmd('tagEvent'), event.EventName, event.EventAttributes);
195+
}
196+
}
197+
198+
function logCommerce(data) {
199+
200+
if(!data.ProductAction)
201+
return 'Can\'t forward commerce event: No production action found';
202+
203+
switch (data.ProductAction.ProductActionType) {
204+
case mParticle.ProductActionType.Purchase:
205+
case mParticle.ProductActionType.Refund:
206+
var total = null,
207+
multiplier = forwarderSettings.trackClvAsRawValue ? 1 : 100;
208+
209+
if (data.ProductAction.ProductActionType == mParticle.ProductActionType.Refund) {
210+
multiplier *= -1;
211+
}
212+
213+
if(!isNaN(parseFloat(data.ProductAction.TotalAmount, 10))) {
214+
total = parseFloat(data.ProductAction.TotalAmount, 10).toFixed(2);
215+
}
216+
217+
if(total != null) {
218+
window.ll(createCmd('tagEvent'), data.EventName, data.EventAttributes, total * multiplier);
219+
} else {
220+
return 'Can\'t forward commerce event: TotalAmount is set incorrectly';
221+
}
222+
break;
223+
224+
default:
225+
logEvent(data);
226+
break;
227+
}
228+
}
229+
230+
function setUserAttribute(key, value) {
231+
if (!key) {
232+
return 'Can\'t call setUserAttribute on forwarder: No key provided';
233+
}
234+
235+
if(key === 'Localytics.CustomerName') {
236+
try {
237+
window.ll(createCmd('setCustomerName'), value);
238+
}
239+
catch(e) {
240+
return 'Can\'t call setCustomerName on forwarder: ' + name + ': ' + e;
241+
}
242+
} else {
243+
if(customDimensions.length > 0) {
244+
updateCustomDimension(key, value);
245+
}
246+
}
247+
}
248+
249+
function removeUserAttribute(key) {
250+
if(customDimensions.length > 0) {
251+
updateCustomDimension(key, null);
252+
}
253+
}
254+
255+
function setUserIdentity(id, type) {
256+
if (!id) {
257+
return 'Can\'t call setUserIdentity on forwarder: ' + name + ' without ID';
258+
}
259+
260+
if (!isInitialized) {
261+
return 'Can\'t call setUserIdentity on forwarder: ' + name + ', not initialized';
262+
}
263+
264+
try {
265+
if(window.mParticle.IdentityType.Email == type) {
266+
window.ll(createCmd('setCustomerEmail'), id);
267+
}
268+
else {
269+
window.ll(createCmd('setCustomerId'), id);
270+
}
271+
272+
return 'Successfully called identify on forwarder: ' + name;
273+
}
274+
catch (e) {
275+
return 'Can\'t call identify on forwarder: ' + name + ': ' + e;
276+
}
277+
}
278+
279+
function updateCustomDimension(key, value) {
280+
if(customDimensions.length === 0)
281+
return;
282+
283+
var index = -1,
284+
keyLower = key.toLowerCase();
285+
286+
for (var i = 0; i < customDimensions.length; i++) {
287+
if(customDimensions[i].toLowerCase() === keyLower) {
288+
index = i;
289+
}
290+
}
291+
292+
if(index >= 0) {
293+
window.ll(createCmd('setCustomDimension'), index, value);
294+
}
295+
}
296+
297+
function getOrderedCustomDimensions() {
298+
var dimensions = null,
299+
orderedDimensions = [];
300+
301+
try {
302+
dimensions = JSON.parse(forwarderSettings.customDimensions.replace(/&quot;/g,'"'));
303+
} catch (e) {
304+
return orderedDimensions;
305+
}
306+
307+
if(dimensions.length > 0) {
308+
for (var index = 0; index < maxAllowedDimensions; index++) {
309+
for (var j = 0; j < dimensions.length; j++) {
310+
if(dimensions[j].maptype.toLowerCase() !== 'userattributeclass.name' ||
311+
!dimensions[j].value ||
312+
!dimensions[j].map)
313+
continue;
314+
315+
if(dimensions[j].value.toLowerCase() === 'dimension ' + index) {
316+
orderedDimensions.push(dimensions[j].map);
317+
}
318+
}
319+
}
320+
}
321+
322+
return orderedDimensions;
323+
}
324+
325+
this.init = initForwarder;
326+
this.process = processEvent;
327+
this.setUserAttribute = setUserAttribute;
328+
this.setUserIdentity = setUserIdentity;
329+
this.removeUserAttribute = removeUserAttribute;
330+
};
331+
332+
function getId() {
333+
return moduleId;
334+
}
335+
336+
function register(config) {
337+
if (!config) {
338+
window.console.log('You must pass a config object to register the kit ' + name);
339+
return;
340+
}
341+
342+
if (!isobject$1(config)) {
343+
window.console.log('\'config\' must be an object. You passed in a ' + typeof config);
344+
return;
345+
}
346+
347+
if (isobject$1(config.kits)) {
348+
config.kits[name] = {
349+
constructor: constructor
350+
};
351+
} else {
352+
config.kits = {};
353+
config.kits[name] = {
354+
constructor: constructor
355+
};
356+
}
357+
window.console.log('Successfully registered ' + name + ' to your mParticle configuration');
358+
}
359+
360+
if (window && window.mParticle && window.mParticle.addForwarder) {
361+
window.mParticle.addForwarder({
362+
name: name,
363+
constructor: constructor,
364+
getId: getId
365+
});
366+
}
367+
368+
var LocalyticsEventForwarder = {
369+
register: register
370+
};
371+
var LocalyticsEventForwarder_1 = LocalyticsEventForwarder.register;
372+
373+
exports.default = LocalyticsEventForwarder;
374+
exports.register = LocalyticsEventForwarder_1;

0 commit comments

Comments
 (0)