Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/layouts/Base.astro
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,35 @@ const jsonLd = {
{ threshold: 0.1 },
);
document.querySelectorAll('.reveal').forEach((el) => observer.observe(el));

// Besides page views, GoatCounter counts what a visitor came for: a PDF
// downloaded (CV, thesis), an email started, or a link followed out.
declare global {
interface Window {
goatcounter?: { count: (vars: { path: string; title?: string; event: boolean }) => void };
}
}
document.addEventListener('click', (event) => {
const link = (event.target as Element).closest('a');
if (!link || !window.goatcounter) return;
const url = new URL(link.href, location.href);
const path =
url.protocol === 'mailto:'
? 'email'
: url.pathname.endsWith('.pdf')
? `download ${url.pathname.split('/').pop()}`
: url.host !== location.host
? `out ${url.host}${url.pathname.replace(/\/$/, '')}`
: null;
if (path) window.goatcounter.count({ path, title: link.textContent?.trim(), event: true });
});
</script>
<!-- Visits, counted without cookies or personal data: no consent needed. -->
<script
is:inline
data-goatcounter="https://rememberyou.goatcounter.com/count"
async
src="https://gc.zgo.at/count.js"
></script>
</body>
</html>
30 changes: 30 additions & 0 deletions tests/site.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,36 @@ test('shows the name and the button in the navigation only once scrolled past',
await expect(button).toBeVisible();
});

test('counts the visit, and the CV, the thesis, an email and a link out', async ({ page }) => {
// A stand-in for GoatCounter's script, which records what it is given.
await page.route('https://gc.zgo.at/count.js', (route) =>
route.fulfill({
contentType: 'text/javascript',
body: `window.counted = [location.pathname];
window.goatcounter = { count: (vars) => window.counted.push(vars.path) };`,
}),
);
// Clicks are counted, not followed.
await page.route(/\.pdf$|linkedin\.com/, (route) => route.abort());
await page.goto('/');
await page.waitForFunction(() => 'goatcounter' in window);

await page.evaluate(() => document.addEventListener('click', (event) => event.preventDefault()));
await page.locator('#top a[download]').click();
await page.locator(`#experience a[href$="master-thesis.pdf"]`).click();
await page.locator('#contact a[href^="mailto:"]').click();
await page.locator('#contact a[href*="linkedin.com"]').click();
await page.locator('#top a[href="#contact"]').click();

expect(await page.evaluate(() => (window as unknown as { counted: string[] }).counted)).toEqual([
'/',
'download terencio-agozzino-cv.pdf',
'download terencio-agozzino-master-thesis.pdf',
'email',
'out www.linkedin.com/in/terencioagozzino',
]);
});

test.describe('without JavaScript', () => {
test.use({ javaScriptEnabled: false });

Expand Down
Loading