Skip to content

Commit

Permalink
Added sessionID to BTShopperInsightClient. Added unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
stechiu committed Dec 4, 2024
1 parent 8941506 commit c40807f
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 9 deletions.
6 changes: 5 additions & 1 deletion Sources/BraintreeCore/Analytics/FPTIBatchData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,10 @@ struct FPTIBatchData: Codable {
/// Used for linking events from the client to server side request
/// This value will be PayPal Order ID, Payment Token, EC token, Billing Agreement, or Venmo Context ID depending on the flow
let payPalContextID: String?

/// UTC millisecond timestamp when a networking task started requesting a resource. See [Apple's docs](https://developer.apple.com/documentation/foundation/urlsessiontasktransactionmetrics#3162615).
let requestStartTime: Int?
/// This value should be the shopper session ID returned from your server SDK request
let sessionID: String?
/// UTC millisecond timestamp when a networking task initiated.
let startTime: Int?
let timestamp = String(Date().utcTimestampMilliseconds)
Expand All @@ -76,6 +77,7 @@ struct FPTIBatchData: Codable {
paymentMethodsDisplayed: String? = nil,
payPalContextID: String? = nil,
requestStartTime: Int? = nil,
sessionID: String? = nil,
startTime: Int? = nil
) {
self.appSwitchURL = appSwitchURL?.absoluteString
Expand All @@ -92,6 +94,7 @@ struct FPTIBatchData: Codable {
self.paymentMethodsDisplayed = paymentMethodsDisplayed
self.payPalContextID = payPalContextID
self.requestStartTime = requestStartTime
self.sessionID = sessionID
self.startTime = startTime
}

Expand All @@ -110,6 +113,7 @@ struct FPTIBatchData: Codable {
case requestStartTime = "request_start_time"
case timestamp = "t"
case tenantName = "tenant_name"
case sessionID = "session_id"
case startTime = "start_time"
case endTime = "end_time"
case endpoint = "endpoint"
Expand Down
6 changes: 4 additions & 2 deletions Sources/BraintreeCore/BTAPIClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,8 @@ import Foundation
linkType: LinkType? = nil,
paymentMethodsDisplayed: String? = nil,
payPalContextID: String? = nil,
appSwitchURL: URL? = nil
appSwitchURL: URL? = nil,
sessionID: String? = nil
) {
analyticsService.sendAnalyticsEvent(
FPTIBatchData.Event(
Expand All @@ -324,7 +325,8 @@ import Foundation
linkType: linkType?.rawValue,
merchantExperiment: merchantExperiment,
paymentMethodsDisplayed: paymentMethodsDisplayed,
payPalContextID: payPalContextID
payPalContextID: payPalContextID,
sessionID: sessionID
)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ public class BTShopperInsightsClient {
// MARK: - Private Properties

private let apiClient: BTAPIClient

/// This value should be the shopper session ID returned from your server SDK request
private let shopperSessionID: String? = ""

/// Creates a `BTShopperInsightsClient`
/// - Parameter apiClient: A `BTAPIClient` instance.
Expand Down Expand Up @@ -127,7 +130,8 @@ public class BTShopperInsightsClient {
private func notifySuccess(with result: BTShopperInsightsResult, for experiment: String?) -> BTShopperInsightsResult {
apiClient.sendAnalyticsEvent(
BTShopperInsightsAnalytics.recommendedPaymentsSucceeded,
merchantExperiment: experiment
merchantExperiment: experiment,
sessionID: shopperSessionID
)
return result
}
Expand All @@ -136,7 +140,8 @@ public class BTShopperInsightsClient {
apiClient.sendAnalyticsEvent(
BTShopperInsightsAnalytics.recommendedPaymentsFailed,
errorDescription: error.localizedDescription,
merchantExperiment: experiment
merchantExperiment: experiment,
sessionID: shopperSessionID
)
return error
}
Expand Down
12 changes: 8 additions & 4 deletions UnitTests/BraintreeCoreTests/Analytics/FPTIBatchData_Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ final class FPTIBatchData_Tests: XCTestCase {
linkType: LinkType.universal.rawValue,
payPalContextID: "fake-order-id",
requestStartTime: 456,
sessionID: "123456",
startTime: 999888777666
),
FPTIBatchData.Event(
Expand All @@ -42,29 +43,30 @@ final class FPTIBatchData_Tests: XCTestCase {
linkType: nil,
payPalContextID: "fake-order-id-2",
requestStartTime: nil,
sessionID: nil,
startTime: nil
)
]

override func setUp() {
super.setUp()

sut = FPTIBatchData(metadata: batchMetadata, events: eventParams)
}

func testInit_formatsJSONBody() throws {
let jsonBody = try sut.toDictionary()

guard let events = jsonBody["events"] as? [[String: Any]] else {
XCTFail("JSON body missing top level `events` key.")
return
}

guard let eventParams = events[0]["event_params"] as? [[String: Any]] else {
XCTFail("JSON body missing `event_params` key.")
return
}

guard let batchParams = events[0]["batch_params"] as? [String: Any] else {
XCTFail("JSON body missing `batch_params` key.")
return
Expand Down Expand Up @@ -121,6 +123,8 @@ final class FPTIBatchData_Tests: XCTestCase {
XCTAssertNil(eventParams[1]["connect_start_time"])
XCTAssertEqual(eventParams[0]["request_start_time"] as? Int, 456)
XCTAssertNil(eventParams[1]["request_start_time"])
XCTAssertEqual(eventParams[0]["session_id"] as? String, "123456")
XCTAssertNil(eventParams[1]["session_id"])
}
}

Expand Down

0 comments on commit c40807f

Please sign in to comment.