Skip to content

Commit

Permalink
Fix duplicate event tracking and incorrect foreground event logging f…
Browse files Browse the repository at this point in the history
…or push notifications (#22956)
  • Loading branch information
salimbraksa authored Apr 5, 2024
2 parents e5127db + 4017c7e commit 0906842
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 15 deletions.
8 changes: 5 additions & 3 deletions WordPress/Classes/System/WordPressAppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -385,9 +385,11 @@ extension WordPressAppDelegate {

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
DDLogInfo("\(self) \(#function)")

PushNotificationsManager.shared.handleNotification(userInfo as NSDictionary,
completionHandler: completionHandler)
PushNotificationsManager.shared.application(
application,
didReceiveRemoteNotification: userInfo,
fetchCompletionHandler: completionHandler
)
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,7 @@ extension InteractiveNotificationsManager: UNUserNotificationCenterDelegate {
let textInputResponse = response as? UNTextInputNotificationResponse

// Analytics
PushNotificationsManager.shared.trackNotification(with: userInfo)
PushNotificationsManager.shared.trackNotification(with: userInfo, response: response)

if handleAction(with: response.actionIdentifier,
category: response.notification.request.content.categoryIdentifier,
Expand Down
28 changes: 23 additions & 5 deletions WordPress/Classes/Utility/PushNotificationsManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,6 @@ final public class PushNotificationsManager: NSObject {
return
}

// Analytics
trackNotification(with: userInfo)

// Handling!
let handlers = [handleSupportNotification,
handleAuthenticationNotification,
Expand All @@ -207,7 +204,18 @@ final public class PushNotificationsManager: NSObject {
///
/// - Parameter userInfo: The Notification's Payload
///
func trackNotification(with userInfo: NSDictionary) {
func trackNotification(with userInfo: NSDictionary, response: UNNotificationResponse? = nil) {
let event: WPAnalyticsStat? = {
if let response {
return response.actionIdentifier == UNNotificationDefaultActionIdentifier ? .pushNotificationAlertPressed : nil
}
return .pushNotificationReceived
}()

guard let event else {
return
}

var properties = [String: String]()

if let noteId = userInfo.number(forKey: Notification.identifierKey) {
Expand All @@ -222,7 +230,6 @@ final public class PushNotificationsManager: NSObject {
properties[Tracking.tokenKey] = theToken
}

let event: WPAnalyticsStat = (applicationState == .background) ? .pushNotificationReceived : .pushNotificationAlertPressed
WPAnalytics.track(event, withProperties: properties)
}

Expand Down Expand Up @@ -391,6 +398,17 @@ extension PushNotificationsManager {
}
}

// MARK: - Application Delegate

extension PushNotificationsManager: UIApplicationDelegate {

public func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
let dictionary = userInfo as NSDictionary
self.trackNotification(with: dictionary)
self.handleNotification(dictionary, completionHandler: completionHandler)
}
}

// MARK: - Nested Types
//
extension PushNotificationsManager {
Expand Down
14 changes: 8 additions & 6 deletions WordPress/WordPress.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -10979,7 +10979,7 @@
path = Classes;
sourceTree = "<group>";
};
29B97314FDCFA39411CA2CEA = {
29B97314FDCFA39411CA2CEA /* CustomTemplate */ = {
isa = PBXGroup;
children = (
3F20FDF3276BF21000DA3CAD /* Packages */,
Expand Down Expand Up @@ -19403,7 +19403,7 @@
bg,
sk,
);
mainGroup = 29B97314FDCFA39411CA2CEA;
mainGroup = 29B97314FDCFA39411CA2CEA /* CustomTemplate */;
packageReferences = (
3FF1442E266F3C2400138163 /* XCRemoteSwiftPackageReference "ScreenObject" */,
3FC2C33B26C4CF0A00C6D98F /* XCRemoteSwiftPackageReference "XCUITestHelpers" */,
Expand Down Expand Up @@ -21004,11 +21004,11 @@
files = (
);
inputPaths = (
"$SRCROOT/../Scripts/BuildPhases/CopyGutenbergJS.inputs.xcfilelist",
$SRCROOT/../Scripts/BuildPhases/CopyGutenbergJS.inputs.xcfilelist,
);
name = "Copy Gutenberg JS";
outputFileListPaths = (
"$SRCROOT/../Scripts/BuildPhases/CopyGutenbergJS.outputs.xcfilelist",
$SRCROOT/../Scripts/BuildPhases/CopyGutenbergJS.outputs.xcfilelist,
);
outputPaths = (
"",
Expand Down Expand Up @@ -21203,13 +21203,13 @@
files = (
);
inputFileListPaths = (
"$SRCROOT/../Scripts/BuildPhases/CopyGutenbergJS.inputs.xcfilelist",
$SRCROOT/../Scripts/BuildPhases/CopyGutenbergJS.inputs.xcfilelist,
);
inputPaths = (
);
name = "Copy Gutenberg JS";
outputFileListPaths = (
"$SRCROOT/../Scripts/BuildPhases/CopyGutenbergJS.outputs.xcfilelist",
$SRCROOT/../Scripts/BuildPhases/CopyGutenbergJS.outputs.xcfilelist,
);
outputPaths = (
);
Expand Down Expand Up @@ -29754,6 +29754,7 @@
CURRENT_PROJECT_VERSION = "${VERSION_LONG}";
DEFINES_MODULE = YES;
DEVELOPMENT_TEAM = PZYM8XX95Q;
"DEVELOPMENT_TEAM[sdk=iphoneos*]" = PZYM8XX95Q;
ENABLE_BITCODE = NO;
FRAMEWORK_SEARCH_PATHS = "$(inherited)";
GCC_OPTIMIZATION_LEVEL = 0;
Expand Down Expand Up @@ -29799,6 +29800,7 @@
PRODUCT_MODULE_NAME = WordPress;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "Jetpack iOS Development";
"PROVISIONING_PROFILE_SPECIFIER[sdk=iphoneos*]" = "Jetpack iOS Development";
SWIFT_OBJC_BRIDGING_HEADER = "Classes/System/WordPress-Bridging-Header.h";
SWIFT_OBJC_INTERFACE_HEADER_NAME = "$(SWIFT_MODULE_NAME)-Swift-XcodeGenerated.h";
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
Expand Down

0 comments on commit 0906842

Please sign in to comment.