posthog-js
posthog-js copied to clipboard
Sessions do not get recorded once user opts in
Describe the bug Sessions do not get recorded if initially the user is opted out from capturing events and later opted in.
Steps To Reproduce
- Initialize posthog like this:
posthog.init(token, { api_host: apiHost, autocapture: false, opt_out_capturing_by_default: true, });
- After initialization, opt the user in by calling:
posthog.opt_in_capturing(); posthog.startSessionRecording();
Expected Behavior
The sessions starts recording after posthog.opt_in_capturing()
and posthog.startSessionRecording()
are called.
Actual Behavior The session is not being recorded.
More Context On Business Requirement We don't want to record users' sessions until they give cookies consent, so initially we opt them out from capturing events, but once they confirm the cookies we'd like to start recording the session.
Similar issue here.
We are not doing session recording, but manually capturing certain events. We find that the $opt_in
event is automatically sent when the user opts in, but no further events are sent until the the posthog instance is re-initialised with the opted-in state.
To add a bit more context after some more debugging - I've noticed that it's possible for the posthog instance to have both has_opted_in_capturing
and has_opted_out_capturing
false. ie, there is a third state where the user has not opted in or opted out of tracking. I guess this makes sense after thinking about it, because the user hasn't actually made a choice yet, but it is not obvious from the docs or intuitively (for me at least). It's also not obvious that even if the opted in value is false, posthog will happily capture events as long as the opted out value is false. This is a problem for privacy conscious apps such as ours. What meaning does 'opting in' have in this context?
It appears that in my case either of initialising posthog with opt_out_capturing_by_default: true
, or calling posthog.opt_out_capturing()
immediately after initialisation without specifying a value for opt_out_capturing_by_default
, will prevent posthog from being able to capture events even once posthog.opt_in_capturing()
is called.
To add to the confusion, calling posthog.debug
results in console logs saying that posthog is sending events, but the events never show up in my live events stream in posthog.
I think I’ve narrowed down the problem to this if statement, looks like both clear_persistence and enable_persistence are set to true so it's not making it to the else_if and not re-enabling persistence.
cc: @benjackwhite
Any solution or explanation to this behavior?
After doing some debugging, it looks like the issue is that the events are getting queued (hence @chidg seeing the events in the debug console logs), but the queue isn't running. There should be a call to _start_queue_if_opted_in
somewhere within in opt_in_capturing
.
The workaround I found is to manually call this (internal) function:
posthog.opt_in_capturing();
posthog._start_queue_if_opted_in();