-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
4 changed files
with
43 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@gitbook/integration-fathom': minor | ||
--- | ||
|
||
Add external link tracking to Fathom integration |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,31 @@ | ||
(function (doc, siteID) { | ||
(function (doc, siteId, trackExternalLinks) { | ||
const sibling = doc.getElementsByTagName('script')[0]; | ||
const element = doc.createElement('script'); | ||
element.defer = true; | ||
element.setAttribute('data-site', siteID); | ||
element.setAttribute('data-site', siteId); | ||
element.setAttribute('data-spa', 'auto'); | ||
element.src = 'https://cdn.usefathom.com/script.js'; | ||
|
||
// Function to track external links | ||
function trackExternalLink(event) { | ||
var item = event.target.closest('a'); | ||
if (!item) return; | ||
|
||
var linkUrl = new URL(item.getAttribute('href'), window.location.href); | ||
var currentHostname = window.location.hostname; | ||
|
||
if (linkUrl.hostname !== currentHostname) { | ||
// We simply track the full url as an event. | ||
window.fathom.trackEvent('External link clicked: ' + linkUrl.href); | ||
} | ||
} | ||
|
||
element.onload = function () { | ||
if (trackExternalLinks === true || trackExternalLinks === 'true') { | ||
// Use event delegation to capture clicks on all current and later loaded links | ||
doc.addEventListener('click', trackExternalLink); | ||
} | ||
}; | ||
|
||
sibling.parentNode.insertBefore(element, sibling); | ||
})(document, '<TO_REPLACE>'); | ||
})(document, '<TO_REPLACE_SITE_ID>', '<TO_REPLACE_TRACK_EXTERNAL_LINKS>'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters