Skip to content

Commit

Permalink
Revert "Use fetch over XMLHttpRequest in plausible.js (#5025)"
Browse files Browse the repository at this point in the history
This reverts commit de62f80.
  • Loading branch information
macobo authored Feb 4, 2025
1 parent b4ec09c commit d6da6b4
Show file tree
Hide file tree
Showing 14 changed files with 30 additions and 26 deletions.
1 change: 0 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ 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: 1 addition & 0 deletions lib/plausible_web/templates/layout/_tracking.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
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: 18 additions & 14 deletions tracker/src/plausible.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
{{/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 @@ -247,20 +248,8 @@
}

function sendRequest(endpoint, payload, options) {
{{#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) {
{{#if pageleave}}
if (allowFetch && window.fetch) {
fetch(endpoint, {
method: 'POST',
headers: {
Expand All @@ -271,8 +260,23 @@
}).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"></script>
<script defer data-exclude='/*#*/hash/**/ignored' src="/tracker/js/plausible.exclusions.hash.local.pageleave.js" data-allow-fetch></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"></script>
<script id="plausible-script" defer src="/tracker/js/plausible.hash.local.pageleave.pageview-props.js" data-allow-fetch></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"></script>
<script defer src="/tracker/js/plausible.hash.local.pageleave.js" data-allow-fetch></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"></script>
<script defer src="/tracker/js/plausible.local.manual.pageleave.js" data-allow-fetch></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"></script>
<script defer event-author="John" src="/tracker/js/plausible.local.pageleave.pageview-props.js" data-allow-fetch></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"></script>
<script defer src="/tracker/js/plausible.local.pageleave.js" data-allow-fetch></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"></script>
<script defer src="/tracker/js/plausible.local.pageleave.js" data-allow-fetch></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"></script>
<script defer src="/tracker/js/plausible.local.pageleave.js" data-allow-fetch></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"></script>
<script defer src="/tracker/js/plausible.hash.local.pageleave.js" data-allow-fetch></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"></script>
<script defer src="/tracker/js/plausible.local.pageleave.js" data-allow-fetch></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"></script>
<script defer src="/tracker/js/plausible.local.pageleave.js" data-allow-fetch></script>
<title>Document</title>
</head>
<body>
Expand Down

0 comments on commit d6da6b4

Please sign in to comment.