Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shopper Insights - Update sendSelected Events #1486

Merged
4 changes: 4 additions & 0 deletions Braintree.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
04B001122D0D02CF00C0060D /* BTButtonType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 04B001112D0D02CB00C0060D /* BTButtonType.swift */; };
0917F6E42A27BDC700ACED2E /* BTVenmoLineItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 096C6B2529CCDCEB00912863 /* BTVenmoLineItem.swift */; };
09357DCB2A2FBEC10096D449 /* BTVenmoLineItem_Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 09357DCA2A2FBEC10096D449 /* BTVenmoLineItem_Tests.swift */; };
1FEB89E614CB6BF0B9858EE4 /* Pods_Tests_IntegrationTests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 85BD589D380436A0C9D1DEC1 /* Pods_Tests_IntegrationTests.framework */; };
Expand Down Expand Up @@ -729,6 +730,7 @@
035A59D91EA5DE97002960C8 /* BTLocalPaymentClient_UnitTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BTLocalPaymentClient_UnitTests.swift; sourceTree = "<group>"; };
039A8BD91F9E993500D607E7 /* BTAmericanExpressRewardsBalance_Tests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BTAmericanExpressRewardsBalance_Tests.swift; sourceTree = "<group>"; };
03F921C1200EBB200076CD80 /* BTThreeDSecurePostalAddress_Tests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BTThreeDSecurePostalAddress_Tests.swift; sourceTree = "<group>"; };
04B001112D0D02CB00C0060D /* BTButtonType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BTButtonType.swift; sourceTree = "<group>"; };
09357DCA2A2FBEC10096D449 /* BTVenmoLineItem_Tests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BTVenmoLineItem_Tests.swift; sourceTree = "<group>"; };
096C6B2529CCDCEB00912863 /* BTVenmoLineItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BTVenmoLineItem.swift; sourceTree = "<group>"; };
162174E1192D9220008DC35D /* CoreGraphics.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreGraphics.framework; path = System/Library/Frameworks/CoreGraphics.framework; sourceTree = SDKROOT; };
Expand Down Expand Up @@ -1494,6 +1496,7 @@
804698292B27C4D70090878E /* BraintreeShopperInsights */ = {
isa = PBXGroup;
children = (
04B001112D0D02CB00C0060D /* BTButtonType.swift */,
62EA90482B63071800DD79BC /* BTEligiblePaymentMethods.swift */,
800ED7822B4F5B66007D8A30 /* BTEligiblePaymentsRequest.swift */,
8037BFAF2B2CCC130017072C /* BTShopperInsightsAnalytics.swift */,
Expand Down Expand Up @@ -3365,6 +3368,7 @@
buildActionMask = 2147483647;
files = (
804698382B27C53B0090878E /* BTShopperInsightsRequest.swift in Sources */,
04B001122D0D02CF00C0060D /* BTButtonType.swift in Sources */,
624B27F72B6AE0C2000AC08A /* BTShopperInsightsError.swift in Sources */,
804698372B27C5390090878E /* BTShopperInsightsClient.swift in Sources */,
800ED7832B4F5B66007D8A30 /* BTEligiblePaymentsRequest.swift in Sources */,
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* BraintreeShopperInsights (BETA)
* Add `shopperSessionID` to `BTShopperInsightsClient` initializer
* Add `isPayPalAppInstalled()` and/or `isVenmoAppInstalled()`
* Replace `sendPayPalSelectedEvent()` and `sendPayPalSelectedEvent()` with `sendSelectedEvent(for:)`
* Send `url` in `event_params` for App Switch events to PayPal's analytics service (FPTI)
* BraintreeVenmo
* Send `url` in `event_params` for App Switch events to PayPal's analytics service (FPTI)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ class ShopperInsightsViewController: PaymentButtonBaseViewController {
let paymentMethods = ["Apple Pay", "Card", "PayPal"]
shopperInsightsClient.sendPayPalPresentedEvent(paymentMethodsDisplayed: paymentMethods, experiment: sampleExperiment)
progressBlock("Tapped PayPal Vault")
shopperInsightsClient.sendPayPalSelectedEvent()
shopperInsightsClient.sendSelectedEvent(for: .payPal)

button.setTitle("Processing...", for: .disabled)
button.isEnabled = false
Expand All @@ -133,7 +133,7 @@ class ShopperInsightsViewController: PaymentButtonBaseViewController {
@objc func venmoButtonTapped(_ button: UIButton) {
shopperInsightsClient.sendVenmoPresentedEvent()
progressBlock("Tapped Venmo")
shopperInsightsClient.sendVenmoSelectedEvent()
shopperInsightsClient.sendSelectedEvent(for: .venmo)

button.setTitle("Processing...", for: .disabled)
button.isEnabled = false
Expand Down
5 changes: 5 additions & 0 deletions Sources/BraintreeCore/Analytics/FPTIBatchData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ struct FPTIBatchData: Codable {
struct Event: Codable {

let appSwitchURL: String?
/// The button type to be displayed or presented
let buttonType: String?
/// UTC millisecond timestamp when a networking task started establishing a TCP connection. See [Apple's docs](https://developer.apple.com/documentation/foundation/urlsessiontasktransactionmetrics#3162615).
/// `nil` if a persistent connection is used.
let connectionStartTime: Int?
Expand Down Expand Up @@ -65,6 +67,7 @@ struct FPTIBatchData: Codable {

init(
appSwitchURL: URL? = nil,
buttonType: String? = nil,
connectionStartTime: Int? = nil,
correlationID: String? = nil,
endpoint: String? = nil,
Expand All @@ -82,6 +85,7 @@ struct FPTIBatchData: Codable {
startTime: Int? = nil
) {
self.appSwitchURL = appSwitchURL?.absoluteString
self.buttonType = buttonType
self.connectionStartTime = connectionStartTime
self.correlationID = correlationID
self.endpoint = endpoint
Expand All @@ -101,6 +105,7 @@ struct FPTIBatchData: Codable {

enum CodingKeys: String, CodingKey {
case appSwitchURL = "url"
case buttonType = "button_type"
case connectionStartTime = "connect_start_time"
case correlationID = "correlation_id"
case errorDescription = "error_desc"
Expand Down
2 changes: 2 additions & 0 deletions Sources/BraintreeCore/BTAPIClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ import Foundation
@_documentation(visibility: private)
public func sendAnalyticsEvent(
_ eventName: String,
buttonType: String? = nil,
correlationID: String? = nil,
errorDescription: String? = nil,
merchantExperiment: String? = nil,
Expand All @@ -317,6 +318,7 @@ import Foundation
analyticsService.sendAnalyticsEvent(
FPTIBatchData.Event(
appSwitchURL: appSwitchURL,
buttonType: buttonType,
correlationID: correlationID,
errorDescription: errorDescription,
eventName: eventName,
Expand Down
9 changes: 9 additions & 0 deletions Sources/BraintreeShopperInsights/BTButtonType.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import Foundation

/// The button type to be displayed or presented
/// - Warning: This module is in beta. It's public API may change or be removed in future releases.
public enum BTButtonType: String {
case payPal = "PayPal"
case venmo = "Venmo"
case other = "Other"
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ enum BTShopperInsightsAnalytics {
// MARK: - Merchant Triggered Events

static let payPalPresented = "shopper-insights:paypal-presented"
static let payPalSelected = "shopper-insights:paypal-selected"
static let venmoPresented = "shopper-insights:venmo-presented"
static let venmoSelected = "shopper-insights:venmo-selected"


static let buttonSelected = "shopper-insights:button-selected"

// MARK: - SDK Triggered Events

static let recommendedPaymentsStarted = "shopper-insights:get-recommended-payments:started"
Expand Down
18 changes: 6 additions & 12 deletions Sources/BraintreeShopperInsights/BTShopperInsightsClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,7 @@ public class BTShopperInsightsClient {
shopperSessionID: shopperSessionID
)
}

/// Call this method when the PayPal button has been selected/tapped by the buyer.
/// This method sends analytics to help improve the Shopper Insights feature experience
public func sendPayPalSelectedEvent() {
apiClient.sendAnalyticsEvent(BTShopperInsightsAnalytics.payPalSelected, shopperSessionID: shopperSessionID)
}


/// Call this method when the Venmo button has been successfully displayed to the buyer.
/// This method sends analytics to help improve the Shopper Insights feature experience.
/// - Parameters:
Expand All @@ -124,11 +118,11 @@ public class BTShopperInsightsClient {
shopperSessionID: shopperSessionID
)
}
/// Call this method when the Venmo button has been selected/tapped by the buyer.
/// This method sends analytics to help improve the Shopper Insights feature experience
public func sendVenmoSelectedEvent() {
apiClient.sendAnalyticsEvent(BTShopperInsightsAnalytics.venmoSelected, shopperSessionID: shopperSessionID)

/// Call this method when a button has been selected/tapped by the buyer.
/// This method sends analytics to help improve the Shopper Insights feature experience.
public func sendSelectedEvent(for buttonType: BTButtonType) {
apiClient.sendAnalyticsEvent(BTShopperInsightsAnalytics.buttonSelected, shopperSessionID: shopperSessionID)
}

/// Indicates whether the PayPal App is installed.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ final class BTShopperInsightsAnalytics_Tests: XCTestCase {

func test_recommendedPaymentAnalyticEvents_sendExpectedEventNames() {
XCTAssertEqual(BTShopperInsightsAnalytics.payPalPresented, "shopper-insights:paypal-presented")
XCTAssertEqual(BTShopperInsightsAnalytics.payPalSelected, "shopper-insights:paypal-selected")
XCTAssertEqual(BTShopperInsightsAnalytics.buttonSelected, "shopper-insights:button-selected")
XCTAssertEqual(BTShopperInsightsAnalytics.venmoPresented, "shopper-insights:venmo-presented")
XCTAssertEqual(BTShopperInsightsAnalytics.venmoSelected, "shopper-insights:venmo-selected")
XCTAssertEqual(BTShopperInsightsAnalytics.recommendedPaymentsStarted, "shopper-insights:get-recommended-payments:started")
XCTAssertEqual(BTShopperInsightsAnalytics.recommendedPaymentsSucceeded, "shopper-insights:get-recommended-payments:succeeded")
XCTAssertEqual(BTShopperInsightsAnalytics.recommendedPaymentsFailed, "shopper-insights:get-recommended-payments:failed")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ class BTShopperInsightsClient_Tests: XCTestCase {
}

func testGetRecommendedPaymentMethods_withTokenizationKey_returnsError() async {
var apiClient = BTAPIClient(authorization: "sandbox_merchant_1234567890abc")!
let apiClient = BTAPIClient(authorization: "sandbox_merchant_1234567890abc")!
let shopperInsightsClient = BTShopperInsightsClient(apiClient: apiClient)

do {
Expand Down Expand Up @@ -221,8 +221,8 @@ class BTShopperInsightsClient_Tests: XCTestCase {
}

func testSendPayPalSelectedEvent_sendsAnalytic() {
sut.sendPayPalSelectedEvent()
XCTAssertEqual(mockAPIClient.postedAnalyticsEvents.first, "shopper-insights:paypal-selected")
sut.sendSelectedEvent(for: .payPal)
stechiu marked this conversation as resolved.
Show resolved Hide resolved
XCTAssertEqual(mockAPIClient.postedAnalyticsEvents.first, "shopper-insights:button-selected")
XCTAssertEqual(mockAPIClient.postedShopperSessionID, "fake-shopper-session-id")
}

Expand All @@ -233,8 +233,14 @@ class BTShopperInsightsClient_Tests: XCTestCase {
}

func testSendVenmoSelectedEvent_sendsAnalytic() {
sut.sendVenmoSelectedEvent()
XCTAssertEqual(mockAPIClient.postedAnalyticsEvents.first, "shopper-insights:venmo-selected")
sut.sendSelectedEvent(for: .venmo)
XCTAssertEqual(mockAPIClient.postedAnalyticsEvents.first, "shopper-insights:button-selected")
}

func testShopperInsightsClient_withSessionID_setSessionIDInMetadata() {
sut = BTShopperInsightsClient(apiClient: mockAPIClient, shopperSessionID: "123456")
XCTAssertEqual(mockAPIClient.metadata.sessionID, "123456")
XCTAssertEqual(mockAPIClient.postedAnalyticsEvents.first, "shopper-insights:button-selected")
XCTAssertEqual(mockAPIClient.postedShopperSessionID, "fake-shopper-session-id")
}

Expand Down
1 change: 1 addition & 0 deletions UnitTests/BraintreeTestShared/MockAPIClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ public class MockAPIClient: BTAPIClient {

public override func sendAnalyticsEvent(
_ name: String,
buttonType: String? = nil,
correlationID: String? = nil,
errorDescription: String? = nil,
merchantExperiment experiment: String? = nil,
Expand Down
Loading