Skip to content

Commit 04a7432

Browse files
back to sendBeacon
1 parent a9fc7c9 commit 04a7432

1 file changed

Lines changed: 7 additions & 26 deletions

File tree

assets/src/js/script.js

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,49 +5,30 @@
55
*/
66

77
// Map variables to global identifiers so that minifier can mangle them to even shorter names
8-
var doc = document;
98
var win = window;
10-
var nav = navigator;
119
var ka = "koko_analytics";
1210

13-
function request(params) {
14-
if (!win[ka].urls.length) return;
15-
16-
var url = win[ka].urls[0];
17-
11+
function request(data) {
1812
// if window.koko_analytics.use_cookie is set, use that (for cookie consent plugins)
19-
var m = win[ka].use_cookie ? 'c' : win[ka].method[0];
13+
data['m'] = win[ka].use_cookie ? 'c' : win[ka].method[0];
2014

21-
url += (url.indexOf('?') > -1 ? '&' : '?');
22-
url += params;
23-
url += "&m=";
24-
url += m;
25-
26-
win.fetch(url, { method: 'POST', cache: 'no-store', priority: 'low' })
27-
.then(function(response) {
28-
// verify response: if failed, try next url from config array
29-
if ((!response.ok || response.headers.get('Content-Type').indexOf('text/plain') === -1)) {
30-
win[ka].urls.shift();
31-
request(params + "&disable-custom-endpoint=1");
32-
}
33-
});
15+
navigator.sendBeacon(win[ka].url, new URLSearchParams(data));
3416
}
3517

3618
win[ka].trackPageview = function(postId) {
3719
if (
3820
// do not track if this is a prerender request
39-
(doc.visibilityState == 'prerender') ||
21+
(document.visibilityState == 'prerender') ||
4022

4123
// do not track if user agent looks like a bot
42-
((/bot|crawl|spider|seo|lighthouse|facebookexternalhit|preview/i).test(nav.userAgent))
24+
((/bot|crawl|spider|seo|lighthouse|facebookexternalhit|preview/i).test(navigator.userAgent))
4325
) {
4426
return;
4527
}
4628

4729
// don't store referrer if from same-site
48-
var referrer = doc.referrer.indexOf(win[ka].site_url) == 0 ? '' : doc.referrer;
49-
50-
request("p="+postId+"&r="+encodeURIComponent(referrer));
30+
var referrer = document.referrer.indexOf(win[ka].site_url) == 0 ? '' : document.referrer;
31+
request({ p: postId, r: referrer })
5132
}
5233

5334
win.addEventListener('load', function() {

0 commit comments

Comments
 (0)