From bad826f5f5b688cfa75c7620746506ca84801978 Mon Sep 17 00:00:00 2001 From: Josh Wymer Date: Mon, 3 Feb 2020 22:44:03 -0600 Subject: [PATCH] Add additional track props for tap event --- Mixpanel/Mixpanel.h | 5 +++++ Mixpanel/Mixpanel.m | 39 ++++++++++++++++++++++++++------------- 2 files changed, 31 insertions(+), 13 deletions(-) diff --git a/Mixpanel/Mixpanel.h b/Mixpanel/Mixpanel.h index ceb629ee3..ebe3515c1 100644 --- a/Mixpanel/Mixpanel.h +++ b/Mixpanel/Mixpanel.h @@ -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 diff --git a/Mixpanel/Mixpanel.m b/Mixpanel/Mixpanel.m index f553c7ba7..d61795452 100755 --- a/Mixpanel/Mixpanel.m +++ b/Mixpanel/Mixpanel.m @@ -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 @@ -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); @@ -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"]; @@ -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 @@ -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(); @@ -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"]; } @@ -1913,7 +1914,7 @@ + (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", @@ -1921,10 +1922,22 @@ + (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNoti } } + // 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]; }];