Skip to content

Commit 9246e58

Browse files
committed
feat: add Plausible analytics tracking for code overlay interactions (#334)
# Add Plausible Analytics Event Tracking for Homepage Code Interactions This PR adds analytics event tracking for user interactions with the code overlay component on the homepage: - Added a global Plausible function definition in the site configuration to enable custom event tracking - Implemented tracking for three specific user interactions: - When users click the "Reveal Code" button (`home:reveal-code` event) - When users manually scroll through the code (`home:code-scroll` event, tracked only once per session) - Added logic to distinguish between programmatic scrolling (auto-scroll or scroll-to-top) and manual user scrolling - Added TypeScript declaration for the Plausible global function These analytics will help us better understand how users engage with the code examples on our homepage.
1 parent cd24b9a commit 9246e58

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

pkgs/website/astro.config.mjs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ export default defineConfig({
8686
src: PLAUSIBLE_PROXY.url + PLAUSIBLE_PROXY.scriptPath,
8787
},
8888
},
89+
{
90+
tag: 'script',
91+
content: `window.plausible = window.plausible || function() { (window.plausible.q = window.plausible.q || []).push(arguments) }`,
92+
},
8993
{
9094
tag: 'meta',
9195
attrs: {

pkgs/website/src/components/CodeOverlay.astro

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,9 +317,22 @@
317317
</style>
318318

319319
<script>
320+
// TypeScript declaration for Plausible
321+
declare global {
322+
interface Window {
323+
plausible?: (event: string, options?: { props?: Record<string, string | number> }) => void;
324+
}
325+
}
326+
320327
// Global flag to track if auto-scroll animation is running
321328
let isAutoScrolling = false;
322329

330+
// Flag to track if programmatic scroll is happening (auto-scroll or scroll-to-top)
331+
let isProgrammaticScroll = false;
332+
333+
// Flag to track if manual scroll event has been sent (only send once)
334+
let hasTrackedManualScroll = false;
335+
323336
const handleRevealButton = () => {
324337
const button = document.querySelector('.reveal-button') as HTMLElement | null;
325338
const overlay = document.querySelector('.code-overlay') as HTMLElement | null;
@@ -329,6 +342,11 @@
329342
if (!button || !overlay || !scrollableContainer || !scrollableInner) return;
330343

331344
button.addEventListener('click', async () => {
345+
// Track custom event in Plausible
346+
if (typeof window.plausible !== 'undefined') {
347+
window.plausible('home:reveal-code');
348+
}
349+
332350
const wait = (ms: number) => new Promise(resolve => setTimeout(resolve, ms));
333351

334352
// Helper to animate scroll with custom duration (cancellable on user interaction)
@@ -445,6 +463,14 @@
445463
return;
446464
}
447465

466+
// Track manual scroll event (only once, and only if not programmatic)
467+
if (!hasTrackedManualScroll && !isProgrammaticScroll) {
468+
hasTrackedManualScroll = true;
469+
if (typeof window.plausible !== 'undefined') {
470+
window.plausible('home:code-scroll');
471+
}
472+
}
473+
448474
if (scrollableInner.scrollTop > 50) {
449475
button.style.display = 'block';
450476
} else {
@@ -456,7 +482,14 @@
456482
scrollableInner.addEventListener('scroll', updateButtonVisibility);
457483

458484
button.addEventListener('click', () => {
485+
// Mark as programmatic scroll to avoid tracking
486+
isProgrammaticScroll = true;
459487
scrollableInner.scrollTo({ top: 0, behavior: 'smooth' });
488+
489+
// Reset flag after scroll completes
490+
setTimeout(() => {
491+
isProgrammaticScroll = false;
492+
}, 1000);
460493
});
461494
};
462495

@@ -480,6 +513,10 @@
480513
if (scrollTopButton) {
481514
scrollTopButton.style.display = 'none';
482515
}
516+
517+
// Note: hasTrackedManualScroll is NOT reset here
518+
// This means we track manual scroll only once per page session,
519+
// even if user reveals, resets, and reveals again
483520
});
484521
};
485522

0 commit comments

Comments
 (0)