diff --git a/Sources/Segment/Plugins/Platforms/Vendors/VendorSystem.swift b/Sources/Segment/Plugins/Platforms/Vendors/VendorSystem.swift index d2a71a21..f64a1939 100644 --- a/Sources/Segment/Plugins/Platforms/Vendors/VendorSystem.swift +++ b/Sources/Segment/Plugins/Platforms/Vendors/VendorSystem.swift @@ -68,6 +68,8 @@ internal class VendorSystem { static var current: VendorSystem { #if os(iOS) || os(tvOS) || targetEnvironment(macCatalyst) return iOSVendorSystem() + #elseif os(macOS) + return MacOSVendorSystem() #elseif os(watchOS) return watchOSVendorSystem() #elseif os(Linux) diff --git a/Sources/Segment/Startup.swift b/Sources/Segment/Startup.swift index c1164d3f..2d9298f3 100644 --- a/Sources/Segment/Startup.swift +++ b/Sources/Segment/Startup.swift @@ -37,12 +37,12 @@ extension Analytics: Subscriber { internal func platformPlugins() -> [PlatformPlugin]? { var plugins = [PlatformPlugin]() + // add context plugin as well as it's platform specific internally. + // this must come first. + plugins.append(Context()) + // setup lifecycle if desired if configuration.values.trackApplicationLifecycleEvents { - // add context plugin as well as it's platform specific internally. - // this must come first. - plugins.append(Context()) - #if os(iOS) || os(tvOS) plugins += [iOSLifecycleMonitor(), iOSLifecycleEvents(), DeviceToken()] #endif diff --git a/Tests/Segment-Tests/Analytics_Tests.swift b/Tests/Segment-Tests/Analytics_Tests.swift index 59250b3a..3d1bfc02 100644 --- a/Tests/Segment-Tests/Analytics_Tests.swift +++ b/Tests/Segment-Tests/Analytics_Tests.swift @@ -111,6 +111,39 @@ final class Analytics_Tests: XCTestCase { XCTAssertTrue(anonId.count == 36) // it's a UUID y0. } + func testContext() { + let analytics = Analytics(configuration: Configuration(writeKey: "test")) + let outputReader = OutputReaderPlugin() + analytics.add(plugin: outputReader) + + waitUntilStarted(analytics: analytics) + + 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!") + + // this key not present on watchOS (doesn't have webkit) + #if !os(watchOS) + XCTAssertNotNil(context?["userAgent"], "userAgent missing!") + #endif + + // 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()