| 1 |
(function () { |
| 2 |
if ('undefined' === typeof ajax_tptn_tracker) { |
| 3 |
return; |
| 4 |
} |
| 5 |
|
| 6 |
var tracker = ajax_tptn_tracker; |
| 7 |
var tracked = false; |
| 8 |
|
| 9 |
function send(id, context) { |
| 10 |
var params = new URLSearchParams({ |
| 11 |
action: 'tptn_tracker', |
| 12 |
top_ten_id: id, |
| 13 |
top_ten_blog_id: tracker.top_ten_blog_id, |
| 14 |
activate_counter: tracker.activate_counter, |
| 15 |
top_ten_debug: tracker.top_ten_debug |
| 16 |
}); |
| 17 |
|
| 18 |
if (context) { |
| 19 |
params.append('top_ten_sitewide_context', context); |
| 20 |
} |
| 21 |
|
| 22 |
var url = tracker.ajax_url; |
| 23 |
if ('query_based' === tracker.tracker_type) { |
| 24 |
url += (url.includes('?') ? '&' : '?') + params; |
| 25 |
} |
| 26 |
|
| 27 |
// Keep URLSearchParams as the beacon body so WordPress parses the payload as form data. |
| 28 |
if ('1' === tracker.top_ten_debug || !navigator.sendBeacon || !navigator.sendBeacon(url, params)) { |
| 29 |
fetch(url, { method: 'POST', body: params, keepalive: true }).catch(function () {}); |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
function track() { |
| 34 |
if (tracked) { |
| 35 |
return; |
| 36 |
} |
| 37 |
if (document.prerendering) { |
| 38 |
document.addEventListener('prerenderingchange', track, { once: true }); |
| 39 |
return; |
| 40 |
} |
| 41 |
if ('hidden' === document.visibilityState) { |
| 42 |
document.addEventListener('visibilitychange', track, { once: true }); |
| 43 |
return; |
| 44 |
} |
| 45 |
|
| 46 |
tracked = true; |
| 47 |
if (tracker.top_ten_id > 0) { |
| 48 |
send(tracker.top_ten_id, ''); |
| 49 |
} |
| 50 |
if (tracker.top_ten_sitewide_context) { |
| 51 |
send(0, tracker.top_ten_sitewide_context); |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
if ('loading' === document.readyState) { |
| 56 |
document.addEventListener('DOMContentLoaded', track); |
| 57 |
} else { |
| 58 |
track(); |
| 59 |
} |
| 60 |
|
| 61 |
window.addEventListener('pageshow', function (event) { |
| 62 |
if (event.persisted) { |
| 63 |
tracked = false; |
| 64 |
track(); |
| 65 |
} |
| 66 |
}); |
| 67 |
})(); |
| 68 |
|