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

[Connect] Add analytic events (part 1/2) #4238

Merged
merged 8 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions Stripe.xcworkspace/contents.xcworkspacedata

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

106 changes: 102 additions & 4 deletions StripeConnect/StripeConnect.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@ public class AccountManagementViewController: UIViewController {

init(componentManager: EmbeddedComponentManager,
collectionOptions: AccountCollectionOptions,
loadContent: Bool) {
loadContent: Bool,
analyticsClientFactory: ComponentAnalyticsClientFactory) {
super.init(nibName: nil, bundle: nil)
webVC = ConnectComponentWebViewController(
componentManager: componentManager,
componentType: .accountManagement,
loadContent: loadContent
loadContent: loadContent,
analyticsClientFactory: analyticsClientFactory
) {
Props(collectionOptions: collectionOptions)
} didFailLoadWithError: { [weak self] error in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,15 @@ public class AccountOnboardingViewController: UIViewController {

init(props: Props,
componentManager: EmbeddedComponentManager,
loadContent: Bool
loadContent: Bool,
analyticsClientFactory: ComponentAnalyticsClientFactory
) {
super.init(nibName: nil, bundle: nil)
webVC = ConnectComponentWebViewController(
componentManager: componentManager,
componentType: .onboarding,
loadContent: loadContent
loadContent: loadContent,
analyticsClientFactory: analyticsClientFactory
) {
props
} didFailLoadWithError: { [weak self] error in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@ public class NotificationBannerViewController: UIViewController {

init(componentManager: EmbeddedComponentManager,
collectionOptions: AccountCollectionOptions,
loadContent: Bool) {
loadContent: Bool,
analyticsClientFactory: ComponentAnalyticsClientFactory) {
super.init(nibName: nil, bundle: nil)
webVC = ConnectComponentWebViewController(
componentManager: componentManager,
componentType: .notificationBanner,
loadContent: loadContent
loadContent: loadContent,
analyticsClientFactory: analyticsClientFactory
) {
Props(collectionOptions: collectionOptions)
} didFailLoadWithError: { [weak self] error in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ public class PaymentDetailsViewController: UIViewController {
public weak var delegate: PaymentDetailsViewControllerDelegate?

init(componentManager: EmbeddedComponentManager,
loadContent: Bool) {
loadContent: Bool,
analyticsClientFactory: ComponentAnalyticsClientFactory) {
super.init(nibName: nil, bundle: nil)
webVC = ConnectComponentWebViewController(
componentManager: componentManager,
componentType: .paymentDetails,
loadContent: loadContent
loadContent: loadContent,
analyticsClientFactory: analyticsClientFactory
) { [weak self] error in
guard let self else { return }
delegate?.paymentDetails(self, didFailLoadWithError: error)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ public class PayoutsViewController: UIViewController {
public weak var delegate: PayoutsViewControllerDelegate?

init(componentManager: EmbeddedComponentManager,
loadContent: Bool) {
loadContent: Bool,
analyticsClientFactory: ComponentAnalyticsClientFactory) {
super.init(nibName: nil, bundle: nil)
webVC = ConnectComponentWebViewController(
componentManager: componentManager,
componentType: .payouts,
loadContent: loadContent
loadContent: loadContent,
analyticsClientFactory: analyticsClientFactory
) { [weak self] error in
guard let self else { return }
delegate?.payouts(self, didFailLoadWithError: error)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//

import JavaScriptCore
import StripeCore
@_spi(STP) import StripeCore
import UIKit

/// Manages Connect embedded components
Expand All @@ -28,6 +28,12 @@ public class EmbeddedComponentManager {
// content should load.
var shouldLoadContent: Bool = true

// This should only be used for tests to mock the analytics logger
var analyticsClientFactory: ComponentAnalyticsClientFactory = {
ComponentAnalyticsClient(client: AnalyticsClientV2.sharedConnect,
commonFields: $0)
}
Comment on lines +32 to +35
Copy link
Contributor Author

@mludowise-stripe mludowise-stripe Nov 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using a factory pattern for test injection.

Initially I tried adding an AnalyticsClientV2 instance and was planning to use MockAnalyticsClientV2 in tests. But when I started writing tests in #4239, I realized that MockAnalyticsClientV2 would force us to test against the raw analytics payload and we'd be better served by our own Mock (MockComponentAnalyticsClient) so we could test on the ComponentAnalyticEvent types rather than a raw payload.


/**
Initializes a StripeConnect instance.

Expand Down Expand Up @@ -66,7 +72,9 @@ public class EmbeddedComponentManager {
/// Creates a payouts component
/// - Seealso: https://docs.stripe.com/connect/supported-embedded-components/payouts
public func createPayoutsViewController() -> PayoutsViewController {
.init(componentManager: self, loadContent: shouldLoadContent)
.init(componentManager: self,
loadContent: shouldLoadContent,
analyticsClientFactory: analyticsClientFactory)
}

/**
Expand Down Expand Up @@ -96,12 +104,15 @@ public class EmbeddedComponentManager {
collectionOptions: collectionOptions
),
componentManager: self,
loadContent: shouldLoadContent)
loadContent: shouldLoadContent,
analyticsClientFactory: analyticsClientFactory)
}

@_spi(DashboardOnly)
public func createPaymentDetailsViewController() -> PaymentDetailsViewController {
.init(componentManager: self, loadContent: shouldLoadContent)
.init(componentManager: self,
loadContent: shouldLoadContent,
analyticsClientFactory: analyticsClientFactory)
}

@_spi(DashboardOnly)
Expand All @@ -110,7 +121,8 @@ public class EmbeddedComponentManager {
) -> AccountManagementViewController {
.init(componentManager: self,
collectionOptions: collectionOptions,
loadContent: shouldLoadContent)
loadContent: shouldLoadContent,
analyticsClientFactory: analyticsClientFactory)
}

@_spi(DashboardOnly)
Expand All @@ -119,7 +131,8 @@ public class EmbeddedComponentManager {
) -> NotificationBannerViewController {
.init(componentManager: self,
collectionOptions: collectionOptions,
loadContent: shouldLoadContent)
loadContent: shouldLoadContent,
analyticsClientFactory: analyticsClientFactory)
}

/// Used to keep reference of all web views associated with this component manager.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,8 @@ extension ConnectJSURLParams {
}
}

var url: URL {
guard let data = try? JSONEncoder().encode(self),
let dict = try? JSONSerialization.jsonObject(with: data) as? [String: Any] else {
// TODO: MXMOBILE-2491 Log error
return StripeConnectConstants.connectJSBaseURL
}
func url() throws -> URL {
let dict = try jsonDictionary(with: .connectEncoder)

// Append as hash params
return URL(string: "#\(URLEncoder.queryString(from: dict))", relativeTo: StripeConnectConstants.connectJSBaseURL)!
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// AnalyticsClientV2+Connect.swift
// StripeConnect
//
// Created by Mel Ludowise on 10/4/24.
//

@_spi(STP) import StripeCore

extension AnalyticsClientV2 {
static let sharedConnect = AnalyticsClientV2(
clientId: "mobile_connect_sdk",
origin: "stripe-connect-ios"
)
}
Loading
Loading