Skip to content

Commit

Permalink
feat: add deep link helper
Browse files Browse the repository at this point in the history
  • Loading branch information
liuyang1520 committed Jun 27, 2023
1 parent 60025b0 commit da2e75d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
34 changes: 30 additions & 4 deletions Sources/Amplitude/Amplitude.m
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -465,8 +463,6 @@ - (void)handleAppStateUpdates:(NSNotification *)notification {
kAMPEventPropBuild: currentBuild ?: @"",
kAMPEventPropVersion: currentVersion ?: @"",
kAMPEventPropFromBackground: @NO,
kAMPEventPropReferringApplication: launchOptions[UIApplicationLaunchOptionsSourceApplicationKey] ?: @"",
kAMPEventPropReferringUrl: launchOptions[UIApplicationLaunchOptionsURLKey] ?: @"",
}];

// persist the build/version
Expand Down Expand Up @@ -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 {
Expand Down
12 changes: 11 additions & 1 deletion Sources/Amplitude/Public/Amplitude.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit da2e75d

Please sign in to comment.