Skip to content

Allow configuration of custom userAgent #268

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

Merged
merged 1 commit into from
Nov 16, 2023
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
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