From a29e99d7d716bdecaf8762153733181ebec3f07c Mon Sep 17 00:00:00 2001 From: Jan Cizmar Date: Wed, 12 Jul 2023 17:37:02 +0200 Subject: [PATCH] fix: PostHog frontend initialization --- .../controllers/PublicControllerTest.kt | 2 ++ .../src/component/MandatoryDataProvider.tsx | 19 +++++++++++-------- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/backend/app/src/test/kotlin/io/tolgee/controllers/PublicControllerTest.kt b/backend/app/src/test/kotlin/io/tolgee/controllers/PublicControllerTest.kt index 0c7360e004..0c7f64f210 100644 --- a/backend/app/src/test/kotlin/io/tolgee/controllers/PublicControllerTest.kt +++ b/backend/app/src/test/kotlin/io/tolgee/controllers/PublicControllerTest.kt @@ -14,6 +14,7 @@ import io.tolgee.testing.assertions.Assertions.assertThat import org.junit.jupiter.api.AfterEach import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.Test +import org.mockito.Mockito import org.mockito.kotlin.any import org.mockito.kotlin.eq import org.mockito.kotlin.times @@ -31,6 +32,7 @@ class PublicControllerTest : @BeforeEach fun setup() { + Mockito.reset(postHog) canCreateOrganizations = tolgeeProperties.authentication.userCanCreateOrganizations } diff --git a/webapp/src/component/MandatoryDataProvider.tsx b/webapp/src/component/MandatoryDataProvider.tsx index 53400d835f..3c6595348b 100644 --- a/webapp/src/component/MandatoryDataProvider.tsx +++ b/webapp/src/component/MandatoryDataProvider.tsx @@ -28,11 +28,12 @@ export const MandatoryDataProvider = (props: any) => { function initPostHog() { let postHogPromise: Promise | undefined; if (!window[POSTHOG_INITIALIZED_WINDOW_PROPERTY]) { - window[POSTHOG_INITIALIZED_WINDOW_PROPERTY] = true; - postHogPromise = import('posthog-js').then((m) => m.default); - postHogPromise.then((posthog) => { - if (config?.postHogApiKey) { - posthog.init(config.postHogApiKey, { + const postHogAPIKey = config?.postHogApiKey; + if (postHogAPIKey) { + window[POSTHOG_INITIALIZED_WINDOW_PROPERTY] = true; + postHogPromise = import('posthog-js').then((m) => m.default); + postHogPromise.then((posthog) => { + posthog.init(postHogAPIKey, { api_host: config?.postHogHost || undefined, }); posthog.identify(userData!.id.toString(), { @@ -40,11 +41,13 @@ export const MandatoryDataProvider = (props: any) => { email: userData!.username, ...getUtmParams(), }); - } - }); + }); + } } return () => { - postHogPromise?.then((ph) => ph.reset()); + postHogPromise?.then((ph) => { + ph.reset(); + }); window[POSTHOG_INITIALIZED_WINDOW_PROPERTY] = false; }; }