From fbeecd58e65d140fcde2906514b2d4cb619a936f Mon Sep 17 00:00:00 2001 From: Chris Raible Date: Tue, 20 Feb 2024 13:57:42 -0800 Subject: [PATCH] Fixed issue with calling Posthog functions before it is loaded (#19721) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit refs PA-36 - Since Posthog is loaded outside of the main Admin app bundle, we need to check to make sure it exists before calling it. This way it will only run on Pro and not locally or on self-hosted instances - Previously we were checking that `window.posthog` existed, but there are some cases where `window.posthog` may exist, but the `posthog` object is not fully loaded yet. - This change fixes this by checking for `window.posthog.__loaded` instead, which is set to `true` once the `posthog` object is fully loaded — at this point, we should be able to call whatever functions we need to on `window.posthog` --- ghost/admin/app/utils/analytics.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ghost/admin/app/utils/analytics.js b/ghost/admin/app/utils/analytics.js index a8d5c310e6..c509ba9c1a 100644 --- a/ghost/admin/app/utils/analytics.js +++ b/ghost/admin/app/utils/analytics.js @@ -44,7 +44,7 @@ export function trackEvent(eventName, props = {}) { */ export async function identifyUser(user) { // Return early if window.posthog doesn't exist - if (!window.posthog) { + if (!window.posthog?.__loaded) { return; } // User the user exists and has an email address, identify them in PostHog @@ -79,7 +79,7 @@ export async function identifyUser(user) { * @returns {void} */ export function resetUser() { - if (window.posthog) { + if (window.posthog?.__loaded) { window.posthog.reset(); } } \ No newline at end of file