Skip to content

Commit 3e16308

Browse files
committed
Add trackClickAndReturnOriginal method
Developers call the `OneSignal.LiveActivities.trackClickAndReturnOriginal(url)` method to provide OneSignal the click tracking metadata. This method returns the intended original URL, with which they use to navigate their users.
1 parent ebf6ce9 commit 3e16308

File tree

3 files changed

+66
-4
lines changed

3 files changed

+66
-4
lines changed

iOS_SDK/OneSignalSDK/OneSignalLiveActivities/Source/OneSignalLiveActivitiesManagerImpl.swift

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,54 @@ public class OneSignalLiveActivitiesManagerImpl: NSObject, OSLiveActivities {
175175
}
176176
}
177177

178+
@objc
179+
public static func trackClickAndReturnOriginal(_ url: URL) -> URL? {
180+
// Check if this is a OneSignal click tracking URL
181+
guard url.scheme == LiveActivityConstants.Tracking.scheme,
182+
url.host == LiveActivityConstants.Tracking.host,
183+
url.path == LiveActivityConstants.Tracking.clickPath,
184+
let components = URLComponents(url: url, resolvingAgainstBaseURL: false),
185+
let queryItems = components.queryItems else
186+
{
187+
OneSignalLog.onesignalLog(.LL_VERBOSE, message: "trackClickAndReturnOriginal:\(url) is not a tracking URL")
188+
return url
189+
}
190+
191+
/// Helper function to extract redirect URL
192+
func getRedirectURL() -> URL? {
193+
guard let redirectString = queryItems.first(where: { $0.name == LiveActivityConstants.Tracking.redirect })?.value,
194+
let redirectURL = URL(string: redirectString)
195+
else {
196+
return nil
197+
}
198+
return redirectURL
199+
}
200+
201+
// Extract metadata
202+
guard let clickId = queryItems.first(where: { $0.name == LiveActivityConstants.Tracking.clickId })?.value,
203+
let activityId = queryItems.first(where: { $0.name == LiveActivityConstants.Tracking.activityId })?.value,
204+
let activityType = queryItems.first(where: { $0.name == LiveActivityConstants.Tracking.activityType })?.value else
205+
{
206+
OneSignalLog.onesignalLog(.LL_ERROR, message: "Missing required parameters in tracking URL: \(url)")
207+
return getRedirectURL()
208+
}
209+
210+
trackClick(clickId: clickId, activityType: activityType, activityId: activityId)
211+
212+
return getRedirectURL()
213+
}
214+
215+
/**
216+
Track the click event.
217+
- Parameters:
218+
- clickId: UUID representing the unique click event, as it is possible for this click to be tracked multiple times.
219+
*/
220+
private static func trackClick(clickId: String, activityType: String, activityId: String) {
221+
OneSignalLog.onesignalLog(.LL_VERBOSE, message: "OneSignal.LiveActivities trackClick called with clickId: \(clickId), activityType: \(activityType), activityId: \(activityId)")
222+
let req = OSRequestLiveActivityClicked(key: clickId, activityType: activityType, activityId: activityId)
223+
_executor.append(req)
224+
}
225+
178226
@available(iOS 17.2, *)
179227
private static func listenForPushToStart<Attributes: OneSignalLiveActivityAttributes>(_ activityType: Attributes.Type, options: LiveActivitySetupOptions? = nil) {
180228
if options == nil || options!.enablePushToStart {

iOS_SDK/OneSignalSDK/OneSignalOSCore/Source/OSLiveActivities.swift

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import OneSignalCore
3434
public protocol OSLiveActivities {
3535
/**
3636
Indicate this device has entered a live activity, identified within OneSignal by the `activityId`.
37-
- Parameters
37+
- Parameters:
3838
- activityId: The activity identifier the live activity on this device will receive updates for.
3939
- withToken: The live activity's update token to receive the updates.
4040
*/
@@ -43,7 +43,7 @@ public protocol OSLiveActivities {
4343
/**
4444
Indicate this device has entered a live activity, identified within OneSignal by the `activityId`. This method is deprecated since
4545
the request to enter a live activity will always succeed.
46-
- Parameters
46+
- Parameters:
4747
- activityId: The activity identifier the live activity on this device will receive updates for.
4848
- withToken: The live activity's update token to receive the updates.
4949
- withSuccess: A success callback that will be called when the live activity enter request has been queued.
@@ -54,19 +54,29 @@ public protocol OSLiveActivities {
5454

5555
/**
5656
Indicate this device has exited a live activity, identified within OneSignal by the `activityId`.
57-
- Parameters
57+
- Parameters:
5858
- activityId: The activity identifier the live activity on this device will no longer receive updates for.
5959
*/
6060
static func exit(_ activityId: String)
6161

6262
/**
6363
Indicate this device has exited a live activity, identified within OneSignal by the `activityId`. This method is deprecated since
6464
the request to enter a live activity will always succeed.
65-
- Parameters
65+
- Parameters:
6666
- activityId: The activity identifier the live activity on this device will no longer receive updates for.
6767
- withSuccess: A success callback that will be called when the live activity exit request has been queued.
6868
- withFailure: A failure callback that will be called when the live activity enter exit was not successfully queued.
6969
*/
7070
@available(*, deprecated)
7171
static func exit(_ activityId: String, withSuccess: OSResultSuccessBlock?, withFailure: OSFailureBlock?)
72+
73+
/**
74+
Use in conjunction with the `onesignalWidgetURL` modifier. Handle a URL opened in the app to track Live Activity clicks. Call this method from your app's URL handling code
75+
(e.g., `application(_:open:options:)` in AppDelegate or `onOpenURL` in SwiftUI).
76+
77+
- Parameters:
78+
- url: The URL that was opened, which may be a OneSignal Live Activity click tracking URL.
79+
- Returns: The intended original nullable URL.
80+
*/
81+
static func trackClickAndReturnOriginal(_ url: URL) -> URL?
7282
}

iOS_SDK/OneSignalSDK/OneSignalOSCore/Source/OSStubLiveActivities.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,8 @@ public class OSStubLiveActivities: NSObject, OSLiveActivities {
3131
OneSignalLog.onesignalLog(.LL_ERROR, message: "OneSignalLiveActivities not found. In order to use OneSignal's LiveActivities features the OneSignalLiveActivities module must be added.")
3232
}
3333

34+
public static func trackClickAndReturnOriginal(_ url: URL) -> URL? {
35+
OneSignalLog.onesignalLog(.LL_ERROR, message: "OneSignalLiveActivities not found. In order to use OneSignal's LiveActivities features the OneSignalLiveActivities module must be added.")
36+
return url
37+
}
3438
}

0 commit comments

Comments
 (0)