From e71a41bed2b043a7c7a0c1fcacabfbb5e68072ea Mon Sep 17 00:00:00 2001 From: Tim Pechersky Date: Tue, 31 Aug 2021 15:14:33 +0200 Subject: [PATCH] add files to previous commit --- .../providers/AnalyticsProvider/constants.js | 3 +- .../core/providers/AnalyticsProvider/index.js | 70 +++++++++++++++---- .../src/core/providers/UIProvider/index.js | 1 + 3 files changed, 60 insertions(+), 14 deletions(-) diff --git a/frontend/src/core/providers/AnalyticsProvider/constants.js b/frontend/src/core/providers/AnalyticsProvider/constants.js index e8b55c73..d8dc2a09 100644 --- a/frontend/src/core/providers/AnalyticsProvider/constants.js +++ b/frontend/src/core/providers/AnalyticsProvider/constants.js @@ -29,7 +29,8 @@ export const MIXPANEL_EVENTS = { BEACON: "beacon", ONBOARDING_COMPLETED: "Onbording complete", SESSIONS_COUNT: "Sessions Counter", - ONBOARDING_STEPS: "Onboarding Steps", + ONBOARDING_STEP: "Onboarding step", + ONBOARDING_STATE: "Onboarding state", TIMES_VISITED: "Page visit times", FORM_SUBMITTED: "form submitted", PAGEVIEW_DURATION: "Time spent on page", diff --git a/frontend/src/core/providers/AnalyticsProvider/index.js b/frontend/src/core/providers/AnalyticsProvider/index.js index daefb962..f05b2d85 100644 --- a/frontend/src/core/providers/AnalyticsProvider/index.js +++ b/frontend/src/core/providers/AnalyticsProvider/index.js @@ -11,26 +11,57 @@ const AnalyticsProvider = ({ children }) => { const { user, isInit } = useUser(); const [isMixpanelReady, setIsLoaded] = useState(false); const router = useRouter(); - const ui = useContext(UIContext); + + // ********** OBOARDING STATE ************** + useEffect(() => { + if (ui.onboardingState && isMixpanelReady) { + mixpanel.people.set(MIXPANEL_EVENTS.ONBOARDING_STATE, { + state: { ...ui.onboardingState }, + }); + } + }, [ui.onboardingState, isMixpanelReady]); + useEffect(() => { if (ui.isOnboardingComplete && isMixpanelReady && user) { mixpanel.people.set(MIXPANEL_EVENTS.ONBOARDING_COMPLETED, true); } }, [ui.isOnboardingComplete, isMixpanelReady, user]); - useEffect(() => { - if (!user && isInit && isMixpanelReady) { - mixpanel.time_event(MIXPANEL_EVENTS.CONVERT_TO_USER); - } - }, [user, isInit, isMixpanelReady]); + // ********** ONBOARDING STEP and TIMING ************** + const [previousOnboardingStep, setPreviousOnboardingStep] = useState(false); useEffect(() => { - if (ui.onboardingStep && isMixpanelReady) { - mixpanel.people.set(MIXPANEL_EVENTS.ONBOARDING_STEPS, ui.onboardingStep); + if (isMixpanelReady && router.nextRouter.pathname === "/welcome") { + if (!previousOnboardingStep) { + mixpanel.time_event(MIXPANEL_EVENTS.ONBOARDING_STEP); + setPreviousOnboardingStep(ui.onboardingStep); + } + if ( + previousOnboardingStep && + previousOnboardingStep !== ui.onboardingStep + ) { + mixpanel.track(MIXPANEL_EVENTS.ONBOARDING_STEP, { + step: previousOnboardingStep, + isBeforeUnload: false, + }); + setPreviousOnboardingStep(false); + } + } else if (previousOnboardingStep) { + mixpanel.track(MIXPANEL_EVENTS.ONBOARDING_STEP, { + step: previousOnboardingStep, + isBeforeUnload: false, + }); + setPreviousOnboardingStep(false); } - }, [ui.onboardingStep, isMixpanelReady]); + }, [ + previousOnboardingStep, + ui.onboardingStep, + isMixpanelReady, + router.nextRouter.pathname, + ]); + // ********** PING_PONG ************** useEffect(() => { let durationSeconds = 0; @@ -51,6 +82,8 @@ const AnalyticsProvider = ({ children }) => { return () => clearInterval(intervalId); }, [isMixpanelReady, router.nextRouter.pathname]); + // ********** TIME SPENT ON PATH************** + const [previousPathname, setPreviousPathname] = useState(false); useEffect(() => { @@ -69,6 +102,7 @@ const AnalyticsProvider = ({ children }) => { } }, [router.nextRouter.pathname, previousPathname, isMixpanelReady]); + // ********** PAGES VIEW ************** useEffect(() => { if (isMixpanelReady && ui.sessionId && router.nextRouter.pathname) { mixpanel.track(MIXPANEL_EVENTS.PAGEVIEW, { @@ -95,10 +129,7 @@ const AnalyticsProvider = ({ children }) => { }; }, [router.nextRouter.pathname, isMixpanelReady, ui.sessionId]); - useEffect(() => { - isMixpanelReady && mixpanel.register("sessionId", ui.sessionId); - }, [ui.sessionId, isMixpanelReady]); - + // ********** SESSION STATE ************** useEffect(() => { if (clientID) { try { @@ -115,6 +146,12 @@ const AnalyticsProvider = ({ children }) => { } }, [analytics, clientID]); + useEffect(() => { + isMixpanelReady && mixpanel.register("sessionId", ui.sessionId); + }, [ui.sessionId, isMixpanelReady]); + + // ********** USER STATE ************** + useEffect(() => { if (user) { try { @@ -152,6 +189,13 @@ const AnalyticsProvider = ({ children }) => { } }, [ui.isLoggingOut, isMixpanelReady]); + // ********** USER BOUNCE TIME ************** + useEffect(() => { + if (!user && isInit && isMixpanelReady) { + mixpanel.time_event(MIXPANEL_EVENTS.CONVERT_TO_USER); + } + }, [user, isInit, isMixpanelReady]); + return ( { setisOnboardingComplete, onboardingSteps, setOnboardingState, + onboardingState, isLoggingOut, }} >