@mparticle/web-mixpanel-kit 2.1.0 → 2.1.2

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.
@@ -14,6 +14,17 @@ Object.defineProperty(exports, '__esModule', { value: true });
14
14
  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
15
  // See the License for the specific language governing permissions and
16
16
  // limitations under the License.
17
+
18
+ // ** Glossary of terms
19
+ // Mixpanel Terms:
20
+ // user_id: A unique identifier used by the Mixpanel.identify method to identify a unique user. This will map to what an mParticle customer selects in the UI, which translates to the forwarderSettings.userIdentificationType.
21
+ // device_id: A unique identifier used by Mixpanel to identify an anonymous user, usually a guid. mParticle and Mixpnael generate their own device ids.
22
+ // distinct_id: A unique identifier that Mixpanel uses to bridge a user and a device. Usually the
23
+ // distinct_id has a prefix of $device:guid<device_id> to denote an anonymouse user
24
+ // and this will be replaced by the user_id once the user has been identified by
25
+ // a request to Mixpanel.identify
26
+
27
+ // eslint-disable-next-line no-redeclare
17
28
  var name = 'MixpanelEventForwarder',
18
29
  moduleId = 10,
19
30
  MessageType = {
@@ -34,7 +45,8 @@ var renderSnippet = function() {
34
45
  };
35
46
  /* eslint-enable */
36
47
 
37
- var constructor = function() {
48
+ // eslint-disable-next-line no-redeclare
49
+ var constructor = function () {
38
50
  var self = this,
39
51
  isInitialized = false,
40
52
  forwarderSettings = null,
@@ -80,9 +92,8 @@ var constructor = function() {
80
92
  reportEvent = true;
81
93
  logEvent(event);
82
94
  } else if (event.EventDataType == MessageType.PageView) {
83
- event.EventName = 'Viewed ' + event.EventName;
84
95
  reportEvent = true;
85
- logEvent(event);
96
+ logPageView(event);
86
97
  } else if (
87
98
  event.EventDataType == MessageType.Commerce &&
88
99
  event.ProductAction &&
@@ -136,11 +147,89 @@ var constructor = function() {
136
147
  }
137
148
  }
138
149
 
139
- function onUserIdentified(user) {
140
- var idForMixpanel;
141
- var userIdentities = user.getUserIdentities()
150
+ function getUserIdentities(user) {
151
+ return user.getUserIdentities()
142
152
  ? user.getUserIdentities().userIdentities
143
153
  : {};
154
+ }
155
+
156
+ function onLoginComplete(user) {
157
+ var userIdentities = getUserIdentities(user);
158
+
159
+ // When mParticle identifies a user, the user might
160
+ // actually be anonymous. We only want to send an
161
+ // identify request to Mixpanel if the user is
162
+ // actually known. Only known (logged in) users
163
+ // will have userIdentities.
164
+ if (!isEmpty(userIdentities)) {
165
+ sendIdentifyRequest(user, userIdentities);
166
+ } else {
167
+ return 'Logged in user does not have user identities and will not be sent to Mixpanel to Identify';
168
+ }
169
+ }
170
+
171
+ function onLogoutComplete() {
172
+ // For all Identity Requests, we run mixpanel.identify(<user_id>)
173
+ // as per Mixpanel's documentation https://docs.mixpanel.com/docs/tracking-methods/identifying-users
174
+ // except when a user logs out, where we run mixpanel.reset() to
175
+ // detach the Mixpanel distinct_id from the Mixpanel device_id
176
+
177
+ if (!isInitialized) {
178
+ return (
179
+ 'Cannot call logout on forwarder: ' + name + ', not initialized'
180
+ );
181
+ }
182
+
183
+ try {
184
+ mixpanel.mparticle.reset();
185
+
186
+ return 'Successfully called reset on forwarder: ' + name;
187
+ } catch (e) {
188
+ return 'Cannot call reset on forwarder: ' + name + ': ' + e;
189
+ }
190
+ }
191
+
192
+ function onIdentifyComplete(user) {
193
+ // Mixpanel considers any user with an identity to be a known user.
194
+ // In mParticle, a user will always have an MPID even if they are anonymous.
195
+ // When mParticle identifies a user, because the user might
196
+ // actually be anonymous, we only want to send an
197
+ // identify request to Mixpanel if the user is
198
+ // actually known. If a user has any user identities, they are
199
+ // considered to be "known" users.
200
+ var userIdentities = getUserIdentities(user);
201
+
202
+ if (!isEmpty(userIdentities)) {
203
+ sendIdentifyRequest(user, userIdentities);
204
+ } else {
205
+ return 'Identified user does not have user identities and will not be sent to Mixpanel to Identify';
206
+ }
207
+ }
208
+
209
+ function onModifyComplete(user) {
210
+ // Mixpanel does not have the concept of modifying a
211
+ // user's identity. For the time being, we will rely on
212
+ // doing a simple Mixpanel.identify request for backwards compatibility. However
213
+ // this method may be deprecated in a future release
214
+ var userIdentities = getUserIdentities(user);
215
+
216
+ if (!isEmpty(userIdentities)) {
217
+ sendIdentifyRequest(user, userIdentities);
218
+ } else {
219
+ return 'Modified user does not have user identities and will not be sent to Mixpanel to Identify';
220
+ }
221
+ }
222
+
223
+ function sendIdentifyRequest(user, userIdentities) {
224
+ // We should only make Mixpanel.identify requests
225
+ // when a user has userIdentities and is therefore
226
+ // a known mParticle user and not anonymous
227
+ if (isEmpty(userIdentities)) {
228
+ return;
229
+ }
230
+
231
+ var idForMixpanel;
232
+
144
233
  switch (forwarderSettings.userIdentificationType) {
145
234
  case 'CustomerId':
146
235
  idForMixpanel = userIdentities.customerid;
@@ -209,6 +298,17 @@ var constructor = function() {
209
298
  }
210
299
  }
211
300
 
301
+ function logPageView(event) {
302
+ var eventName = 'Viewed ' + event.EventName;
303
+ event.EventAttributes = event.EventAttributes || {};
304
+
305
+ try {
306
+ mixpanel.mparticle.track(eventName, event.EventAttributes);
307
+ } catch (e) {
308
+ return 'Cannot log event on forwarder: ' + name + ': ' + e;
309
+ }
310
+ }
311
+
212
312
  function logEvent(event) {
213
313
  event.EventAttributes = event.EventAttributes || {};
214
314
 
@@ -231,7 +331,9 @@ var constructor = function() {
231
331
  try {
232
332
  mixpanel.mparticle.people.track_charge(
233
333
  event.ProductAction.TotalAmount,
234
- { $time: new Date().toISOString() }
334
+ {
335
+ $time: new Date().toISOString(),
336
+ }
235
337
  );
236
338
  } catch (e) {
237
339
  return 'Cannot log commerce event on forwarder: ' + name + ': ' + e;
@@ -242,8 +344,12 @@ var constructor = function() {
242
344
  this.process = processEvent;
243
345
  this.setUserAttribute = setUserAttribute;
244
346
  this.setUserIdentity = setUserIdentity;
245
- this.onUserIdentified = onUserIdentified;
246
347
  this.removeUserAttribute = removeUserAttribute;
348
+
349
+ this.onIdentifyComplete = onIdentifyComplete;
350
+ this.onLoginComplete = onLoginComplete;
351
+ this.onLogoutComplete = onLogoutComplete;
352
+ this.onModifyComplete = onModifyComplete;
247
353
  };
248
354
 
249
355
  function getId() {
@@ -280,6 +386,10 @@ function register(config) {
280
386
  );
281
387
  }
282
388
 
389
+ function isEmpty(value) {
390
+ return value == null || !(Object.keys(value) || value).length;
391
+ }
392
+
283
393
  function isObject(val) {
284
394
  return (
285
395
  val != null && typeof val === 'object' && Array.isArray(val) === false
@@ -296,6 +406,7 @@ if (typeof window !== 'undefined') {
296
406
  }
297
407
  }
298
408
 
409
+ // eslint-disable-next-line no-undef
299
410
  var MixpanelEventForwarder = {
300
411
  register: register,
301
412
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mparticle/web-mixpanel-kit",
3
- "version": "2.1.0",
3
+ "version": "2.1.2",
4
4
  "author": "mParticle Developers <developers@mparticle.com> (https://www.mparticle.com)",
5
5
  "description": "mParticle integration sdk for Mixpanel",
6
6
  "browser": "dist/MixpanelEventForwarder.common.js",
@@ -11,20 +11,26 @@
11
11
  "repository": "https://github.com/mparticle-integrations/mparticle-javascript-integration-mixpanel",
12
12
  "scripts": {
13
13
  "build": "rollup --config rollup.config.js",
14
+ "build:test": "rollup --config rollup.test.config.js",
14
15
  "watch": "rollup --config rollup.config.js -w",
15
- "test": "node test/boilerplate/test-karma.js"
16
+ "test": "npm run build && npm run build:test && DEBUG=false karma start test/karma.config.js",
17
+ "test:debug": "npm run build && npm run build:test && DEBUG=true karma start test/karma.config.js"
16
18
  },
17
19
  "devDependencies": {
20
+ "@mparticle/web-sdk": "^2.23.7",
21
+ "@semantic-release/changelog": "^5.0.1",
22
+ "@semantic-release/exec": "^5.0.0",
23
+ "@semantic-release/git": "^9.0.0",
18
24
  "chai": "^4.2.0",
19
- "eslint-config-prettier": "6.3.0",
20
- "eslint-plugin-prettier": "3.1.1",
25
+ "eslint-config-prettier": "9.0.0",
26
+ "eslint-plugin-prettier": "5.0.1",
21
27
  "karma": "^5.1.0",
22
28
  "karma-chai": "^0.1.0",
23
29
  "karma-chrome-launcher": "^3.1.0",
24
30
  "karma-firefox-launcher": "^1.3.0",
25
31
  "karma-mocha": "^2.0.1",
26
32
  "mocha": "^2.5.3",
27
- "prettier": "^2.2.1",
33
+ "prettier": "^3.0.3",
28
34
  "rollup": "^1.13.1",
29
35
  "rollup-plugin-commonjs": "^10.0.0",
30
36
  "rollup-plugin-node-resolve": "^5.0.1",
@@ -32,8 +38,5 @@
32
38
  "should": "^7.1.0",
33
39
  "watchify": "^3.11.1"
34
40
  },
35
- "dependencies": {
36
- "@mparticle/web-sdk": "^2.11.8"
37
- },
38
41
  "license": "Apache-2.0"
39
42
  }
package/CHANGELOG.md DELETED
@@ -1,6 +0,0 @@
1
- ## Releases
2
-
3
- --
4
- #### 2.1.0 - 2021-06-28
5
-
6
- - feat: Support EU Data Residency