Skip to content

Commit

Permalink
fix: PostHog frontend initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
JanCizmar committed Jul 12, 2023
1 parent 52339c4 commit a29e99d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -31,6 +32,7 @@ class PublicControllerTest :

@BeforeEach
fun setup() {
Mockito.reset(postHog)
canCreateOrganizations = tolgeeProperties.authentication.userCanCreateOrganizations
}

Expand Down
19 changes: 11 additions & 8 deletions webapp/src/component/MandatoryDataProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,26 @@ export const MandatoryDataProvider = (props: any) => {
function initPostHog() {
let postHogPromise: Promise<PostHog> | 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(), {
name: userData!.username,
email: userData!.username,
...getUtmParams(),
});
}
});
});
}
}
return () => {
postHogPromise?.then((ph) => ph.reset());
postHogPromise?.then((ph) => {
ph.reset();
});
window[POSTHOG_INITIALIZED_WINDOW_PROPERTY] = false;
};
}
Expand Down

0 comments on commit a29e99d

Please sign in to comment.