-
Notifications
You must be signed in to change notification settings - Fork 2
Add Scarf analytics for documentation page view tracking #50
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,24 @@ | ||||||||||||||||||
| // Scarf pixel – SPA-aware tracking for the Docusaurus documentation site. | ||||||||||||||||||
| // This client module fires on the initial page load and on every subsequent | ||||||||||||||||||
| // in-app navigation so that each page view is recorded, even in a SPA where | ||||||||||||||||||
| // the full page is never reloaded. | ||||||||||||||||||
|
|
||||||||||||||||||
| const PIXEL_ID = '5ff443aa-dd19-43ab-9c9e-ad26252d0fb0'; | ||||||||||||||||||
|
|
||||||||||||||||||
| let lastHref = null; | ||||||||||||||||||
|
|
||||||||||||||||||
| function sendScarfPing() { | ||||||||||||||||||
| const currentHref = window.location.href; | ||||||||||||||||||
| if (currentHref === lastHref) return; // dedup: skip if same page | ||||||||||||||||||
| lastHref = currentHref; | ||||||||||||||||||
|
|
||||||||||||||||||
| const img = new Image(); | ||||||||||||||||||
| img.referrerPolicy = 'no-referrer-when-downgrade'; | ||||||||||||||||||
| img.src = `https://static.scarf.sh/a.png?x-pxid=${PIXEL_ID}`; | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| // Docusaurus client-module lifecycle hook – called after every route update | ||||||||||||||||||
| // (initial load + every SPA navigation). | ||||||||||||||||||
| export function onRouteDidUpdate() { | ||||||||||||||||||
| sendScarfPing(); | ||||||||||||||||||
| } | ||||||||||||||||||
|
Comment on lines
+22
to
+24
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's good practice to only enable analytics in the production environment to avoid sending tracking events during local development. You should also check if the
Suggested change
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hardcoding the
PIXEL_IDcan make it difficult to manage for different environments (e.g., development, staging, production) or if it needs to be updated. It's better to use an environment variable. Docusaurus supports this by prefixing environment variables withDOCUSAURUS_. You will need to setDOCUSAURUS_SCARF_PIXEL_IDin your build environment.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The pixel ID is not a secret — it's sent to the browser as a visible query parameter on every page view. There's also only one environment (production). Hardcoding keeps it simple and avoids a silent failure if the env var is missing.