Posthog nextjs set user/event properties in autocapture mode

635 Views Asked by At

I am initializing posthog in my nextjs application using

import { usePostHog } from 'next-use-posthog'
usePostHog(POSTHOG_KEY, { api_host: POSTHOG_HOST })

In _app.js at a different route when the user session starts i want to set custom properties on all events captured against this user. For which i have used

posthog.people.set({ consentId: decryptedData.srcref })
posthog.people.set({ orgId: decryptedData.fi })
posthog.register({
          consentId: decryptedData.srcref,
          orgId: decryptedData.fi
})

After initilaizing posthog again in the useEffect function at this route.

  import  posthog  from 'posthog-js';
  posthog.init(POSTHOG_KEY, { api_host: POSTHOG_HOST });

If i do not extensively initialize posthog again maunaly at this page the posthog.set does not work as posthog object in not defined.

The problem with this setup is there are two events pushed for the same event one by the auto capture and another by manual capture i setup. one event log contains set values other does not.

How can i set these values on the autocapture event itself removing the manual declaration of posthog again at login route?

0

There are 0 best solutions below