Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed the amplitude user_id issue #1183

Merged
merged 1 commit into from
Sep 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
137 changes: 76 additions & 61 deletions src/contexts/TrackingContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,75 +5,90 @@ import { useRouter } from 'next/router';

const amplitudeApiKey = process.env.NEXT_PUBLIC_AMPLITUDE_ENV!;

const TrackingContext = createContext((eventName: string, additionalData: Record<string, any> = {}) => {});
const TrackingContext = createContext(
(eventName: string, additionalData: Record<string, any> = {}) => {}
);

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<string, any> = {}) => {
amplitude.track(eventName, {
timestamp: new Date(),
...eventProperties,
...additionalData,
});
};
);
}
}, [router.pathname, session, globalContext?.CurrentOrg]);
const trackEvent = (
eventName: string,
additionalData: Record<string, any> = {}
) => {
amplitude.track(eventName, {
timestamp: new Date(),
...eventProperties,
...additionalData,
});
};

return (
<TrackingContext.Provider value={trackEvent}>
{children}
</TrackingContext.Provider>
);
return (
<TrackingContext.Provider value={trackEvent}>
{children}
</TrackingContext.Provider>
);
};

export const useTracking = () => {
return useContext(TrackingContext);
return useContext(TrackingContext);
};