diff --git a/Sources/Segment/Configuration.swift b/Sources/Segment/Configuration.swift index f5134ad5..bbeba21e 100644 --- a/Sources/Segment/Configuration.swift +++ b/Sources/Segment/Configuration.swift @@ -26,6 +26,7 @@ public class Configuration { var requestFactory: ((URLRequest) -> URLRequest)? = nil var errorHandler: ((Error) -> Void)? = nil var flushPolicies: [FlushPolicy] = [CountBasedFlushPolicy(), IntervalBasedFlushPolicy()] + var userAgent: String? = nil } internal var values: Values @@ -182,6 +183,12 @@ public extension Configuration { values.flushPolicies = policies return self } + + @discardableResult + func userAgent(_ userAgent: String) -> Configuration { + values.userAgent = userAgent + return self + } } extension Analytics { diff --git a/Sources/Segment/Plugins/Context.swift b/Sources/Segment/Plugins/Context.swift index 16f24ad8..3590a625 100644 --- a/Sources/Segment/Plugins/Context.swift +++ b/Sources/Segment/Plugins/Context.swift @@ -148,7 +148,7 @@ public class Context: PlatformPlugin { // BKS: This use to be in the static section, however it was discovered that on some platforms // there can be a delay in retrieval. It has to be fetched on the main thread, so we've spun it off // async and cache it when it comes back. - let userAgent = device.userAgent + let userAgent = analytics?.configuration.values.userAgent ?? device.userAgent context["userAgent"] = userAgent // other stuff?? ... diff --git a/Tests/Segment-Tests/Analytics_Tests.swift b/Tests/Segment-Tests/Analytics_Tests.swift index 5ea8e05f..de9eb28d 100644 --- a/Tests/Segment-Tests/Analytics_Tests.swift +++ b/Tests/Segment-Tests/Analytics_Tests.swift @@ -165,6 +165,53 @@ final class Analytics_Tests: XCTestCase { #endif } + + func testContextWithUserAgent() { + let configuration = Configuration(writeKey: "test") + configuration.userAgent("testing user agent") + let analytics = Analytics(configuration: configuration) + let outputReader = OutputReaderPlugin() + analytics.add(plugin: outputReader) + +#if !os(watchOS) && !os(Linux) + // prime the pump for userAgent, since it's retrieved async. + let vendorSystem = VendorSystem.current + while vendorSystem.userAgent == nil { + RunLoop.main.run(until: Date.distantPast) + } +#endif + + waitUntilStarted(analytics: analytics) + + // add a referrer + analytics.openURL(URL(string: "https://google.com")!) + + analytics.track(name: "token check") + + let trackEvent: TrackEvent? = outputReader.lastEvent as? TrackEvent + let context = trackEvent?.context?.dictionaryValue + // Verify that context isn't empty here. + // We need to verify the values but will do that in separate platform specific tests. + XCTAssertNotNil(context) + XCTAssertNotNil(context?["screen"], "screen missing!") + XCTAssertNotNil(context?["network"], "network missing!") + XCTAssertNotNil(context?["os"], "os missing!") + XCTAssertNotNil(context?["timezone"], "timezone missing!") + XCTAssertNotNil(context?["library"], "library missing!") + XCTAssertNotNil(context?["device"], "device missing!") + + let referrer = context?["referrer"] as! [String: Any] + XCTAssertEqual(referrer["url"] as! String, "https://google.com") + + XCTAssertEqual(context?["userAgent"] as! String, "testing user agent") + + // these keys not present on linux +#if !os(Linux) + XCTAssertNotNil(context?["app"], "app missing!") + XCTAssertNotNil(context?["locale"], "locale missing!") +#endif + } + func testDeviceToken() { let analytics = Analytics(configuration: Configuration(writeKey: "test")) let outputReader = OutputReaderPlugin()