Created
December 6, 2021 23:29
-
-
Save cameronehrlich/d5077ffa6f514699ab55186a7931a04e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// BranchLinkRouter.swift | |
// StartEngine | |
// | |
// Created by Spencer Prescott on 8/19/21. | |
// | |
import SEUtility | |
import SEViewModel | |
import Branch | |
protocol BranchLinkRouterDelegate: AnyObject { | |
func branchLinkRouter(_ router: BranchLinkRouter, showDeeplinkLoadingViewController viewController: DeeplinkLoadingViewController) | |
} | |
final class BranchLinkRouter { | |
weak var delegate: BranchLinkRouterDelegate? | |
/// Setup Branch SDK and GA linking to Branch. Must be called after analytics are setup in the app | |
func setup(launchOptions: [AnyHashable: Any]?) { | |
Branch.setUseTestBranchKey(!Environment.isBuiltForProduction) | |
Branch.getInstance().initSession(launchOptions: launchOptions) { [weak self] params, error in | |
guard let self = self else { return } | |
Branch.getInstance().lastAttributedTouchData(withAttributionWindow: 0) { data, error in | |
// TODO: Handle LATD. Currently data is always nil | |
} | |
if let error = error { | |
ErrorTracker.track(error: error) | |
} else if let params = params { | |
if let didClickBranchLink = params["+clicked_branch_link"] as? Int, didClickBranchLink != 0 { | |
var userProperties: [String: String] = [:] | |
var tapEvent = AnalyticEvent(.tappedBranchLink) | |
if let campaign = params["~campaign"] as? String { | |
userProperties["utm_campaign"] = campaign | |
} | |
if let medium = params["~feature"] as? String { | |
userProperties["utm_medium"] = medium | |
} | |
if let source = params["~channel"] as? String { | |
userProperties["utm_source"] = source | |
} | |
if let referringLink = params["~referring_link"] as? String { | |
userProperties["referring_link"] = referringLink | |
} | |
userProperties.forEach { key, value in | |
tapEvent = tapEvent.with(key: key, value: value) | |
} | |
tapEvent.track() | |
AnalyticsManager.shared.set(userProperties: userProperties) | |
} | |
if let deeplink = URL(string: params["$ios_deeplink_path"] as? String) { | |
let vm = DeeplinkLoadingVM(url: deeplink, user: UserManager.shared) | |
self.delegate?.branchLinkRouter(self, showDeeplinkLoadingViewController: DeeplinkLoadingViewController(viewModel: vm)) | |
} | |
} | |
} | |
if !Environment.isBuiltForProduction { | |
Branch.getInstance().enableLogging() | |
} | |
} | |
func shouldHandleLink(_ url: URL) -> Bool { | |
return url.absoluteString.contains("link_click_id") == true | |
} | |
func shouldHandleActivity(_ userActivity: NSUserActivity) -> Bool { | |
return userActivity.webpageURL?.absoluteString.contains("app.link") == true | |
} | |
func handleLinkTap(app: UIApplication, url: URL, options: [UIApplication.OpenURLOptionsKey: Any]) -> Bool { | |
return Branch.getInstance().application(app, open: url, options: options) | |
} | |
func `continue`(_ userActivity: NSUserActivity) -> Bool { | |
return Branch.getInstance().continue(userActivity) | |
} | |
func handlePushNotification(_ userInfo: [AnyHashable: Any]) { | |
Branch.getInstance().handlePushNotification(userInfo) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment