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

Access to Analytics in Swift / Objective C (For hybrid react-native / bare workflow) #592

Open
mkrn opened this issue Jul 5, 2022 · 2 comments
Labels
enhancement New feature or request

Comments

@mkrn
Copy link

mkrn commented Jul 5, 2022

Summary

Is there a way to expose some modules from the iOS native part of the app from Swift or ObjectiveC?
Pre-2.0 version I was able to achieve that with

import Segment

class Track {
  static func track(event: String, properties: [String : Any]?) {
    Analytics.shared().track(event, properties: properties);
  }
}

The workaround seems to add Analytics for iOS separately. Is there a good way to share identity between 2 clients?

Proposed Solution

Expose some of the javascript methods so they can be called from ObjectiveC or Swift side of the app.

@mkrn mkrn added the enhancement New feature or request label Jul 5, 2022
@oscb
Copy link
Contributor

oscb commented Jul 14, 2022

We don't currently have a way to do this right now. The old library was just a wrapper over the Analytics.iOS/Android libraries whereas v2 was written with RN in mind from the start so we don't have yet exposed methods to the native side.

But this is a good idea. I can see the value here. We will put this in our roadmap!

@mkrn
Copy link
Author

mkrn commented Jul 19, 2022

Thanks @oscb

I end up using a workaround by sending iOS events to javascript side. This example doesn't support attributes, but you can add them:

import { DeviceEventEmitter, NativeEventEmitter, NativeModules, Platform } from 'react-native'
const emitter = Platform.OS === 'ios' ? new NativeEventEmitter(NativeModules.RNEventEmitter) : DeviceEventEmitter

const listener = emitter.addListener("track", (eventname) => {
      this.segmentClient.track(eventname, {  });
})

And in swift Track.swift

import Foundation

class Track {
  static func track(event: String, properties: [String : Any]?) {
    RNEventEmitter.emitter.sendEvent(withName: "track", body: event)
  }
}

So maybe adding a swift or Objective C class would emit events in JS library would do it.

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

2 participants