Skip to content

Commit 41c5c06

Browse files
committed
Added MixPanel referral tracking; updated button in navbar.
1 parent 41563a3 commit 41c5c06

File tree

5 files changed

+117
-11
lines changed

5 files changed

+117
-11
lines changed

public/scripts/referral-tracking.js

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
(function() {
2+
// Function to get query parameter value by name
3+
function getQueryParam(param) {
4+
const urlParams = new URLSearchParams(window.location.search);
5+
return urlParams.get(param);
6+
}
7+
8+
// Function to get the initial referrer
9+
function getInitialReferrer() {
10+
return document.referrer;
11+
}
12+
13+
// Function to get the referring domain
14+
function getReferringDomain(referrer) {
15+
try {
16+
const url = new URL(referrer);
17+
return url.hostname;
18+
} catch (e) {
19+
return null;
20+
}
21+
}
22+
23+
// Store initial referrer and referring domain to avoid redundant calls
24+
const initialReferrer = getInitialReferrer();
25+
const referringDomain = getReferringDomain(initialReferrer);
26+
27+
// Function to check if the referrer or browser is Facebook
28+
function isFacebookSource() {
29+
const userAgent = navigator.userAgent.toLowerCase();
30+
return (
31+
(referringDomain && referringDomain.includes('facebook.com')) ||
32+
userAgent.includes('fb') ||
33+
userAgent.includes('facebook')
34+
);
35+
}
36+
37+
// Function to check if the referrer is Google
38+
function isGoogleSource() {
39+
return (
40+
referringDomain && referringDomain.includes('google.com')
41+
);
42+
}
43+
44+
// Function to check if the referrer is Bing
45+
function isBingSource() {
46+
return (
47+
referringDomain && referringDomain.includes('bing.com')
48+
);
49+
}
50+
51+
// Determine referral source
52+
let referralSource = 'Direct or Other';
53+
54+
const fbclid = getQueryParam('fbclid');
55+
const utmSource = getQueryParam('utm_source');
56+
57+
if (fbclid) {
58+
referralSource = 'Facebook';
59+
} else if (utmSource) {
60+
const normalisedUtmSource = utmSource.toLowerCase();
61+
if (normalisedUtmSource === 'chatgpt.com') {
62+
referralSource = 'ChatGPT';
63+
} else if (normalisedUtmSource === 'perplexity.ai') {
64+
referralSource = 'Perplexity';
65+
} else if (normalisedUtmSource === 'deepseek.com') {
66+
referralSource = 'DeepSeek';
67+
} else {
68+
referralSource = normalisedUtmSource;
69+
}
70+
} else if (isFacebookSource()) {
71+
referralSource = 'Facebook';
72+
} else if (isGoogleSource()) {
73+
referralSource = 'Google';
74+
} else if (isBingSource()) {
75+
referralSource = 'Bing';
76+
} // No need for an else here as 'Direct or Other' is already set by default
77+
78+
// Polling mechanism to ensure MixPanel is loaded before tracking
79+
var maxAttempts = 10;
80+
var attempts = 0;
81+
var interval = 100; // in milliseconds
82+
83+
function trackReferralSource() {
84+
attempts++;
85+
// Safely check if mixpanel is defined and ready
86+
if (window.mixpanelLoaded && typeof window.mixpanel !== 'undefined' && typeof window.mixpanel.track === 'function') {
87+
window.mixpanel.track('Referral Source Identified', {
88+
'Referral Source': referralSource
89+
});
90+
} else if (attempts < maxAttempts) {
91+
// Retry after a short delay
92+
setTimeout(trackReferralSource, interval);
93+
}
94+
}
95+
96+
// Start tracking referral source
97+
trackReferralSource();
98+
})();

src/app/page.jsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,11 @@ export default function Home() {
235235
__html: JSON.stringify(organizationSchema)
236236
}}
237237
/>
238+
<Script
239+
id="referral-tracking"
240+
src="/scripts/referral-tracking.js"
241+
strategy="afterInteractive"
242+
/>
238243
<Hero />
239244
<main>
240245
{/* Commented out logo cloud for now */}

src/components/MixpanelProvider.jsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export default function MixpanelProvider() {
1616
useEffect(() => {
1717
console.log('Initializing Mixpanel with Session Replay...');
1818
initMixpanel();
19+
// Flag is now set in initMixpanel
1920
}, []);
2021

2122
// Track page views whenever the pathname changes

src/components/navbar.jsx

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { motion } from 'framer-motion'
1010
import { Link } from './link'
1111
import { Logo } from './logo'
1212
import { PlusGrid, PlusGridItem, PlusGridRow } from './plus-grid'
13+
import { Button } from './button'
1314

1415
const links = [
1516
{ href: '/pricing', label: 'Pricing' },
@@ -26,10 +27,10 @@ function DesktopNav() {
2627
{label === 'Book a Demo' ? (
2728
<Link
2829
href={href}
29-
className="inline-flex items-center mx-2 text-sm font-semibold transition"
30+
className="flex items-center px-2 py-3"
3031
>
31-
<div className="rounded-full px-4 py-2.5 bg-black text-white hover:bg-gray-800 transition">
32-
<span className="relative top-px">{label}</span>
32+
<div className="rounded-full px-4 py-2 bg-black text-white hover:bg-gray-800 transition">
33+
<span className="text-sm font-medium">{label}</span>
3334
</div>
3435
</Link>
3536
) : (
@@ -73,14 +74,9 @@ function MobileNav() {
7374
key={href}
7475
>
7576
{label === 'Book a Demo' ? (
76-
<Link
77-
href={href}
78-
className="inline-flex items-center mx-2 text-sm font-semibold transition"
79-
>
80-
<div className="rounded-full px-4 py-1.5 bg-black text-white hover:bg-gray-800 transition">
81-
<span className="relative top-px">{label}</span>
82-
</div>
83-
</Link>
77+
<div className="mx-2">
78+
<Button href={href}>{label}</Button>
79+
</div>
8480
) : (
8581
<Link href={href} className="text-base font-medium text-gray-950">
8682
{label}

src/lib/mixpanelClient.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ export const initMixpanel = () => {
2424
record_idle_timeout_ms: 600000,
2525
record_min_ms: 3000,
2626
});
27+
28+
// Explicitly expose mixpanel instance globally
29+
window.mixpanel = mixpanel;
30+
31+
// Set flag for referral tracking script
32+
window.mixpanelLoaded = true;
2733
};
2834

2935
/**

0 commit comments

Comments
 (0)