Skip to content

Commit

Permalink
Merge branch 'main' into bsneed/operatingMode
Browse files Browse the repository at this point in the history
  • Loading branch information
bsneed authored Nov 17, 2023
2 parents 53a31bb + 3fbc48f commit f95d841
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 8 deletions.
12 changes: 5 additions & 7 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version:5.3
// swift-tools-version:5.7
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription
Expand All @@ -20,18 +20,16 @@ let package = Package(
dependencies: [
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.0"),
.package(
name: "Sovran",
url: "https://github.com/segmentio/Sovran-Swift.git",
from: "1.1.0"
)
.package(url: "https://github.com/segmentio/sovran-swift.git", from: "1.1.0")
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages this package depends on.
.target(
name: "Segment",
dependencies: ["Sovran"]),
dependencies: [
.product(name: "Sovran", package: "sovran-swift")
]),
.testTarget(
name: "Segment-Tests",
dependencies: ["Segment"]),
Expand Down
6 changes: 6 additions & 0 deletions Sources/Segment/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@ public class Configuration {
var requestFactory: ((URLRequest) -> URLRequest)? = nil
var errorHandler: ((Error) -> Void)? = nil
var flushPolicies: [FlushPolicy] = [CountBasedFlushPolicy(), IntervalBasedFlushPolicy()]

var operatingMode: OperatingMode = .asynchronous
var flushQueue: DispatchQueue = OperatingMode.defaultQueue
var userAgent: String? = nil
}

internal var values: Values
Expand Down Expand Up @@ -211,6 +213,10 @@ public extension Configuration {
@discardableResult
func flushQueue(_ queue: DispatchQueue) -> Configuration {
values.flushQueue = queue

@discardableResult
func userAgent(_ userAgent: String) -> Configuration {
values.userAgent = userAgent
return self
}
}
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 f95d841

Please sign in to comment.