Skip to content

Fixes for userAgent delay. Retrieved async now. #236

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 2 commits into from
Jun 26, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 7 additions & 3 deletions Sources/Segment/Plugins/Context.swift
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,6 @@ public class Context: PlatformPlugin {
"width": screen.width,
"height": screen.height
]
// user-agent
let userAgent = device.userAgent
context["userAgent"] = userAgent
// locale
if Locale.preferredLanguages.count > 0 {
context["locale"] = Locale.preferredLanguages[0]
Expand Down Expand Up @@ -147,6 +144,13 @@ public class Context: PlatformPlugin {
"wifi": wifi
]

// user-agent
// 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
context["userAgent"] = userAgent

// other stuff?? ...
}

Expand Down
33 changes: 16 additions & 17 deletions Sources/Segment/Plugins/Platforms/Vendors/AppleUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import WebKit

internal class iOSVendorSystem: VendorSystem {
private let device = UIDevice.current
@Atomic private static var asyncUserAgent: String? = nil

override var manufacturer: String {
return "Apple"
Expand Down Expand Up @@ -65,17 +66,15 @@ internal class iOSVendorSystem: VendorSystem {

override var userAgent: String? {
#if !os(tvOS)
var userAgent: String?

if Thread.isMainThread {
userAgent = WKWebView().value(forKey: "userAgent") as? String
} else {
DispatchQueue.main.sync {
userAgent = WKWebView().value(forKey: "userAgent") as? String
// BKS: 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.
if Self.asyncUserAgent == nil {
DispatchQueue.main.async {
Self.asyncUserAgent = WKWebView().value(forKey: "userAgent") as? String
}
}

return userAgent
return Self.asyncUserAgent
#else
// webkit isn't on tvos
return "unknown"
Expand Down Expand Up @@ -198,6 +197,7 @@ import WebKit

internal class MacOSVendorSystem: VendorSystem {
private let device = ProcessInfo.processInfo
@Atomic private static var asyncUserAgent: String? = nil

override var manufacturer: String {
return "Apple"
Expand Down Expand Up @@ -238,16 +238,15 @@ internal class MacOSVendorSystem: VendorSystem {
}

override var userAgent: String? {
var userAgent: String?
if Thread.isMainThread {
userAgent = WKWebView().value(forKey: "userAgent") as? String
} else {
DispatchQueue.main.sync {
userAgent = WKWebView().value(forKey: "userAgent") as? String
// BKS: 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.
if Self.asyncUserAgent == nil {
DispatchQueue.main.async {
Self.asyncUserAgent = WKWebView().value(forKey: "userAgent") as? String
}
}

return userAgent
return Self.asyncUserAgent
}

override var connection: ConnectionStatus {
Expand Down
6 changes: 6 additions & 0 deletions Tests/Segment-Tests/Analytics_Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,12 @@ final class Analytics_Tests: XCTestCase {
let outputReader = OutputReaderPlugin()
analytics.add(plugin: outputReader)

// prime the pump for userAgent, since it's retrieved async.
let vendorSystem = VendorSystem.current
while vendorSystem.userAgent == nil {
RunLoop.main.run(until: Date.distantPast)
}

waitUntilStarted(analytics: analytics)

// add a referrer
Expand Down