Skip to content

Commit

Permalink
Send network_type as metadata on analytics requests (stripe#2720)
Browse files Browse the repository at this point in the history
* Send network_type as metadata on analytics requests

* Format

* Update test

* Fix test
  • Loading branch information
porter-stripe authored Jul 10, 2023
1 parent c2a3aee commit 090bc51
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,9 @@ class STPAnalyticsClientPaymentSheetTest: XCTestCase {
let payload = client.payload(from: analytic, apiClient: apiClient)

// verify
XCTAssertEqual(15, payload.count)
XCTAssertEqual(16, payload.count)
XCTAssertNotNil(payload["device_type"] as? String)
XCTAssertEqual("Wi-Fi", payload["network_type"] as? String)
// In xctest, this is the version of Xcode
XCTAssertNotNil(payload["app_version"] as? String)
XCTAssertEqual("none", payload["ocr_type"] as? String)
Expand Down
2 changes: 1 addition & 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, 13)
XCTAssertEqual(payload.count, 14)

// Verify event name is included
XCTAssertEqual(payload["event"] as? String, mockAnalytic.event.rawValue)
Expand Down
54 changes: 54 additions & 0 deletions StripeCore/StripeCore/Source/Analytics/NetworkDetector.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//
// NetworkDetector.swift
// StripeCore
//
// Created by Nick Porter on 7/5/23.
//

import CoreTelephony
import Foundation
import SystemConfiguration

/// A class which can detect the current network type of the device
class NetworkDetector {

static func getConnectionType() -> String? {
guard let reachability = SCNetworkReachabilityCreateWithName(kCFAllocatorDefault, "www.stripe.com") else {
return nil
}

var flags = SCNetworkReachabilityFlags()
SCNetworkReachabilityGetFlags(reachability, &flags)

let isReachable = flags.contains(.reachable)
let isWWAN = flags.contains(.isWWAN)

guard isReachable else {
return nil
}

guard isWWAN else {
return "Wi-Fi"
}

let networkInfo = CTTelephonyNetworkInfo()
let carrierType = networkInfo.serviceCurrentRadioAccessTechnology

guard let carrierTypeName = carrierType?.first?.value else {
return "unknown"
}

switch carrierTypeName {
case CTRadioAccessTechnologyGPRS, CTRadioAccessTechnologyEdge, CTRadioAccessTechnologyCDMA1x:
return "2G"
case CTRadioAccessTechnologyWCDMA, CTRadioAccessTechnologyHSDPA, CTRadioAccessTechnologyHSUPA, CTRadioAccessTechnologyCDMAEVDORev0, CTRadioAccessTechnologyCDMAEVDORevA, CTRadioAccessTechnologyCDMAEVDORevB, CTRadioAccessTechnologyeHRPD:
return "3G"
case CTRadioAccessTechnologyLTE:
return "4G"
default:
return "5G"
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ extension STPAnalyticsClient {
payload["app_name"] = Bundle.stp_applicationName() ?? ""
payload["app_version"] = Bundle.stp_applicationVersion() ?? ""
payload["plugin_type"] = PluginDetector.shared.pluginType?.rawValue
payload["network_type"] = NetworkDetector.getConnectionType()
payload["install"] = InstallMethod.current.rawValue
payload["publishable_key"] = apiClient.sanitizedPublishableKey ?? "unknown"

Expand Down

0 comments on commit 090bc51

Please sign in to comment.