Skip to content

On opt-in in intial user session, the queue is never flushed #2401

@bvanderdrift

Description

@bvanderdrift

Bug Description

I have an issue with data not showing up on first session only. I debug in live view

When I act as a new user (anonymous browser) the following happens:

How to reproduce

  • init with opt_out_capturing_by_default: true
  • Cookie banner is shown
  • I accept
  • $opt_in event is correctly received in live view
  • I change page -> console logs [PostHog.js] send "$pageview" but no event shows in live view
  • I change page again -> console logs [PostHog.js] send "$pageview" but no event shows in live view
  • I change page once more -> console logs [PostHog.js] send "$pageview" but no event shows in live view
  • I wait 1 minute (tried with 10 minutes too just to be sure)
  • I click a button -> console logs [PostHog.js] send "$autocapture" but no event shows in live view
  • at this point no other event than $opt_in are shown in the live events view
  • I refresh the page
  • now all the events (3x page view and button click) show up backdated correctly.

This is reproducable 100% of the time.

Any future sessions work as expected -> events are correctly periodically flushed from now on

Additional context

posthog-js: 1.269.1

Opt-in logic

export const usePosthogStateSyncEffect = () => {
  const { isAuthenticated, isLoading, user } = useCurrentUser()
  const posthog = usePostHog()
  const didUserAllowTracking = useDidUserAllowTracking()

  useEffect(() => {
    if (isLoading) return

    if (user) {
      posthog.identify(user._id)
    } else {
      posthog.reset()
    }
  }, [isAuthenticated, user, posthog])

  useEffect(() => {
    if (didUserAllowTracking && !posthog.has_opted_in_capturing()) {
      posthog.opt_in_capturing()
    } else if (didUserAllowTracking === false) {
      posthog.opt_out_capturing()
    }
  }, [posthog, didUserAllowTracking])
}

init logic

<PosthogProvider
  options={{
    api_host: config.posthog.apiHost,
    defaults: '2025-05-24',
    opt_out_capturing_by_default: true,
    cookieless_mode: 'on_reject',
    capture_exceptions: true,
    debug: !config.isProduction,
  }}
>
...

Debug info

Session: https://us.posthog.com/project/sTMFPsFhdP1Ssg/replay/0199ab5a-656d-76ec-b952-f4fae2dba896?t=1112
Admin: http://go/adminOrgEU/0199a963-d0ff-0000-8186-3a87be298b7b (project ID 92410)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions