| 30 | } |
| 31 | |
| 32 | export const setupPostHog = async ( |
| 33 | apiKey: string, |
| 34 | userId?: string, |
| 35 | groupId?: string, |
| 36 | ): Promise<void> => { |
| 37 | try { |
| 38 | await mutex.lock(LOCK_NAME); |
| 39 | posthog.init(apiKey, { |
| 40 | api_host: API_HOST, |
| 41 | loaded: async (posthog: any) => { |
| 42 | /* We need to do this to use elsewhere in more dynamic ways, outside the scope of this file */ |
| 43 | window.posthog = posthog; |
| 44 | userId && posthog.identify(userId); |
| 45 | groupId && posthog.group("company", groupId); |
| 46 | await mutex.release(LOCK_NAME); |
| 47 | }, |
| 48 | }); |
| 49 | } catch (e) { |
| 50 | console.error("Posthog setup error", e); |
| 51 | await mutex.release(LOCK_NAME); |
| 52 | } |
| 53 | }; |
| 54 | |
| 55 | /** |
| 56 | * async safely get the global posthog instance, sleeping until ready |