Skip to content
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

visionOS support #130

Open
aureliendelrue opened this issue Mar 7, 2024 · 8 comments
Open

visionOS support #130

aureliendelrue opened this issue Mar 7, 2024 · 8 comments
Labels
enhancement New feature or request

Comments

@aureliendelrue
Copy link

In my application (iOS/macOS/watchOS/visionOS), your SDK is working for visionOS but your web console did not support it (no device type, device OS, etc...), currently the only way to filter visionOS events is by the application version. Any plan to support it ?

@aureliendelrue aureliendelrue added the enhancement New feature or request label Mar 7, 2024
@izaaz
Copy link
Collaborator

izaaz commented Mar 7, 2024

@aureliendelrue - Thanks for raising this issue. This is currently in our backlog but we don't have an ETA for this yet. Can you please share a link to the event that you sent from vision os but the device type info was not properly populated?

@aureliendelrue
Copy link
Author

@izaaz
Copy link
Collaborator

izaaz commented Mar 7, 2024

Thank you @aureliendelrue. I'll add this as an example for when this gets prioritized.

@drewolbrich
Copy link

@izaaz Thank you for working to add visionOS support. 😀

@drewolbrich
Copy link

drewolbrich commented Mar 26, 2024

UPDATE: Instead of using this code, use the more elegant enrichment plugin suggested by @izaaz which I posted below in #130 (comment)

@aureliendelrue Until Amplitude-Swift supports visionOS, I've discovered that as a workaround, if your app's analytics happens to be funneled through a single call to Amplitude/track(eventType:properties:options:), it appears you can use code like the following to manually specify the visionOS device and platform yourself:

#if os(visionOS)
    // As of March 2024, Amplitude-Swift doesn't report the visionOS platform or OS version,
    // so we do it ourselves. We can remove this once the Amplitude-Swift SDK supports visionOS.
    // See https://github.com/amplitude/Amplitude-Swift/issues/130
    private let manuallyReportVendorSystem = true
#else
    private let manuallyReportVendorSystem = false
#endif
    
    lazy var amplitude: Amplitude = {
        let configuration = Configuration(apiKey: AppConfiguration.amplitudeAPIKey)

        if manuallyReportVendorSystem {
            // We'll report these ourselves in `trackEvent(_:withEventProperties:)`.
            configuration.trackingOptions.disableTrackDeviceManufacturer()
            configuration.trackingOptions.disableTrackDeviceModel()
            configuration.trackingOptions.disableTrackIDFV()
            configuration.trackingOptions.disableTrackPlatform()
            configuration.trackingOptions.disableTrackOsName()
            configuration.trackingOptions.disableTrackOsVersion()
        }

        let amplitude = Amplitude(configuration: configuration)
        return amplitude
    }()
    
    func trackEvent(_ eventType: String, withEventProperties eventProperties: [String: Any] = [:]) {
        let eventOptions = EventOptions()
        
        if manuallyReportVendorSystem {
            eventOptions.deviceManufacturer = "Apple"
#if targetEnvironment(simulator)
            // This is consistent with the Amplitude-Swift SDK, which doesn't recognize the
            // simulator when running on Apple Silicon.
            eventOptions.deviceModel = ""
#else
            eventOptions.deviceModel = UIDevice.current.name
#endif
            eventOptions.idfv = UIDevice.current.identifierForVendor?.uuidString
            eventOptions.platform = UIDevice.current.systemName
            // This matches the behavior of the Amplitude-Swift SDK.
            eventOptions.osName = UIDevice.current.systemName.lowercased()
            eventOptions.osVersion = UIDevice.current.systemVersion
        }

        amplitude.track(eventType: eventType, eventProperties: eventProperties, options: eventOptions)
    }

@izaaz
Copy link
Collaborator

izaaz commented Mar 26, 2024

Thanks for the work around @drewolbrich. You can also create an enrichment plugin to move this logic to a separate class so your track method is not overloaded.

https://www.docs.developers.amplitude.com/data/sdks/ios-swift/#amplitude-sdk-plugin

@drewolbrich
Copy link

drewolbrich commented Mar 27, 2024

@izaaz Thank you. Here's the enrichment plugin I've created which performs the same function as the code above and doesn't require overloading the track method, as you suggested:

#if os(visionOS)

/// An Amplitude plugin that supplies the vendor system information that isn't
/// reported on visionOS in Amplitude-Swift 1.4.2.
/// See https://github.com/amplitude/Amplitude-Swift/issues/130
class MissingVendorSystemPlugin: Plugin {
    
    let type: PluginType

    var amplitude: Amplitude?

    init() {
        self.type = .enrichment
    }

    func setup(amplitude: Amplitude) {
        amplitude.configuration.trackingOptions.disableTrackDeviceManufacturer()
        amplitude.configuration.trackingOptions.disableTrackDeviceModel()
        amplitude.configuration.trackingOptions.disableTrackIDFV()
        amplitude.configuration.trackingOptions.disableTrackPlatform()
        amplitude.configuration.trackingOptions.disableTrackOsName()
        amplitude.configuration.trackingOptions.disableTrackOsVersion()

        self.amplitude = amplitude
    }

    func execute(event: BaseEvent) -> BaseEvent? {
        let eventOptions = EventOptions()

        eventOptions.deviceManufacturer = "Apple"
#if targetEnvironment(simulator)
        // This is consistent with the Amplitude-Swift SDK, which doesn't recognize the
        // simulator when running on an Apple Silicon Mac.
        eventOptions.deviceModel = ""
#else
        eventOptions.deviceModel = UIDevice.current.name
#endif
        eventOptions.idfv = UIDevice.current.identifierForVendor?.uuidString
        eventOptions.platform = UIDevice.current.systemName
        // This matches the behavior of the Amplitude-Swift SDK.
        eventOptions.osName = UIDevice.current.systemName.lowercased()
        eventOptions.osVersion = UIDevice.current.systemVersion

        event.mergeEventOptions(eventOptions: eventOptions)

        return event
    }

}

#endif // os(visionOS)

It can be used like this when the Amplitude object is created:

#if os(visionOS)
        // As of March 2024, Amplitude-Swift doesn't report the visionOS platform or OS
        // version, so we do it ourselves using this plugin. We can remove it once the
        // Amplitude-Swift SDK supports visionOS.
        // See https://github.com/amplitude/Amplitude-Swift/issues/130
        amplitude.add(plugin: MissingVendorSystemPlugin())
#endif

@drewolbrich
Copy link

drewolbrich commented Mar 27, 2024

Thanks for the work around @drewolbrich. You can also create an enrichment plugin to move this logic to a separate class so your track method is not overloaded.

https://www.docs.developers.amplitude.com/data/sdks/ios-swift/#amplitude-sdk-plugin

@izaaz FYI, the sample code at https://www.docs.developers.amplitude.com/data/sdks/ios-swift/#enrichment-type-plugin is incorrect.

The EnrichmentPlugin class defines this method:

func execute(event: BaseEvent?) -> BaseEvent?

but the Plugin protocol in Amplitude-Swift 1.4.2 requires a method with this signature:

func execute(event: BaseEvent) -> BaseEvent?

Consequently, the execute(event:) method in the EnrichmentPlugin sample code will never be called in Mediator.swift in the Swift package. The plugin won't have any effect, and no errors or warnings will be reported.

Please fix the documentation so other developers who try to implement the sample code at https://www.docs.developers.amplitude.com/data/sdks/ios-swift/#enrichment-type-plugin don't have the same issue.

Thank you. 😊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants