You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// 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
varname='MixpanelEventForwarder',
18
29
moduleId=10,
19
30
MessageType={
@@ -34,7 +45,8 @@ var renderSnippet = function() {
34
45
};
35
46
/* eslint-enable */
36
47
37
-
varconstructor=function(){
48
+
// eslint-disable-next-line no-redeclare
49
+
varconstructor=function(){
38
50
varself=this,
39
51
isInitialized=false,
40
52
forwarderSettings=null,
@@ -136,11 +148,89 @@ var constructor = function() {
136
148
}
137
149
}
138
150
139
-
functiononUserIdentified(user){
140
-
varidForMixpanel;
141
-
varuserIdentities=user.getUserIdentities()
151
+
functiongetUserIdentities(user){
152
+
returnuser.getUserIdentities()
142
153
? user.getUserIdentities().userIdentities
143
154
: {};
155
+
}
156
+
157
+
functiononLoginComplete(user){
158
+
varuserIdentities=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
+
functiononLogoutComplete(){
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
+
functiononIdentifyComplete(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
+
varuserIdentities=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
+
functiononModifyComplete(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
+
varuserIdentities=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
+
functionsendIdentifyRequest(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
+
varidForMixpanel;
233
+
144
234
switch(forwarderSettings.userIdentificationType){
145
235
case'CustomerId':
146
236
idForMixpanel=userIdentities.customerid;
@@ -231,7 +321,9 @@ var constructor = function() {
231
321
try{
232
322
mixpanel.mparticle.people.track_charge(
233
323
event.ProductAction.TotalAmount,
234
-
{$time: newDate().toISOString()}
324
+
{
325
+
$time: newDate().toISOString(),
326
+
}
235
327
);
236
328
}catch(e){
237
329
return'Cannot log commerce event on forwarder: '+name+': '+e;
@@ -242,8 +334,12 @@ var constructor = function() {
Copy file name to clipboardExpand all lines: dist/MixpanelEventForwarder.iife.js
+107-6Lines changed: 107 additions & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -13,6 +13,17 @@ var mpMixpanelKit = (function (exports) {
13
13
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
14
// See the License for the specific language governing permissions and
15
15
// 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
16
27
varname='MixpanelEventForwarder',
17
28
moduleId=10,
18
29
MessageType={
@@ -33,7 +44,8 @@ var mpMixpanelKit = (function (exports) {
33
44
};
34
45
/* eslint-enable */
35
46
36
-
varconstructor=function(){
47
+
// eslint-disable-next-line no-redeclare
48
+
varconstructor=function(){
37
49
varself=this,
38
50
isInitialized=false,
39
51
forwarderSettings=null,
@@ -135,11 +147,89 @@ var mpMixpanelKit = (function (exports) {
135
147
}
136
148
}
137
149
138
-
functiononUserIdentified(user){
139
-
varidForMixpanel;
140
-
varuserIdentities=user.getUserIdentities()
150
+
functiongetUserIdentities(user){
151
+
returnuser.getUserIdentities()
141
152
? user.getUserIdentities().userIdentities
142
153
: {};
154
+
}
155
+
156
+
functiononLoginComplete(user){
157
+
varuserIdentities=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
+
functiononLogoutComplete(){
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
+
functiononIdentifyComplete(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
+
varuserIdentities=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
+
functiononModifyComplete(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
+
varuserIdentities=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
+
functionsendIdentifyRequest(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
+
varidForMixpanel;
232
+
143
233
switch(forwarderSettings.userIdentificationType){
144
234
case'CustomerId':
145
235
idForMixpanel=userIdentities.customerid;
@@ -230,7 +320,9 @@ var mpMixpanelKit = (function (exports) {
230
320
try{
231
321
mixpanel.mparticle.people.track_charge(
232
322
event.ProductAction.TotalAmount,
233
-
{$time: newDate().toISOString()}
323
+
{
324
+
$time: newDate().toISOString(),
325
+
}
234
326
);
235
327
}catch(e){
236
328
return'Cannot log commerce event on forwarder: '+name+': '+e;
@@ -241,8 +333,12 @@ var mpMixpanelKit = (function (exports) {
241
333
this.process=processEvent;
242
334
this.setUserAttribute=setUserAttribute;
243
335
this.setUserIdentity=setUserIdentity;
244
-
this.onUserIdentified=onUserIdentified;
245
336
this.removeUserAttribute=removeUserAttribute;
337
+
338
+
this.onIdentifyComplete=onIdentifyComplete;
339
+
this.onLoginComplete=onLoginComplete;
340
+
this.onLogoutComplete=onLogoutComplete;
341
+
this.onModifyComplete=onModifyComplete;
246
342
};
247
343
248
344
functiongetId(){
@@ -279,6 +375,10 @@ var mpMixpanelKit = (function (exports) {
0 commit comments