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

call without distinctId (mandatory) causes ~10 second timeout awaiting client.shtdown() #253

Open tompscanlan opened 3 months ago

tompscanlan commented 3 months ago

Bug description

In a minimal node http handler I'm trying to use posthog to capture an event. I'm seeing a ~10 second timeout when waiting on the call to shutdown(). At that time I get the error:

ERROR Error while shutting down PostHog HTTP error while fetching PostHog: 400

at retriable (node_modules/posthog-core/src/index.ts:652:17) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at retriable (node_modules/posthog-core/src/utils.ts:27:19) at PostHog.fetchWithRetry (node_modules/posthog-core/src/index.ts:635:12) at PostHog._flush (node_modules/posthog-core/src/index.ts:605:7)

How to reproduce

This is a Nuxt project backend running in node 20.


export default defineEventHandler(async (event) => {
  let posthogClient = null;
  try {
    posthogClient = new PostHog("XXXXX", {
      host: "https://us.i.posthog.com",
    });
    posthogClient.on("error", (err) => {
        console.error("PostHog had an error!", err);
    });
    posthogClient.debug();
    posthogClient.capture({
      distinct_id: "test",
      event: "Test",
    });

  } catch (error) {
    console.error("Error initializing PostHog:", error);
  } finally {
  await posthogClient.shutdown();
}
});

Additional context

I'm not to posthog, and a hack front end person, I'm probably doing something silly. I'm getting front end events through just fine using the same api key, but none of my backend events show up, and get a 400 instead.

tompscanlan commented 3 months ago

Oh... I may have found it... distinctId is different than distinct_id. Changing that seems to make the difference.

Chalking this one up to new-userism... might want a documentation change at https://posthog.com/docs/libraries/node. Seeing distinct_id so much stuck in my head. maybe switch up the examples, or get smarter users.

benjackwhite commented 3 months ago

I think we should perhaps do some more SDK side alerting if a call happens without a distinctId. Most people seem to use Typescript which indicates this immediately but we may need to rely on that less...