0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-03-11 02:12:21 -05:00

Added reusable isPosthogLoaded function (#19722)

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
This commit is contained in:
Chris Raible 2024-02-20 14:08:35 -08:00 committed by GitHub
parent fbeecd58e6
commit f49b5ab78d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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();
}
}