From 762fdabe2bb58936e61a2d57a37fb1b8638f5628 Mon Sep 17 00:00:00 2001 From: himanshudube97 Date: Fri, 13 Sep 2024 10:42:31 +0530 Subject: [PATCH] fixed the amplitude wala thing --- src/contexts/TrackingContext.tsx | 137 +++++++++++++++++-------------- 1 file changed, 76 insertions(+), 61 deletions(-) diff --git a/src/contexts/TrackingContext.tsx b/src/contexts/TrackingContext.tsx index 4dc94268..773e2d90 100644 --- a/src/contexts/TrackingContext.tsx +++ b/src/contexts/TrackingContext.tsx @@ -5,75 +5,90 @@ import { useRouter } from 'next/router'; const amplitudeApiKey = process.env.NEXT_PUBLIC_AMPLITUDE_ENV!; -const TrackingContext = createContext((eventName: string, additionalData: Record = {}) => {}); +const TrackingContext = createContext( + (eventName: string, additionalData: Record = {}) => {} +); -export const TrackingProvider = ({session, children }: any) => { - const router = useRouter(); - const globalContext = useContext(GlobalContext); - const [eventProperties, setEventProperties] = useState({}); +export const TrackingProvider = ({ session, children }: any) => { + const router = useRouter(); + const globalContext = useContext(GlobalContext); + const [eventProperties, setEventProperties] = useState({}); - useEffect(() => { - amplitude.init(amplitudeApiKey, { - defaultTracking: { - pageViews: false, - sessions: true, - attribution: true, - formInteractions: true, - }, - }); - }, []); + useEffect(() => { + amplitude.init(amplitudeApiKey, { + defaultTracking: { + pageViews: false, + sessions: true, + attribution: true, + formInteractions: true, + }, + }); + if (session?.user?.email) { + const userEmail = session?.user.email; + const identifyEvent = new amplitude.Identify(); + amplitude.setUserId(session?.user.email); + identifyEvent.set('User_id', session.user?.email); + amplitude.setUserId(userEmail); + amplitude.identify(identifyEvent); + } + }, [session?.user?.email]); - useEffect(() => { - if (session) { - const userEmail = session.user?.email; - amplitude.setUserId(userEmail); - } - - if (globalContext?.CurrentOrg) { - const identifyEvent = new amplitude.Identify(); - identifyEvent.set('User_orgs', globalContext.CurrentOrg.state.name); - amplitude.identify(identifyEvent); + useEffect(() => { + if (globalContext?.CurrentOrg) { + const identifyEvent = new amplitude.Identify(); + identifyEvent.set('User_orgs', globalContext.CurrentOrg.state.name); + amplitude.identify(identifyEvent); - setEventProperties({ - userCurrentOrg: globalContext.CurrentOrg.state.name, - userEmail: session?.user?.email, - page_domain: window.location.hostname, - page_location: window.location.href, - page_path: window.location.pathname + window.location.search, - page_title: document.title, - page_url: window.location.href, - referrer: document.referrer, - referring_domain: document.referrer ? new URL(document.referrer).hostname : '', - }); + setEventProperties({ + userCurrentOrg: globalContext.CurrentOrg.state.name, + userEmail: session?.user?.email, + page_domain: window.location.hostname, + page_location: window.location.href, + page_path: window.location.pathname + window.location.search, + page_title: document.title, + page_url: window.location.href, + referrer: document.referrer, + referring_domain: document.referrer + ? new URL(document.referrer).hostname + : '', + }); - amplitude.logEvent(`[${router.pathname}${window.location.search}] Page Viewed`, { - userCurrentOrg: globalContext.CurrentOrg.state.name, - userEmail: session?.user?.email, - page_domain: window.location.hostname, - page_location: window.location.href, - page_path: window.location.pathname + window.location.search, - page_title: document.title, - page_url: window.location.href, - referrer: document.referrer, - referring_domain: document.referrer ? new URL(document.referrer).hostname : '', - }); + amplitude.logEvent( + `[${router.pathname}${window.location.search}] Page Viewed`, + { + userCurrentOrg: globalContext.CurrentOrg.state.name, + userEmail: session?.user?.email, + page_domain: window.location.hostname, + page_location: window.location.href, + page_path: window.location.pathname + window.location.search, + page_title: document.title, + page_url: window.location.href, + referrer: document.referrer, + referring_domain: document.referrer + ? new URL(document.referrer).hostname + : '', } - }, [router.pathname, session, globalContext?.CurrentOrg]); - const trackEvent = (eventName: string, additionalData: Record = {}) => { - amplitude.track(eventName, { - timestamp: new Date(), - ...eventProperties, - ...additionalData, - }); - }; + ); + } + }, [router.pathname, session, globalContext?.CurrentOrg]); + const trackEvent = ( + eventName: string, + additionalData: Record = {} + ) => { + amplitude.track(eventName, { + timestamp: new Date(), + ...eventProperties, + ...additionalData, + }); + }; - return ( - - {children} - - ); + return ( + + {children} + + ); }; export const useTracking = () => { - return useContext(TrackingContext); + return useContext(TrackingContext); };