Skip to content

Commit

Permalink
Move decorateLink to TrackerControllerImpl
Browse files Browse the repository at this point in the history
  • Loading branch information
greg-el committed Sep 26, 2023
1 parent f1f1227 commit 1a5ec3a
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 55 deletions.
52 changes: 0 additions & 52 deletions Sources/Core/Tracker/Tracker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -386,58 +386,6 @@ class Tracker: NSObject {
session?.startChecker()
}

private func decorateLinkErrorTemplate(_ extendedParameterName: String) -> String {
"\(extendedParameterName) has been requested in CrossDeviceParameterConfiguration, but it is not set."
}

func decorateLink(_ url: URL, extendedParameters: CrossDeviceParameterConfiguration? = nil) -> URL? {
var userId: String
switch self.session?.userId {
case .none:
logError(message: "\(url) could not be decorated as session.userId is nil")
return nil
case .some(let id):
userId = id
}

let extendedParameters = extendedParameters ?? CrossDeviceParameterConfiguration()

let sessionId = extendedParameters.sessionId ? self.session?.state?.sessionId ?? "" : ""
if (extendedParameters.sessionId && sessionId.isEmpty) {
logDebug(message: "\(decorateLinkErrorTemplate("sessionId")) Ensure an event has been tracked to generate a session before calling this method.")
}

let sourceId = extendedParameters.sourceId ? self.appId : ""

let sourcePlatform = extendedParameters.sourcePlatform ? devicePlatformToString(self.devicePlatform) : ""

let subjectUserId = extendedParameters.subjectUserId ? self.subject?.userId ?? "" : ""
if (extendedParameters.subjectUserId && subjectUserId.isEmpty) {
logDebug(message: "\(decorateLinkErrorTemplate("subjectUserId")) Ensure SubjectConfiguration.userId has been set on your tracker.")
}

let reason = extendedParameters.reason ?? ""

let spParameters = [
userId,
String(Int(Date().timeIntervalSince1970 * 1000)),
sessionId,
subjectUserId.toBase64(),
sourceId.toBase64(),
sourcePlatform,
reason.toBase64()
].joined(separator: ".").trimmingCharacters(in: ["."])

var components = URLComponents(url: url, resolvingAgainstBaseURL: false)
let spQueryParam = URLQueryItem(name: "_sp", value: spParameters)

// Modification requires exclusive access, we must make a copy
var queryItems = components?.queryItems
components?.queryItems = (queryItems?.filter { $0.name != "_sp" } ?? []) + [spQueryParam]

return components?.url
}

// MARK: - Notifications management

@objc func receiveScreenViewNotification(_ notification: Notification) {
Expand Down
51 changes: 49 additions & 2 deletions Sources/Core/Tracker/TrackerControllerImpl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,53 @@ class TrackerControllerImpl: Controller, TrackerController {
}

func decorateLink(_ url: URL) -> URL? {
return tracker.decorateLink(url)
self.decorateLink(url, extendedParameters: CrossDeviceParameterConfiguration())
}

func decorateLink(_ url: URL, extendedParameters: CrossDeviceParameterConfiguration) -> URL? {
return tracker.decorateLink(url, extendedParameters: extendedParameters)
var userId: String
switch self.session?.userId {
case .none:
logError(message: "\(url) could not be decorated as session.userId is nil")
return nil
case .some(let id):
userId = id
}

let sessionId = extendedParameters.sessionId ? self.session?.sessionId ?? "" : ""
if (extendedParameters.sessionId && sessionId.isEmpty) {
logDebug(message: "\(decorateLinkErrorTemplate("sessionId")) Ensure an event has been tracked to generate a session before calling this method.")
}

let sourceId = extendedParameters.sourceId ? self.appId : ""

let sourcePlatform = extendedParameters.sourcePlatform ? devicePlatformToString(self.devicePlatform) : ""

let subjectUserId = extendedParameters.subjectUserId ? self.subject?.userId ?? "" : ""
if (extendedParameters.subjectUserId && subjectUserId.isEmpty) {
logDebug(message: "\(decorateLinkErrorTemplate("subjectUserId")) Ensure SubjectConfiguration.userId has been set on your tracker.")
}

let reason = extendedParameters.reason ?? ""

let spParameters = [
userId,
String(Int(Date().timeIntervalSince1970 * 1000)),
sessionId,
subjectUserId.toBase64(),
sourceId.toBase64(),
sourcePlatform,
reason.toBase64()
].joined(separator: ".").trimmingCharacters(in: ["."])

var components = URLComponents(url: url, resolvingAgainstBaseURL: false)
let spQueryParam = URLQueryItem(name: "_sp", value: spParameters)

// Modification requires exclusive access, we must make a copy
let queryItems = components?.queryItems
components?.queryItems = (queryItems?.filter { $0.name != "_sp" } ?? []) + [spQueryParam]

return components?.url
}

// MARK: - Properties' setters and getters
Expand Down Expand Up @@ -296,6 +338,7 @@ class TrackerControllerImpl: Controller, TrackerController {
return kSPVersion
}


// MARK: - Private methods

private var tracker: Tracker {
Expand All @@ -309,4 +352,8 @@ class TrackerControllerImpl: Controller, TrackerController {
private var dirtyConfig: TrackerConfiguration {
return serviceProvider.trackerConfiguration
}

private func decorateLinkErrorTemplate(_ extendedParameterName: String) -> String {
"\(extendedParameterName) has been requested in CrossDeviceParameterConfiguration, but it is not set."
}
}
2 changes: 1 addition & 1 deletion Tests/TestLinkDecorator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ class TestLinkDecorator: XCTestCase {

let result = tracker.decorateLink(link, extendedParameters: CrossDeviceParameterConfiguration(sessionId: false, sourceId: false))!

matches(for: "https://example.com?a=a&b=b&_sp=\(tracker.session!.userId!).\(epoch)&b=b", in: result.absoluteString)
matches(for: "https://example.com?a=a&b=b&_sp=\(tracker.session!.userId!).\(epoch)", in: result.absoluteString)
}

func testMissingFields() {
Expand Down

0 comments on commit 1a5ec3a

Please sign in to comment.