Skip to content

Commit 758952f

Browse files
Merge pull request #50 from Couchbase-Ecosystem/add-scarf-analytics
Add Scarf analytics for documentation page view tracking
2 parents 5961b8e + 71a49d5 commit 758952f

2 files changed

Lines changed: 28 additions & 0 deletions

File tree

docusaurus.config.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ const config = {
2424
onBrokenLinks: 'warn',
2525
onBrokenMarkdownLinks: 'warn',
2626

27+
clientModules: [
28+
'./src/clientModules/scarfAnalytics.js',
29+
],
30+
2731
// Even if you don't use internationalization, you can use this field to set
2832
// useful metadata like html lang. For example, if your site is Chinese, you
2933
// may want to replace "en" with "zh-Hans".
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Scarf pixel – SPA-aware tracking for the Docusaurus documentation site.
2+
// This client module fires on the initial page load and on every subsequent
3+
// in-app navigation so that each page view is recorded, even in a SPA where
4+
// the full page is never reloaded.
5+
6+
const PIXEL_ID = '5ff443aa-dd19-43ab-9c9e-ad26252d0fb0';
7+
8+
let lastHref = null;
9+
10+
function sendScarfPing() {
11+
const currentHref = window.location.href;
12+
if (currentHref === lastHref) return; // dedup: skip if same page
13+
lastHref = currentHref;
14+
15+
const img = new Image();
16+
img.referrerPolicy = 'no-referrer-when-downgrade';
17+
img.src = `https://static.scarf.sh/a.png?x-pxid=${PIXEL_ID}`;
18+
}
19+
20+
// Docusaurus client-module lifecycle hook – called after every route update
21+
// (initial load + every SPA navigation).
22+
export function onRouteDidUpdate() {
23+
sendScarfPing();
24+
}

0 commit comments

Comments
 (0)