PostHog / posthog-js-lite

Reimplementation of posthog-js to be as light and modular as possible.
https://posthog.com/docs/libraries
MIT License
70 stars 36 forks source link

Can't await captures with `sendFeatureFlags` #99

Closed hansottowirtz closed 9 months ago

hansottowirtz commented 1 year ago

Bug description

Normally, this works:

posthog.capture({
  distinctId,
  event: "Purchase"
});
await posthog.flushAsync()

but this doesn't work (flushAsync doesn't flush this capture):

posthog.capture({
  distinctId,
  event: "Purchase",
  sendFeatureFlags: true
});
await posthog.flushAsync()

because super.getFeatureFlagsStateless is not waited-on on this line:

https://github.com/PostHog/posthog-js-lite/blob/32d74d2642168325a10cb1f9a6eab4f7a75ff0b8/posthog-node/src/posthog-node.ts#L106-L107

Related sub-libraries

Additional context

benjackwhite commented 1 year ago

Hmmm this is indeed a tricky one. We don't want to change the signature of the method but if we don't make it async and awaitable then there is a potential for the captured event to happen after that flush call... We would have to do something like allow the capture to receive promises that could later be awaited but that is... tricky.

marandaneto commented 9 months ago

There were a few improvements discussed here that might help with this issue, I don't think we need to make everything async but flushAsync should be able to send everything pending regardless of calling capture or just using sendFeatureFlags.

neilkakkar commented 9 months ago

oh, I think I fixed this here: https://github.com/PostHog/posthog-js-lite/pull/149 - didn't realise you had already mentioned the issue! 🤦

marandaneto commented 9 months ago

oh, I think I fixed this here: #149 - didn't realise you had already mentioned the issue! 🤦

26 Dec I was OOO, sorry missed that. Alright, let's close it then, thanks.

neilkakkar commented 9 months ago

Feel free to comment if this isn't solved @hansottowirtz , but now we track promises being enqueued from inside sendFeatureFlags, which should make sure they are sent on shutdown, although not necessarily on the flush immediately after the capture call.