posthog-js icon indicating copy to clipboard operation
posthog-js copied to clipboard

Add `bootstrap` options to `posthog.reset()`

Open gustavopch opened this issue 2 years ago • 3 comments
trafficstars

I want to set my own anonymous ID instead of using the one generated by PostHog. I can do that when initializing. Great. But if the user logs out, I have to call posthog.reset() and then the next anonymous ID assigned will be the one generated by PostHog instead of the one I want.

I suggest posthog.reset() is adjusted to the following signature so I can pass the new anonymous ID:

reset(
  reset_device_id_or_options?:
    | boolean // <- For backwards compatibility
    | {
        resetDeviceID?: boolean
        bootstrap?: {
          distinctID?: string
          isIdentifiedID?: boolean
          featureFlags?: Record<string, boolean | string>
          featureFlagPayloads?: Record<string, JsonType>
        }
      }
): void

I'd call it like:

posthog.reset({
  bootstrap: {
    distinctID: myAnonymousID,
    isIdentifiedID: false
  }
})

Currently, the workaround that I can see is to reload the whole page when the user logs out so that posthog.init is called again and my anonymous ID is passed via its bootstrap option.

gustavopch avatar Oct 26 '23 21:10 gustavopch

+1

glassworks-projects avatar Jan 31 '24 16:01 glassworks-projects