Skip to content

Commit 3283e3a

Browse files
chore(build): Generate latest bundle [skip ci]
1 parent 391517d commit 3283e3a

File tree

2 files changed

+214
-12
lines changed

2 files changed

+214
-12
lines changed

dist/MixpanelEventForwarder.common.js

Lines changed: 107 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,17 @@ Object.defineProperty(exports, '__esModule', { value: true });
1414
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1515
// See the License for the specific language governing permissions and
1616
// 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
1728
var name = 'MixpanelEventForwarder',
1829
moduleId = 10,
1930
MessageType = {
@@ -34,7 +45,8 @@ var renderSnippet = function() {
3445
};
3546
/* eslint-enable */
3647

37-
var constructor = function() {
48+
// eslint-disable-next-line no-redeclare
49+
var constructor = function () {
3850
var self = this,
3951
isInitialized = false,
4052
forwarderSettings = null,
@@ -136,11 +148,89 @@ var constructor = function() {
136148
}
137149
}
138150

139-
function onUserIdentified(user) {
140-
var idForMixpanel;
141-
var userIdentities = user.getUserIdentities()
151+
function getUserIdentities(user) {
152+
return user.getUserIdentities()
142153
? user.getUserIdentities().userIdentities
143154
: {};
155+
}
156+
157+
function onLoginComplete(user) {
158+
var userIdentities = getUserIdentities(user);
159+
160+
// When mParticle identifies a user, the user might
161+
// actually be anonymous. We only want to send an
162+
// identify request to Mixpanel if the user is
163+
// actually known. Only known (logged in) users
164+
// will have userIdentities.
165+
if (!isEmpty(userIdentities)) {
166+
sendIdentifyRequest(user, userIdentities);
167+
} else {
168+
return 'Logged in user does not have user identities and will not be sent to Mixpanel to Identify';
169+
}
170+
}
171+
172+
function onLogoutComplete() {
173+
// For all Identity Requests, we run mixpanel.identify(<user_id>)
174+
// as per Mixpanel's documentation https://docs.mixpanel.com/docs/tracking-methods/identifying-users
175+
// except when a user logs out, where we run mixpanel.reset() to
176+
// detach the Mixpanel distinct_id from the Mixpanel device_id
177+
178+
if (!isInitialized) {
179+
return (
180+
'Cannot call logout on forwarder: ' + name + ', not initialized'
181+
);
182+
}
183+
184+
try {
185+
mixpanel.mparticle.reset();
186+
187+
return 'Successfully called reset on forwarder: ' + name;
188+
} catch (e) {
189+
return 'Cannot call reset on forwarder: ' + name + ': ' + e;
190+
}
191+
}
192+
193+
function onIdentifyComplete(user) {
194+
// Mixpanel considers any user with an identity to be a known user.
195+
// In mParticle, a user will always have an MPID even if they are anonymous.
196+
// When mParticle identifies a user, because the user might
197+
// actually be anonymous, we only want to send an
198+
// identify request to Mixpanel if the user is
199+
// actually known. If a user has any user identities, they are
200+
// considered to be "known" users.
201+
var userIdentities = getUserIdentities(user);
202+
203+
if (!isEmpty(userIdentities)) {
204+
sendIdentifyRequest(user, userIdentities);
205+
} else {
206+
return 'Identified user does not have user identities and will not be sent to Mixpanel to Identify';
207+
}
208+
}
209+
210+
function onModifyComplete(user) {
211+
// Mixpanel does not have the concept of modifying a
212+
// user's identity. For the time being, we will rely on
213+
// doing a simple Mixpanel.identify request for backwards compatibility. However
214+
// this method may be deprecated in a future release
215+
var userIdentities = getUserIdentities(user);
216+
217+
if (!isEmpty(userIdentities)) {
218+
sendIdentifyRequest(user, userIdentities);
219+
} else {
220+
return 'Modified user does not have user identities and will not be sent to Mixpanel to Identify';
221+
}
222+
}
223+
224+
function sendIdentifyRequest(user, userIdentities) {
225+
// We should only make Mixpanel.identify requests
226+
// when a user has userIdentities and is therefore
227+
// a known mParticle user and not anonymous
228+
if (isEmpty(userIdentities)) {
229+
return;
230+
}
231+
232+
var idForMixpanel;
233+
144234
switch (forwarderSettings.userIdentificationType) {
145235
case 'CustomerId':
146236
idForMixpanel = userIdentities.customerid;
@@ -231,7 +321,9 @@ var constructor = function() {
231321
try {
232322
mixpanel.mparticle.people.track_charge(
233323
event.ProductAction.TotalAmount,
234-
{ $time: new Date().toISOString() }
324+
{
325+
$time: new Date().toISOString(),
326+
}
235327
);
236328
} catch (e) {
237329
return 'Cannot log commerce event on forwarder: ' + name + ': ' + e;
@@ -242,8 +334,12 @@ var constructor = function() {
242334
this.process = processEvent;
243335
this.setUserAttribute = setUserAttribute;
244336
this.setUserIdentity = setUserIdentity;
245-
this.onUserIdentified = onUserIdentified;
246337
this.removeUserAttribute = removeUserAttribute;
338+
339+
this.onIdentifyComplete = onIdentifyComplete;
340+
this.onLoginComplete = onLoginComplete;
341+
this.onLogoutComplete = onLogoutComplete;
342+
this.onModifyComplete = onModifyComplete;
247343
};
248344

249345
function getId() {
@@ -280,6 +376,10 @@ function register(config) {
280376
);
281377
}
282378

379+
function isEmpty(value) {
380+
return value == null || !(Object.keys(value) || value).length;
381+
}
382+
283383
function isObject(val) {
284384
return (
285385
val != null && typeof val === 'object' && Array.isArray(val) === false
@@ -296,6 +396,7 @@ if (typeof window !== 'undefined') {
296396
}
297397
}
298398

399+
// eslint-disable-next-line no-undef
299400
var MixpanelEventForwarder = {
300401
register: register,
301402
};

dist/MixpanelEventForwarder.iife.js

Lines changed: 107 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,17 @@ var mpMixpanelKit = (function (exports) {
1313
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1414
// See the License for the specific language governing permissions and
1515
// limitations under the License.
16+
17+
// ** Glossary of terms
18+
// Mixpanel Terms:
19+
// 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.
20+
// device_id: A unique identifier used by Mixpanel to identify an anonymous user, usually a guid. mParticle and Mixpnael generate their own device ids.
21+
// distinct_id: A unique identifier that Mixpanel uses to bridge a user and a device. Usually the
22+
// distinct_id has a prefix of $device:guid<device_id> to denote an anonymouse user
23+
// and this will be replaced by the user_id once the user has been identified by
24+
// a request to Mixpanel.identify
25+
26+
// eslint-disable-next-line no-redeclare
1627
var name = 'MixpanelEventForwarder',
1728
moduleId = 10,
1829
MessageType = {
@@ -33,7 +44,8 @@ var mpMixpanelKit = (function (exports) {
3344
};
3445
/* eslint-enable */
3546

36-
var constructor = function() {
47+
// eslint-disable-next-line no-redeclare
48+
var constructor = function () {
3749
var self = this,
3850
isInitialized = false,
3951
forwarderSettings = null,
@@ -135,11 +147,89 @@ var mpMixpanelKit = (function (exports) {
135147
}
136148
}
137149

138-
function onUserIdentified(user) {
139-
var idForMixpanel;
140-
var userIdentities = user.getUserIdentities()
150+
function getUserIdentities(user) {
151+
return user.getUserIdentities()
141152
? user.getUserIdentities().userIdentities
142153
: {};
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+
143233
switch (forwarderSettings.userIdentificationType) {
144234
case 'CustomerId':
145235
idForMixpanel = userIdentities.customerid;
@@ -230,7 +320,9 @@ var mpMixpanelKit = (function (exports) {
230320
try {
231321
mixpanel.mparticle.people.track_charge(
232322
event.ProductAction.TotalAmount,
233-
{ $time: new Date().toISOString() }
323+
{
324+
$time: new Date().toISOString(),
325+
}
234326
);
235327
} catch (e) {
236328
return 'Cannot log commerce event on forwarder: ' + name + ': ' + e;
@@ -241,8 +333,12 @@ var mpMixpanelKit = (function (exports) {
241333
this.process = processEvent;
242334
this.setUserAttribute = setUserAttribute;
243335
this.setUserIdentity = setUserIdentity;
244-
this.onUserIdentified = onUserIdentified;
245336
this.removeUserAttribute = removeUserAttribute;
337+
338+
this.onIdentifyComplete = onIdentifyComplete;
339+
this.onLoginComplete = onLoginComplete;
340+
this.onLogoutComplete = onLogoutComplete;
341+
this.onModifyComplete = onModifyComplete;
246342
};
247343

248344
function getId() {
@@ -279,6 +375,10 @@ var mpMixpanelKit = (function (exports) {
279375
);
280376
}
281377

378+
function isEmpty(value) {
379+
return value == null || !(Object.keys(value) || value).length;
380+
}
381+
282382
function isObject(val) {
283383
return (
284384
val != null && typeof val === 'object' && Array.isArray(val) === false
@@ -295,6 +395,7 @@ var mpMixpanelKit = (function (exports) {
295395
}
296396
}
297397

398+
// eslint-disable-next-line no-undef
298399
var MixpanelEventForwarder = {
299400
register: register,
300401
};

0 commit comments

Comments
 (0)