Skip to content

Commit f1bf66a

Browse files
authored
Allow configuration of custom userAgent (#268)
1 parent e1bd73f commit f1bf66a

File tree

3 files changed

+55
-1
lines changed

3 files changed

+55
-1
lines changed

Sources/Segment/Configuration.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public class Configuration {
2626
var requestFactory: ((URLRequest) -> URLRequest)? = nil
2727
var errorHandler: ((Error) -> Void)? = nil
2828
var flushPolicies: [FlushPolicy] = [CountBasedFlushPolicy(), IntervalBasedFlushPolicy()]
29+
var userAgent: String? = nil
2930
}
3031

3132
internal var values: Values
@@ -182,6 +183,12 @@ public extension Configuration {
182183
values.flushPolicies = policies
183184
return self
184185
}
186+
187+
@discardableResult
188+
func userAgent(_ userAgent: String) -> Configuration {
189+
values.userAgent = userAgent
190+
return self
191+
}
185192
}
186193

187194
extension Analytics {

Sources/Segment/Plugins/Context.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ public class Context: PlatformPlugin {
148148
// BKS: This use to be in the static section, however it was discovered that on some platforms
149149
// there can be a delay in retrieval. It has to be fetched on the main thread, so we've spun it off
150150
// async and cache it when it comes back.
151-
let userAgent = device.userAgent
151+
let userAgent = analytics?.configuration.values.userAgent ?? device.userAgent
152152
context["userAgent"] = userAgent
153153

154154
// other stuff?? ...

Tests/Segment-Tests/Analytics_Tests.swift

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,53 @@ final class Analytics_Tests: XCTestCase {
165165
#endif
166166
}
167167

168+
169+
func testContextWithUserAgent() {
170+
let configuration = Configuration(writeKey: "test")
171+
configuration.userAgent("testing user agent")
172+
let analytics = Analytics(configuration: configuration)
173+
let outputReader = OutputReaderPlugin()
174+
analytics.add(plugin: outputReader)
175+
176+
#if !os(watchOS) && !os(Linux)
177+
// prime the pump for userAgent, since it's retrieved async.
178+
let vendorSystem = VendorSystem.current
179+
while vendorSystem.userAgent == nil {
180+
RunLoop.main.run(until: Date.distantPast)
181+
}
182+
#endif
183+
184+
waitUntilStarted(analytics: analytics)
185+
186+
// add a referrer
187+
analytics.openURL(URL(string: "https://google.com")!)
188+
189+
analytics.track(name: "token check")
190+
191+
let trackEvent: TrackEvent? = outputReader.lastEvent as? TrackEvent
192+
let context = trackEvent?.context?.dictionaryValue
193+
// Verify that context isn't empty here.
194+
// We need to verify the values but will do that in separate platform specific tests.
195+
XCTAssertNotNil(context)
196+
XCTAssertNotNil(context?["screen"], "screen missing!")
197+
XCTAssertNotNil(context?["network"], "network missing!")
198+
XCTAssertNotNil(context?["os"], "os missing!")
199+
XCTAssertNotNil(context?["timezone"], "timezone missing!")
200+
XCTAssertNotNil(context?["library"], "library missing!")
201+
XCTAssertNotNil(context?["device"], "device missing!")
202+
203+
let referrer = context?["referrer"] as! [String: Any]
204+
XCTAssertEqual(referrer["url"] as! String, "https://google.com")
205+
206+
XCTAssertEqual(context?["userAgent"] as! String, "testing user agent")
207+
208+
// these keys not present on linux
209+
#if !os(Linux)
210+
XCTAssertNotNil(context?["app"], "app missing!")
211+
XCTAssertNotNil(context?["locale"], "locale missing!")
212+
#endif
213+
}
214+
168215
func testDeviceToken() {
169216
let analytics = Analytics(configuration: Configuration(writeKey: "test"))
170217
let outputReader = OutputReaderPlugin()

0 commit comments

Comments
 (0)