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

Can't set enableForegroundTracking on Android #119

Open
pdsouza opened this issue Apr 20, 2022 · 4 comments
Open

Can't set enableForegroundTracking on Android #119

pdsouza opened this issue Apr 20, 2022 · 4 comments

Comments

@pdsouza
Copy link

pdsouza commented Apr 20, 2022

Hello! Thanks for your work maintaining a React Native library for Amplitude!

I noticed that the React Native SDK doesn't expose the enableForegroundTracking method, which is highly recommended on Android for accurate session tracking according to the docs.

I can work around this by enabling it in the native code but it would be great to expose this in React Native as well. Developers who aren't familiar with the SDK may not even know that they are missing out on this on Android.

@yuhao900914
Copy link
Contributor

Thanks for sharing your feedback and the workaround.
That's a good call out, I will create a ticket related to this.

@ydv0121
Copy link

ydv0121 commented May 17, 2022

Hello @pdsouza
can you please guide me how can we enable this in native side?

@pdsouza
Copy link
Author

pdsouza commented May 17, 2022

@ydv0121 Sure, I think the simplest way is to just add some initialization code in your MainActivity's onCreate callback:

import com.amplitude.api.Amplitude;
import com.amplitude.api.AmplitudeClient;

public class MainActivity extends DevMenuAwareReactActivity {
  private AmplitudeClient mAmplitudeInstance;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    // ...
    mAmplitudeInstance = Amplitude.getInstance()
            .trackSessionEvents(true)
            .initialize(getApplicationContext(), "YOUR_AMPLITUDE_API_KEY")
            .enableForegroundTracking(getApplication());
    // ...
}

This React Native library just delegates calls down to the native Android Amplitude instance so with the above code it'll have foreground tracking enabled without you needing to set it from RN.

If you need precise control over when you enable foreground tracking, another option is to create a native module and expose a method enableForegroundTracking that will delegate to the native Amplitude client's enableForegroundTracking method. This way you can call it from RN whenever you want. You can read the docs to understand how to do this.

Hope this helps!

@ydv0121
Copy link

ydv0121 commented May 23, 2022

@pdsouza Thanks a ton. it helped me.
session tracking are working in Android side.Thanks.

@yuhao900914 i think its highly required this method in react-native sdk too.

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

No branches or pull requests

3 participants