|
5 | 5 | */ |
6 | 6 |
|
7 | 7 | // Map variables to global identifiers so that minifier can mangle them to even shorter names |
8 | | -var doc = document; |
9 | 8 | var win = window; |
10 | | -var nav = navigator; |
11 | 9 | var ka = "koko_analytics"; |
12 | 10 |
|
13 | | -function request(params) { |
14 | | - if (!win[ka].urls.length) return; |
15 | | - |
16 | | - var url = win[ka].urls[0]; |
17 | | - |
| 11 | +function request(data) { |
18 | 12 | // 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]; |
20 | 14 |
|
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)); |
34 | 16 | } |
35 | 17 |
|
36 | 18 | win[ka].trackPageview = function(postId) { |
37 | 19 | if ( |
38 | 20 | // do not track if this is a prerender request |
39 | | - (doc.visibilityState == 'prerender') || |
| 21 | + (document.visibilityState == 'prerender') || |
40 | 22 |
|
41 | 23 | // 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)) |
43 | 25 | ) { |
44 | 26 | return; |
45 | 27 | } |
46 | 28 |
|
47 | 29 | // 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 }) |
51 | 32 | } |
52 | 33 |
|
53 | 34 | win.addEventListener('load', function() { |
|
0 commit comments