PostHog / posthog-android

PostHog Android SDK
https://posthog.com/docs/libraries/android
MIT License
39 stars 22 forks source link

Add `$is_identified` #136

Closed marandaneto closed 1 month ago

marandaneto commented 4 months ago

Description

https://github.com/PostHog/posthog/pull/22216

$is_identified is the property to filter identified users or not (anonymous).

https://github.com/PostHog/posthog-js/blob/7022ab69505959fb065d41870d2bceeb69145916/src/posthog-core.ts#L912 JS SDK uses the $user_state super property to track if the user has identified already or not with the values identified or anonymous (this super property is in deny list so not set in the event)

Add to:

marandaneto commented 4 months ago

See this comment before implementing https://github.com/PostHog/posthog/issues/10659#issuecomment-2142256209

beradeep commented 2 months ago

Hi, can I take up this issue? I think adding the is_identified property to the PostHog class with a get() getter that checks if the anonymousId is not equal to the distinctId, will solve this.

marandaneto commented 2 months ago

Hi, can I take up this issue? I think adding the is_identified property to the PostHog class with a get() getter that checks if the anonymousId is equal to the distinctId, will solve this.

Hello, sure, feel free to open a Draft PR and I can guide you, I'd link at https://github.com/PostHog/posthog-js/blob/7022ab69505959fb065d41870d2bceeb69145916/src/posthog-core.ts#L912 instead since its more reliable than just comparing the distinctId and anonymousId.

beradeep commented 2 months ago

Thanks for guiding me. Can you explain me why will it be more reliable than comparing them? Going through PostHog.kt, what I've understood is that, if identify() hasn't been invoked earlier i.e. distinctId hasn't been set, then distinctId defaults to the anonymousId. Once identify() is invoked, the distinctId is changed and set to distinctId param of identify(). So, comparing distinctId and anonymousId would be in my opinion a single source of truth to verify if the user is identified or not.

marandaneto commented 2 months ago

@beradeep good point and I don't disagree but this is a heuristics approach, this behavior may change hence implementing directly on identify and reset would be the correct approach without side effects in case this changes.

beradeep commented 2 months ago

Ah, I see. Also going through posthog-js, it seems that an "identify" event is captured only when new_distinct_id !== previous_distinct_id && isKnownAnonymous is true, however in posthog-android, it is captured whenever we call identify(). Is there any specific reason to that?

marandaneto commented 2 months ago

Ah, I see. Also going through posthog-js, it seems that an "identify" event is captured only when new_distinct_id !== previous_distinct_id && isKnownAnonymous is true, however in posthog-android, it is captured whenever we call identify(). Is there any specific reason to that?

Nope, something I likely missed during the Android implementation or that was added after on the JS SDK, we can either address that too or make a new issue.

beradeep commented 2 months ago

Nope, something I likely missed during the Android implementation or that was added after on the JS SDK, we can either address that too or make a new issue.

I'm sorry, I just noticed that there's already an open issue #108 mentioning this. Yes, I think both these issues are related, and can be resolved together.