Countly / countly-sdk-android

Countly Product Analytics Android SDK
https://count.ly/mobile-analytics
MIT License
679 stars 295 forks source link

Event gets associated with wrong user #131

Open xa17d opened 3 years ago

xa17d commented 3 years ago

Events recorded after changeDeviceIdWithoutMerge are still associated with the previous user.

Code to reproduce:

import android.app.Activity
import android.os.Bundle
import ly.count.android.sdk.Countly
import ly.count.android.sdk.CountlyConfig
import ly.count.android.sdk.DeviceId

class DemoActivity : Activity() {

    private lateinit var countly: Countly

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val config = CountlyConfig(
            application,
            CountlyIntegration.COUNTLY_APP_KEY,
            CountlyIntegration.COUNTLY_SERVER_URL
        ).apply {
            setHttpPostForced(true)
            setDeviceId("dg-20210507-test1")
            setLoggingEnabled(true)
            setParameterTamperingProtectionSalt(CountlyIntegration.COUNTLY_SALT)
        }

        countly = Countly.sharedInstance().init(config)

        countly.events().recordEvent("Test", mapOf("event" to "1"))

        countly.changeDeviceIdWithoutMerge(DeviceId.Type.DEVELOPER_SUPPLIED, "dg-20210507-test2")
        countly.events().recordEvent("Test2", mapOf("event" to "2"))

        countly.changeDeviceIdWithoutMerge(DeviceId.Type.DEVELOPER_SUPPLIED, "dg-20210507-test1")
        countly.events().recordEvent("Test", mapOf("event" to "3"))

        countly.doStoredRequests() // send data to server
    }

    override fun onStart() {
        super.onStart()
        countly.onStart(this)
    }

    override fun onStop() {
        super.onStop()
        countly.onStop()
    }
}

The real application is much more complex and initialization is happening in Application.onCreate, but the issue is also reproducible with the code provided above.

Expected Behavior

User with deviceId == "dg-20210507-test1" should have the events:

{"key":"Test", "segmentation":{"event":"1"}}
{"key":"Test", "segmentation":{"event":"3"}}

User with deviceId == "dg-20210507-test2" should have the events:

{"key":"Test2", "segmentation":{"event":"2"}}

Current Behavior

User with deviceId == "dg-20210507-test1" receives all events:

{"key":"Test", "segmentation":{"event":"1"}}
{"key":"Test2", "segmentation":{"event":"2"}}
{"key":"Test", "segmentation":{"event":"3"}}

User with deviceId == "dg-20210507-test2" doesn't receive any events:

Screenshots (if appropriate):

Screenshot of the current behavior from the user profile of user with deviceId == "dg-20210507-test1":

Screenshot 2021-05-07 at 19 08 23

Possible Solution

No proposal. I saw that code that should make sure that events are not associated with the wrong deviceId is in place, but it doesn't seem to work in the provided scenario. Could be a race condition when events are tracked too fast after each other.

Steps to Reproduce (for bugs)

  1. Copy code provided at top into an Android project.
  2. Insert proper values for CountlyIntegration.COUNTLY_APP_KEY, CountlyIntegration.COUNTLY_SERVER_URL, CountlyIntegration.COUNTLY_SALT.
  3. Run the app.
  4. Observe events in User Profiles on Countly Server.

More Description

Our app allows to switch users. We noticed that some events are associated with the wrong user. Especially those that are happening close to the changeDeviceIdWithoutMerge call. We tried to eliminate our own code as root cause and then tried to reproduce the issue with a minimal example. The result is the code provided in this issue.

Your Environment

schusterlich commented 3 years ago

I have the same issue and its posing a real challenge for us, because it breaks the consistency that we need. Hopefully this is fixed soon, for us it could also be a dealbreaker mid-term.

ArtursKadikis commented 3 years ago

Hello. I'm looking into this and we'll try to ship a fix for this in the next release shortly.

daniel-tailored commented 5 months ago

try to ship a fix for this in the next release shortly.

Any update on this @ArtursKadikis ? Was it already fixed?

Thx.

arifBurakDemiray commented 5 months ago

Hello 🙌 ,

We've addressed the issue, and it is resolved in most cases. However, there are a few edge cases where the problem might still occur. We have already merged the fix, which will be included in the upcoming release.

Thank you for your patience. Please feel free to contact me if you have any questions or need further assistance.

floschu commented 5 months ago

We've addressed the issue, and it is resolved in most cases. However, there are a few edge cases where the problem might still occur. We have already merged the fix, which will be included in the upcoming release.

when can we expect the upcoming release?

arifBurakDemiray commented 1 month ago

Hello,

We apologize for the delayed response.

We wanted to let you know that this has been addressed in the 24.7.0 release of the Countly Android SDK. The details are in version 24.7.0.

Please feel free to update to the 24.7.0, and let us know if you encounter any further issues.

Thanks again for your patience and understanding!