amplitude / Amplitude-Android

Native Android SDK for Amplitude
MIT License
161 stars 90 forks source link

Doubling events in multi-process app #287

Open dmersiyanov opened 3 years ago

dmersiyanov commented 3 years ago

I've faced an issue with amplitude auto session tracking in the multi-process app. According to the logs, start_session and end_session events are sent twice or more times.

Also, you can check this user to see how is that look like in analytics - https://analytics.amplitude.com/dpdrussia/project/294323/search/amplitude_id%3D274642275421?sessionHandle=Xp1yKdg_Xp1x4AJ_D%2Fx9Ixd_BH2z_88f8c6d7-fd46-4b50-a392-b3cbf66f1bb5R&eventId=d6ab76df-dd6a-11eb-bf47-02ef0a11101f

Expected Behavior

Session auto-tracking events sent only once per whole application (not per process)

Current Behavior

Start_session and end_session events are sent twice or more times.

Possible Solution

Add internal checking if another process started by the application, check this lib for inspiration - https://github.com/int02h/primaree

Steps to Reproduce

  1. Create a service that runs in another process and declare it in manifest

    <service
        android:name="com.example.JobService"
        android:enabled="true"
        android:exported="false"
        android:permission="android.permission.BIND_JOB_SERVICE"
        android:process=":NewProcess" >
    </service>
  2. Init amplitude in android Application class in onCreate method (enable logging)

     Amplitude.getInstance().initialize(app, ApiKeys.AMPLITUDE)
        .enableForegroundTracking(app)
        .enableLocationListening()
        .trackSessionEvents(true)
        .enableLogging(true)
        .setLogLevel(Log.DEBUG)
  3. Open \ close app to generate required events

Environment

dantetam commented 3 years ago

Hello @dmersiyanov ,

That's an interesting test case, and obviously not a desired result. Apps running on multiple processes should not be treated in the SDK as multiple apps/users. We'll look into this

Dante

dantetam commented 3 years ago

Hello again @dmersiyanov ,

Could you help us by providing more information about your multiprocess setup?

I could not reproduce the bug with just a plain new service inside a separate process. I did analyze your charts and realize there are far too many start session events. However, the timing is often 10 minutes to 3 hours ahead. Do you believe the extra process is triggering a start session event when the user has not opened the app?

1) Which classes are calling the Amplitude SDK, just your main application? Is that all the instrumentation? 2) Are you doing any special multiprocess service binding? Could you provide as much manifest/gradle/file structure info as possible?

Dante

qingzhuozhen commented 3 years ago

Hi @dmersiyanov , we can't reproduce so far. Can you provide more information to help us reproduce or debug?

aabolfazl commented 10 months ago

I am currently encountering a specific issue with the Amplitude SDK in a multiprocess application but I am unsure. The problem manifests as a significant discrepancy in the data reported by Amplitude compared to another analytics service I am using. Specifically, there's a notable difference, approximately double, in the reported analytics.

Could you provide recommendations or best practices for creating such a clear test environment? This would help in isolating the issue and facilitating a more precise diagnosis.