From f49b5ab78d6b9f8399bbaa4245b4460b5b2db05d Mon Sep 17 00:00:00 2001 From: Chris Raible Date: Tue, 20 Feb 2024 14:08:35 -0800 Subject: [PATCH] Added reusable isPosthogLoaded function (#19722) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit no issue - We were checking if Posthog was loaded in a few separate places using the same logic — this PR consolidates that logic into a simple utility function so we can ensure consistency and change this logic more easily if we need to in the future --- ghost/admin/app/utils/analytics.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/ghost/admin/app/utils/analytics.js b/ghost/admin/app/utils/analytics.js index c509ba9c1a..8abb9c8d46 100644 --- a/ghost/admin/app/utils/analytics.js +++ b/ghost/admin/app/utils/analytics.js @@ -1,5 +1,9 @@ // Wrapper function for Plausible event +function isPosthogLoaded() { + return window.posthog?.__loaded; +} + /** * Hashes a user's email address so we can use it as a distinct_id in PostHog without storing the email address itself * @@ -44,7 +48,7 @@ export function trackEvent(eventName, props = {}) { */ export async function identifyUser(user) { // Return early if window.posthog doesn't exist - if (!window.posthog?.__loaded) { + if (!isPosthogLoaded()) { return; } // User the user exists and has an email address, identify them in PostHog @@ -79,7 +83,7 @@ export async function identifyUser(user) { * @returns {void} */ export function resetUser() { - if (window.posthog?.__loaded) { + if (isPosthogLoaded()) { window.posthog.reset(); } } \ No newline at end of file