Skip to content

Commit

Permalink
Allow configuration of custom userAgent (#268)
Browse files Browse the repository at this point in the history
  • Loading branch information
hbmartin authored Nov 16, 2023
1 parent e1bd73f commit f1bf66a
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
7 changes: 7 additions & 0 deletions Sources/Segment/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion Sources/Segment/Plugins/Context.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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?? ...
Expand Down
47 changes: 47 additions & 0 deletions Tests/Segment-Tests/Analytics_Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit f1bf66a

Please sign in to comment.