Skip to content

Commit

Permalink
Send analytics events on simulator but not during tests (stripe#3501)
Browse files Browse the repository at this point in the history
## Summary
- Updates the `STPAnalyticsClient` to only send analytics if we are not
in a test. Now includes sending when running from a simulator.
- Intentionally does not update the V2 analytics client as that is used
outside the scope of our team.
- Left in place the `STPAnalyticsClientDelegate` so our playground can
still inspect analytics logs easily.

## Motivation
https://jira.corp.stripe.com/browse/MOBILESDK-1903

## Testing
Manual

## Changelog
N/A
  • Loading branch information
porter-stripe authored Apr 10, 2024
1 parent 8872ece commit f628dbc
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 23 deletions.
3 changes: 0 additions & 3 deletions Stripe/StripeiOS/Source/STPAnalyticsClient+BasicUI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,6 @@ extension STPPaymentContext {
}

var params: [String: Any] = ["selected_lpm": paymentMethodType]
if STPAnalyticsClient.isSimulatorOrTest {
params["is_development"] = true
}
if let error {
params["error_message"] = error.makeSafeLoggingString()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ class STPAnalyticsClientPaymentSheetTest: XCTestCase {
let payload = client.payload(from: analytic, apiClient: apiClient)

// verify
XCTAssertEqual(16, payload.count)
XCTAssertEqual(17, payload.count)
XCTAssertNotNil(payload["device_type"] as? String)
XCTAssertEqual("Wi-Fi", payload["network_type"] as? String)
// In xctest, this is the version of Xcode
Expand All @@ -208,6 +208,7 @@ class STPAnalyticsClientPaymentSheetTest: XCTestCase {
XCTAssertEqual(STPAPIClient.STPSDKVersion, payload["bindings_version"] as? String)
XCTAssertEqual("testVal", payload["testKey"] as? String)
XCTAssertEqual("X", payload["install"] as? String)
XCTAssertTrue(payload["is_development"] as? Bool ?? false)

let additionalInfo = try XCTUnwrap(payload["additional_info"] as? [String])
XCTAssertEqual(1, additionalInfo.count)
Expand Down
5 changes: 4 additions & 1 deletion Stripe/StripeiOSTests/STPAnalyticsClientPaymentsTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class STPAnalyticsClientPaymentsTest: XCTestCase {
let mockAnalytic = MockAnalytic()
let payload = client.payload(from: mockAnalytic)

XCTAssertEqual(payload.count, 14)
XCTAssertEqual(payload.count, 15)

// Verify event name is included
XCTAssertEqual(payload["event"] as? String, mockAnalytic.event.rawValue)
Expand All @@ -61,6 +61,9 @@ class STPAnalyticsClientPaymentsTest: XCTestCase {

// Verify install method is Xcode
XCTAssertEqual(payload["install"] as? String, "X")

// Verify is_development
XCTAssertTrue(payload["is_development"] as? Bool ?? false)
}

// MARK: - Error tests
Expand Down
17 changes: 9 additions & 8 deletions StripeCore/StripeCore/Source/Analytics/STPAnalyticsClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,12 @@ import UIKit
#if targetEnvironment(simulator)
return true
#else
return NSClassFromString("XCTest") != nil
return isUnitOrUITest
#endif
}

@objc public class func shouldCollectAnalytics() -> Bool {
return !isSimulatorOrTest
static var isUnitOrUITest: Bool {
return NSClassFromString("XCTest") != nil || ProcessInfo.processInfo.environment["UITesting"] != nil
}

public func additionalInfo() -> [String] {
Expand Down Expand Up @@ -115,11 +115,9 @@ import UIKit
delegate?.analyticsClientDidLog(analyticsClient: self, payload: payload)
#endif

guard type(of: self).shouldCollectAnalytics() else {
// Don't send the analytic, but add it to `_testLogHistory` if we're in a test.
if NSClassFromString("XCTest") != nil {
_testLogHistory.append(payload)
}
// If in testing, don't log analytic, instead append payload to log history
guard !STPAnalyticsClient.isUnitOrUITest else {
_testLogHistory.append(payload)
return
}

Expand Down Expand Up @@ -150,6 +148,9 @@ extension STPAnalyticsClient {
payload["network_type"] = NetworkDetector.getConnectionType()
payload["install"] = InstallMethod.current.rawValue
payload["publishable_key"] = apiClient.sanitizedPublishableKey ?? "unknown"
if STPAnalyticsClient.isSimulatorOrTest {
payload["is_development"] = true
}

return payload
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import XCTest

class STPAnalyticsClientTest: XCTestCase {

func testShouldCollectAnalytics_alwaysFalseInTest() {
XCTAssertFalse(STPAnalyticsClient.shouldCollectAnalytics())
func testIsUnitOrUITest_alwaysTrueInTest() {
XCTAssertTrue(STPAnalyticsClient.isUnitOrUITest)
}

func testShouldRedactLiveKeyFromLog() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ extension STPAnalyticsClient {
apiClient: STPAPIClient
) {
var additionalParams = [:] as [String: Any]
if Self.isSimulatorOrTest {
additionalParams["is_development"] = true
}

additionalParams["address_data_blob"] = addressAnalyticData?.analyticsPayload

let analytic = AddressAnalytic(event: event,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,10 +304,6 @@ extension STPAnalyticsClient {
apiClient: STPAPIClient = .shared
) {
var additionalParams = [:] as [String: Any]
if Self.isSimulatorOrTest {
additionalParams["is_development"] = true
}

additionalParams["duration"] = duration
additionalParams["link_enabled"] = linkEnabled
additionalParams["active_link_session"] = activeLinkSession
Expand Down

0 comments on commit f628dbc

Please sign in to comment.