-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathInspectlet.js
214 lines (181 loc) · 6.89 KB
/
Inspectlet.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
var mpInspectletKit = (function (exports) {
/*!
* isobject <https://github.com/jonschlinkert/isobject>
*
* Copyright (c) 2014-2017, Jon Schlinkert.
* Released under the MIT License.
*/
function isObject(val) {
return val != null && typeof val === 'object' && Array.isArray(val) === false;
}
//
// Copyright 2015 mParticle, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
var MessageType = {
SessionStart: 1,
SessionEnd: 2,
PageView: 3,
PageEvent: 4,
CrashReport: 5,
OptOut: 6,
Commerce: 16
},
name = 'Inspectlet',
moduleId = 61;
var constructor = function () {
var self = this,
isInitialized = false,
forwarderSettings,
reportingService,
isTesting = false;
self.name = name;
function getIdentityTypeName(identityType) {
return mParticle.IdentityType.getName(identityType);
}
function reportEvent(event) {
if (reportingService) {
reportingService(self, event);
}
}
function processEvent(event) {
if (isInitialized) {
try {
if (event.EventDataType == MessageType.PageEvent) {
if (event.EventCategory == window.mParticle.EventType.Transaction) {
logTransaction(event);
reportEvent(event);
return 'Successfully sent to ' + name;
}
else if (event.EventCategory == window.mParticle.EventType.Navigation) {
__insp.push(["virtualPage"]);
reportEvent(event);
return 'Successfully sent virtual page view to ' + name;
}
}
else if (event.EventDataType == MessageType.Commerce) {
logTransaction(event);
}
return 'Ignoring non-transaction event for ' + name;
}
catch (e) {
return 'Failed to send to: ' + name + ' ' + e;
}
}
return 'Can\'t send to forwarder ' + name + ', not initialized';
}
function logTransaction(data) {
__insp.push(['tagSession', "purchase"]);
}
function setUserAttribute(key, value) {
var attributeDict;
if (isInitialized) {
if (value) {
attributeDict = {};
attributeDict[key] = value;
__insp.push(['tagSession', attributeDict]);
}
else {
__insp.push(['tagSession', key]);
}
}
else {
return 'Can\'t call setUserAttribute on forwarder ' + name + ', not initialized';
}
}
function setUserIdentity(id, type) {
if (isInitialized) {
setUserAttribute(getIdentityTypeName(type), id);
}
else {
return 'Can\'t call setUserIdentity on forwarder ' + name + ', not initialized';
}
}
function initForwarder(settings, service, testMode) {
forwarderSettings = settings;
reportingService = service;
isTesting = testMode;
try {
function addInspectlet() {
function __ldinsp() {
var insp = document.createElement('script');
insp.type = 'text/javascript';
insp.async = true;
insp.id = "inspsync";
insp.src = ('https:' == document.location.protocol ? 'https' : 'http') + '://cdn.inspectlet.com/inspectlet.js';
var head = document.getElementsByTagName('head')[0];
head.appendChild(insp);
}
if (window.attachEvent) {
window.attachEvent('onload', __ldinsp);
}
else {
window.addEventListener('load', __ldinsp, false);
}
}
window.__insp = window.__insp || [];
__insp.push(['wid', forwarderSettings.wId]);
if (isTesting === false) {
addInspectlet();
}
isInitialized = true;
return 'Successfully initialized: ' + name;
}
catch (e) {
return 'Failed to initialize: ' + name;
}
}
this.init = initForwarder;
this.process = processEvent;
this.setUserAttribute = setUserAttribute;
this.setUserIdentity = setUserIdentity;
};
function getId() {
return moduleId;
}
function register(config) {
if (!config) {
window.console.log('You must pass a config object to register the kit ' + name);
return;
}
if (!isObject(config)) {
window.console.log('\'config\' must be an object. You passed in a ' + typeof config);
return;
}
if (isObject(config.kits)) {
config.kits[name] = {
constructor: constructor
};
} else {
config.kits = {};
config.kits[name] = {
constructor: constructor
};
}
window.console.log('Successfully registered ' + name + ' to your mParticle configuration');
}
if (window && window.mParticle && window.mParticle.addForwarder) {
window.mParticle.addForwarder({
name: name,
constructor: constructor,
getId: getId
});
}
var Inspectlet = {
register: register
};
var Inspectlet_1 = Inspectlet.register;
exports.default = Inspectlet;
exports.register = Inspectlet_1;
return exports;
}({}));