Skip to content

Commit

Permalink
Use fetch over XMLHttpRequest in plausible.js (#5025)
Browse files Browse the repository at this point in the history
`fetch` allows us to use the keepalive flag, which more reliably
delivers data to us. It's also required for reliable scroll depth
capturing down the line.

The only major browser not to support fetch is Internet Explorer.
Clients who care about IE traffic can use the `.compat` variant of our
script (documented in
https://plausible.io/docs/script-extensions#scriptcompatjs). As of this
commit, IE makes up less than ~0.003% of incoming traffic to plausible.
  • Loading branch information
macobo authored Feb 3, 2025
1 parent 62c6cb5 commit de62f80
Show file tree
Hide file tree
Showing 14 changed files with 26 additions and 30 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ All notable changes to this project will be documented in this file.
- Filters appear in the search bar as ?f=is,page,/docs,/blog&f=... instead of ?filters=((is,page,(/docs,/blog)),...) for Plausible links sent on various platforms to work reliably.
- Details modal search inputs are now case-insensitive.
- Improved report performance in cases where site has a lot of unique pathnames
- Plausible script now uses `fetch` with keepalive flag as default over `XMLHttpRequest`. This will ensure more reliable tracking. Reminder to use `compat` script variant if tracking Internet Explorer is required.

### Fixed

Expand Down
1 change: 0 additions & 1 deletion lib/plausible_web/templates/layout/_tracking.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
data-api={PlausibleWeb.Dogfood.api_destination()}
data-domain={PlausibleWeb.Dogfood.domain(@conn)}
src={PlausibleWeb.Dogfood.script_url()}
data-allow-fetch
>
</script>
<script>
Expand Down
32 changes: 14 additions & 18 deletions tracker/src/plausible.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
{{/if}}
var endpoint = scriptEl.getAttribute('data-api') || defaultEndpoint(scriptEl)
var dataDomain = scriptEl.getAttribute('data-domain')
var allowFetch = scriptEl.hasAttribute('data-allow-fetch')

function onIgnoredEvent(eventName, reason, options) {
if (reason) console.warn('Ignoring Event: ' + reason);
Expand Down Expand Up @@ -248,8 +247,20 @@
}

function sendRequest(endpoint, payload, options) {
{{#if pageleave}}
if (allowFetch && window.fetch) {
{{#if compat}}
var request = new XMLHttpRequest();
request.open('POST', endpoint, true);
request.setRequestHeader('Content-Type', 'text/plain');

request.send(JSON.stringify(payload));

request.onreadystatechange = function() {
if (request.readyState === 4) {
options && options.callback && options.callback({status: request.status})
}
}
{{else}}
if (window.fetch) {
fetch(endpoint, {
method: 'POST',
headers: {
Expand All @@ -260,23 +271,8 @@
}).then(function(response) {
options && options.callback && options.callback({status: response.status})
})

return
}
{{/if}}

var request = new XMLHttpRequest();
request.open('POST', endpoint, true);
request.setRequestHeader('Content-Type', 'text/plain');

request.send(JSON.stringify(payload));

request.onreadystatechange = function() {
if (request.readyState === 4) {
options && options.callback && options.callback({status: request.status})
}
}

}

var queue = (window.plausible && window.plausible.q) || []
Expand Down
2 changes: 1 addition & 1 deletion tracker/test/fixtures/pageleave-hash-exclusions.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Plausible Playwright tests</title>
<script defer data-exclude='/*#*/hash/**/ignored' src="/tracker/js/plausible.exclusions.hash.local.pageleave.js" data-allow-fetch></script>
<script defer data-exclude='/*#*/hash/**/ignored' src="/tracker/js/plausible.exclusions.hash.local.pageleave.js"></script>
</head>

<body>
Expand Down
2 changes: 1 addition & 1 deletion tracker/test/fixtures/pageleave-hash-pageview-props.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Plausible Playwright tests</title>
<script id="plausible-script" defer src="/tracker/js/plausible.hash.local.pageleave.pageview-props.js" data-allow-fetch></script>
<script id="plausible-script" defer src="/tracker/js/plausible.hash.local.pageleave.pageview-props.js"></script>
</head>

<body>
Expand Down
2 changes: 1 addition & 1 deletion tracker/test/fixtures/pageleave-hash.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Plausible Playwright tests</title>
<script defer src="/tracker/js/plausible.hash.local.pageleave.js" data-allow-fetch></script>
<script defer src="/tracker/js/plausible.hash.local.pageleave.js"></script>
<script>

</script>
Expand Down
2 changes: 1 addition & 1 deletion tracker/test/fixtures/pageleave-manual.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Plausible Playwright tests</title>
<script defer src="/tracker/js/plausible.local.manual.pageleave.js" data-allow-fetch></script>
<script defer src="/tracker/js/plausible.local.manual.pageleave.js"></script>
<script>window.plausible = window.plausible || function() { (window.plausible.q = window.plausible.q || []).push(arguments) }</script>
</head>

Expand Down
2 changes: 1 addition & 1 deletion tracker/test/fixtures/pageleave-pageview-props.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Plausible Playwright tests</title>
<script defer event-author="John" src="/tracker/js/plausible.local.pageleave.pageview-props.js" data-allow-fetch></script>
<script defer event-author="John" src="/tracker/js/plausible.local.pageleave.pageview-props.js"></script>
</head>

<body>
Expand Down
2 changes: 1 addition & 1 deletion tracker/test/fixtures/pageleave.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Plausible Playwright tests</title>
<script defer src="/tracker/js/plausible.local.pageleave.js" data-allow-fetch></script>
<script defer src="/tracker/js/plausible.local.pageleave.js"></script>
</head>

<body>
Expand Down
2 changes: 1 addition & 1 deletion tracker/test/fixtures/scroll-depth-content-onscroll.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Plausible Playwright tests</title>
<script defer src="/tracker/js/plausible.local.pageleave.js" data-allow-fetch></script>
<script defer src="/tracker/js/plausible.local.pageleave.js"></script>
</head>

<body>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script defer src="/tracker/js/plausible.local.pageleave.js" data-allow-fetch></script>
<script defer src="/tracker/js/plausible.local.pageleave.js"></script>
</head>
<body>
<br>
Expand Down
2 changes: 1 addition & 1 deletion tracker/test/fixtures/scroll-depth-hash.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Plausible Playwright tests</title>
<script defer src="/tracker/js/plausible.hash.local.pageleave.js" data-allow-fetch></script>
<script defer src="/tracker/js/plausible.hash.local.pageleave.js"></script>
</head>

<body>
Expand Down
2 changes: 1 addition & 1 deletion tracker/test/fixtures/scroll-depth-slow-window-load.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script defer src="/tracker/js/plausible.local.pageleave.js" data-allow-fetch></script>
<script defer src="/tracker/js/plausible.local.pageleave.js"></script>
</head>
<body>
<a id="navigate-away" href="/manual.html">
Expand Down
2 changes: 1 addition & 1 deletion tracker/test/fixtures/scroll-depth.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script defer src="/tracker/js/plausible.local.pageleave.js" data-allow-fetch></script>
<script defer src="/tracker/js/plausible.local.pageleave.js"></script>
<title>Document</title>
</head>
<body>
Expand Down

0 comments on commit de62f80

Please sign in to comment.