Skip to content

Commit

Permalink
Add additional track props for tap event
Browse files Browse the repository at this point in the history
  • Loading branch information
jbwyme committed Feb 4, 2020
1 parent 6752289 commit bad826f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
5 changes: 5 additions & 0 deletions Mixpanel/Mixpanel.h
Original file line number Diff line number Diff line change
Expand Up @@ -806,6 +806,11 @@ extern NSString *const MPNotificationTypeTakeover;
*/
+ (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler API_AVAILABLE(ios(10.0), macos(10.14), watchos(6.0)) API_UNAVAILABLE(tvos);

/*!
Internal utility for push notification-related event tracking
*/
- (void)trackPushNotification:(NSDictionary *)userInfo event:(NSString *)event properties:(NSDictionary *)properties;

#if !MIXPANEL_NO_NOTIFICATION_AB_TEST_SUPPORT
#pragma mark - Mixpanel Notifications

Expand Down
39 changes: 26 additions & 13 deletions Mixpanel/Mixpanel.m
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ - (instancetype)initWithToken:(NSString *)apiToken
[self setupAutomaticPushTracking];
NSDictionary *remoteNotification = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey];
if (remoteNotification) {
[self trackPushNotification:remoteNotification event:@"$app_open"];
[self trackPushNotification:remoteNotification event:@"$app_open" properties:@{}];
}
}
#endif
Expand Down Expand Up @@ -804,7 +804,7 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N
}
}

- (void)trackPushNotification:(NSDictionary *)userInfo event:(NSString *)event
- (void)trackPushNotification:(NSDictionary *)userInfo event:(NSString *)event properties:(NSDictionary *)properties
{
MPLogInfo(@"%@ tracking push payload %@", self, userInfo);

Expand All @@ -813,6 +813,8 @@ - (void)trackPushNotification:(NSDictionary *)userInfo event:(NSString *)event

NSDictionary *mpPayload = [rawMp isKindOfClass:[NSDictionary class]] ? rawMp : nil;

[mpPayload setValuesForKeysWithDictionary:properties];

if (mpPayload[@"m"] && mpPayload[@"c"]) {
NSMutableDictionary *properties = [mpPayload mutableCopy];
properties[@"campaign_id"] = mpPayload[@"c"];
Expand All @@ -830,7 +832,7 @@ - (void)trackPushNotification:(NSDictionary *)userInfo event:(NSString *)event

- (void)trackPushNotification:(NSDictionary *)userInfo
{
[self trackPushNotification:userInfo event:@"$campaign_received"];
[self trackPushNotification:userInfo event:@"$campaign_received" properties:@{}];
}
#endif

Expand Down Expand Up @@ -1874,23 +1876,22 @@ + (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNoti
return;
}

NSDictionary *userInfo = response.notification.request.content.userInfo;
UNNotificationRequest *request = response.notification.request;
NSDictionary *userInfo = request.content.userInfo;

// Initialize properties to track to Mixpanel
NSMutableDictionary *trackingProps = [[NSMutableDictionary alloc] init];
[trackingProps setValuesForKeysWithDictionary:@{
@"campaign_id": [userInfo valueForKeyPath:@"mp.c"],
@"message_id": [userInfo valueForKeyPath:@"mp.m"],
NSMutableDictionary *additionalTrackingProps = [[NSMutableDictionary alloc] init];
[additionalTrackingProps setValuesForKeysWithDictionary:@{
@"notification_id": request.identifier,
}];


MPLogInfo(@"%@ didReceiveNotificationResponse action: %@", self, response.actionIdentifier);

// If the notification was dismissed, just track and return
if ([response.actionIdentifier isEqualToString:UNNotificationDismissActionIdentifier]) {
[instances.allKeys enumerateObjectsUsingBlock:^(NSString *token, NSUInteger idx, BOOL * _Nonnull stop) {
Mixpanel *instance = instances[token];
[instance track:@"$push_notification_dismissed" properties:trackingProps];
[instance track:@"$push_notification_dismissed" properties:additionalTrackingProps];
[instance flush];
}];
completionHandler();
Expand All @@ -1901,7 +1902,7 @@ + (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNoti

if ([response.actionIdentifier isEqualToString:UNNotificationDefaultActionIdentifier]) {
// The action that indicates the user opened the app from the notification interface.
[trackingProps setValue:@"notification" forKey:@"tap_target"];
[additionalTrackingProps setValue:@"notification" forKey:@"tap_target"];
if (userInfo[@"mp_ontap"]) {
ontap = userInfo[@"mp_ontap"];
}
Expand All @@ -1913,18 +1914,30 @@ + (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNoti
NSInteger idx = [[response.actionIdentifier stringByReplacingOccurrencesOfString:@"MP_ACTION_" withString:@""] integerValue];
NSDictionary *buttonDict = buttons[idx];
ontap = buttonDict[@"ontap"];
[trackingProps setValuesForKeysWithDictionary:@{
[additionalTrackingProps setValuesForKeysWithDictionary:@{
@"button_id": buttonDict[@"id"],
@"button_label": buttonDict[@"lbl"],
@"tap_target": @"button",
}];
}
}

// Add additional tracking props
if (ontap != nil && ontap != (id)[NSNull null]) {
NSString *tapActionType = ontap[@"type"];
if (tapActionType != nil) {
additionalTrackingProps[@"tap_action_type"] = tapActionType;
}
NSString *tapActionUri = ontap[@"uri"];
if (tapActionUri != nil) {
additionalTrackingProps[@"tap_action_uri"] = tapActionUri;
}
}

// Track tap event to all Mixpanel instances
[instances.allKeys enumerateObjectsUsingBlock:^(NSString *token, NSUInteger idx, BOOL * _Nonnull stop) {
Mixpanel *instance = instances[token];
[instance track:@"$push_notification_tap" properties:trackingProps];
[instance trackPushNotification:userInfo event:@"$push_notification_tap" properties:additionalTrackingProps];
[instance flush];
}];

Expand Down

0 comments on commit bad826f

Please sign in to comment.