Skip to content

Commit

Permalink
Add referrer-tracker
Browse files Browse the repository at this point in the history
  • Loading branch information
ahaapple committed Dec 21, 2024
1 parent 8626049 commit bdd083d
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
2 changes: 2 additions & 0 deletions frontend/app/[locale]/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { TooltipProvider } from '@/components/ui/tooltip';
import { NextIntlClientProvider } from 'next-intl';
import { getMessages, getTranslations, unstable_setRequestLocale } from 'next-intl/server';
import { routing } from '@/i18n/routing';
import { ReferrerTracker } from '@/components/shared/referrer-tracker';

export function generateStaticParams() {
return routing.locales.map((locale) => ({ locale }));
Expand Down Expand Up @@ -96,6 +97,7 @@ export default async function RootLayout({ children, params: { locale } }: { chi
</TooltipProvider>
</SidebarProvider>
</ThemeProvider>
<ReferrerTracker />
<Script defer src="https://accounts.google.com/gsi/client" strategy="lazyOnload" />
<Script
defer
Expand Down
2 changes: 2 additions & 0 deletions frontend/components/layout/user-account-nav.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ export function UserAccountNav({ user }: UserAccountNavProps) {
setUser(user);
window.postMessage({ user: user }, '*');
try {
const referrer = localStorage.getItem('userReferrer');
(window as any).umami?.identify({
userId: user.email,
...(referrer ? { referrer } : {}),
});
} catch (error) {
console.error('Umami tracking failed:', error);
Expand Down
38 changes: 38 additions & 0 deletions frontend/components/shared/referrer-tracker.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
'use client';

import { useEffect } from 'react';

export const ReferrerTracker = () => {
useEffect(() => {
try {
const existingReferrer = localStorage.getItem('userReferrer');

if (!existingReferrer) {
const referrer = document.referrer;

const url = new URL(window.location.href);
const utmSource = url.searchParams.get('utm_source');
const utmMedium = url.searchParams.get('utm_medium');
const utmCampaign = url.searchParams.get('utm_campaign');
const urlRef = url.searchParams.get('ref');

if (referrer || utmSource || urlRef) {
const referrerData = {
referrer,
utmSource,
utmMedium,
utmCampaign,
urlRef,
firstVisit: new Date().toISOString(),
};
console.log('Referrer tracking:', referrerData);
localStorage.setItem('userReferrer', JSON.stringify(referrerData));
}
}
} catch (error) {
console.error('Referrer tracking failed:', error);
}
}, []);

return null;
};

0 comments on commit bdd083d

Please sign in to comment.