From da2e75d0b16940fac4952f29e69bd3cf41130587 Mon Sep 17 00:00:00 2001 From: Marvin Liu Date: Tue, 27 Jun 2023 16:49:41 -0700 Subject: [PATCH] feat: add deep link helper --- Sources/Amplitude/Amplitude.m | 34 ++++++++++++++++++++++++---- Sources/Amplitude/Public/Amplitude.h | 12 +++++++++- 2 files changed, 41 insertions(+), 5 deletions(-) diff --git a/Sources/Amplitude/Amplitude.m b/Sources/Amplitude/Amplitude.m index 2e53f6b6..c2d1c6ae 100644 --- a/Sources/Amplitude/Amplitude.m +++ b/Sources/Amplitude/Amplitude.m @@ -116,8 +116,6 @@ @interface Amplitude () NSString *const kAMPEventPropPreviousVersion = @"[Amplitude] Previous Version"; NSString *const kAMPEventPropPreviousBuild = @"[Amplitude] Previous Build"; NSString *const kAMPEventPropFromBackground = @"[Amplitude] From Background"; -NSString *const kAMPEventPropReferringApplication = @"[Amplitude] Referring Application"; -NSString *const kAMPEventPropReferringUrl = @"[Amplitude] Referring URL"; NSString *const kAMPEventPropLinkUrl = @"[Amplitude] Link URL"; NSString *const kAMPEventPropLinkReferrer = @"[Amplitude] Link Referrer"; @@ -465,8 +463,6 @@ - (void)handleAppStateUpdates:(NSNotification *)notification { kAMPEventPropBuild: currentBuild ?: @"", kAMPEventPropVersion: currentVersion ?: @"", kAMPEventPropFromBackground: @NO, - kAMPEventPropReferringApplication: launchOptions[UIApplicationLaunchOptionsSourceApplicationKey] ?: @"", - kAMPEventPropReferringUrl: launchOptions[UIApplicationLaunchOptionsURLKey] ?: @"", }]; // persist the build/version @@ -913,6 +909,36 @@ - (void)logRevenueV2:(AMPRevenue *)revenue { [self logEvent:kAMPRevenueEvent withEventProperties:[revenue toNSDictionary]]; } +#pragma mark - Deep link methods +- (void)continueUserActivity:(NSUserActivity *)activity { + if (!self.defaultTracking.deepLinks) { + return; + } + + if ([activity.activityType isEqualToString:NSUserActivityTypeBrowsingWeb]) { + NSString *urlString = activity.webpageURL.absoluteString; + NSString *referrerString = nil; + if (@available(iOS 11, tvOS 11.0, macOS 10.13, watchOS 4.0, *)) { + referrerString = activity.referrerURL.absoluteString; + } + [self logEvent:kAMPDeepLinkOpened withEventProperties:@{ + kAMPEventPropLinkUrl: urlString ?: @"", + kAMPEventPropLinkReferrer: referrerString ?: @"", + }]; + } +} + +- (void)openURL:(NSURL *)url options:(NSDictionary *)options { + if (!self.defaultTracking.deepLinks) { + return; + } + + NSString *urlString = url.absoluteString; + [self logEvent:kAMPDeepLinkOpened withEventProperties:@{ + kAMPEventPropLinkUrl: urlString ?: @"", + }]; +} + #pragma mark - Upload events - (void)uploadEventsWithDelay:(int)delay { diff --git a/Sources/Amplitude/Public/Amplitude.h b/Sources/Amplitude/Public/Amplitude.h index e6e5b243..c20da2e4 100644 --- a/Sources/Amplitude/Public/Amplitude.h +++ b/Sources/Amplitude/Public/Amplitude.h @@ -133,7 +133,11 @@ typedef void (^AMPInitCompletionBlock)(void); @property (nonatomic, assign) BOOL trackingSessionEvents DEPRECATED_MSG_ATTRIBUTE("Use `defaultTracking.sessions` instead"); /** - Whether to automatically log start and end session events corresponding to the start and end of a user's session. + Whether to enable the default events: + - sessions tracking, replacing the previous trackingSessionEvents, including session_start, session_end, default to disabled. + - appLifecycles tracking, including Application Installed, Application Updated, Application Opened, Application Backgrounded, default to disabled. + - deepLinks tracking, including Deep Link Opened, note you will still need to call continueUserActivity or openURL method manually, default to disabled. + - screenViews tracking, including Screen Viewed, default to disabled. */ @property (nonatomic, strong) AMPDefaultTrackingOptions *defaultTracking; @@ -764,6 +768,12 @@ typedef void (^AMPInitCompletionBlock)(void); - (NSString *)getContentTypeHeader; +/** + Call to send the Deep Link Opened event, only when defaultTracking.deepLinks is enabled. + */ +- (void)continueUserActivity:(NSUserActivity *)activity; +- (void)openURL:(NSURL *)url options:(NSDictionary *)options; + @end #pragma mark - constants