Skip to content

Commit ca136a9

Browse files
authored
Add external link tracking to Fathom integration (#534)
* #450 add external link tracking to fathom integration * changed comment description * Remove extra quotes causing SyntaxError in Fathom script * Make replacement more readable * Removed space configuration * Track full url instead of domain name only * Improve external link tracking for dynamic content * changeset
1 parent b926de1 commit ca136a9

File tree

4 files changed

+43
-4
lines changed

4 files changed

+43
-4
lines changed

.changeset/witty-ligers-sparkle.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@gitbook/integration-fathom': minor
3+
---
4+
5+
Add external link tracking to Fathom integration

integrations/fathom/gitbook-manifest.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,9 @@ configurations:
4242
type: string
4343
title: Site ID
4444
description: Look for this in your Fathom analytics account
45+
track_external_links:
46+
type: boolean
47+
title: Track External Links
48+
description: Enable tracking of clicks on external links
4549
required:
4650
- site_id
Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,31 @@
1-
(function (doc, siteID) {
1+
(function (doc, siteId, trackExternalLinks) {
22
const sibling = doc.getElementsByTagName('script')[0];
33
const element = doc.createElement('script');
44
element.defer = true;
5-
element.setAttribute('data-site', siteID);
5+
element.setAttribute('data-site', siteId);
66
element.setAttribute('data-spa', 'auto');
77
element.src = 'https://cdn.usefathom.com/script.js';
8+
9+
// Function to track external links
10+
function trackExternalLink(event) {
11+
var item = event.target.closest('a');
12+
if (!item) return;
13+
14+
var linkUrl = new URL(item.getAttribute('href'), window.location.href);
15+
var currentHostname = window.location.hostname;
16+
17+
if (linkUrl.hostname !== currentHostname) {
18+
// We simply track the full url as an event.
19+
window.fathom.trackEvent('External link clicked: ' + linkUrl.href);
20+
}
21+
}
22+
23+
element.onload = function () {
24+
if (trackExternalLinks === true || trackExternalLinks === 'true') {
25+
// Use event delegation to capture clicks on all current and later loaded links
26+
doc.addEventListener('click', trackExternalLink);
27+
}
28+
};
29+
830
sibling.parentNode.insertBefore(element, sibling);
9-
})(document, '<TO_REPLACE>');
31+
})(document, '<TO_REPLACE_SITE_ID>', '<TO_REPLACE_TRACK_EXTERNAL_LINKS>');

integrations/fathom/src/index.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ type FathomRuntimeContext = RuntimeContext<
1212
{},
1313
{
1414
site_id?: string;
15+
track_external_links?: boolean;
1516
}
1617
>
1718
>;
@@ -26,7 +27,14 @@ export const handleFetchEvent: FetchPublishScriptEventCallback = async (
2627
return;
2728
}
2829

29-
return new Response(script.replace('<TO_REPLACE>', siteId), {
30+
const trackExternalLinks =
31+
environment.siteInstallation?.configuration?.track_external_links ?? false;
32+
33+
const updatedScript = script
34+
.replace('<TO_REPLACE_SITE_ID>', siteId)
35+
.replace('<TO_REPLACE_TRACK_EXTERNAL_LINKS>', trackExternalLinks.toString());
36+
37+
return new Response(updatedScript, {
3038
headers: {
3139
'Content-Type': 'application/javascript',
3240
'Cache-Control': 'max-age=604800',

0 commit comments

Comments
 (0)