|
1 | | -// Taken from plausible.io/js/script.js |
2 | 1 | (function () { |
3 | | - const currentUrl = window.location; |
4 | | - const document = window.document; |
5 | | - |
6 | | - const apiUrl = '<api>' || 'https://plausible.io/api/event'; |
7 | | - const domain = '<domain>'; |
8 | | - |
9 | | - function logIgnoredEvent(event) { |
10 | | - console.warn('Ignoring Event: ' + event); |
11 | | - } |
12 | | - |
13 | | - function trackEvent(eventName, options) { |
14 | | - if ( |
15 | | - /^localhost$|^127(\.[0-9]+){0,2}\.[0-9]+$|^\[::1?\]$/.test(currentUrl.hostname) || |
16 | | - 'file:' === currentUrl.protocol |
17 | | - ) { |
18 | | - return logIgnoredEvent('localhost'); |
19 | | - } |
20 | | - |
21 | | - if ( |
22 | | - !(window._phantom || window.__nightmare || window.navigator.webdriver || window.Cypress) |
23 | | - ) { |
24 | | - try { |
25 | | - if (window.localStorage.plausible_ignore === 'true') { |
26 | | - return logIgnoredEvent('localStorage flag'); |
27 | | - } |
28 | | - } catch (error) { |
29 | | - // do nothing |
30 | | - } |
31 | | - |
32 | | - const eventData = {}; |
33 | | - eventData.name = eventName; |
34 | | - eventData.url = currentUrl.href; |
35 | | - eventData.domain = domain; |
36 | | - eventData.referrer = document.referrer || null; |
37 | | - eventData.width = window.innerWidth; |
38 | | - if (options && options.meta) { |
39 | | - eventData.meta = JSON.stringify(options.meta); |
40 | | - } |
41 | | - if (options && options.props) { |
42 | | - eventData.props = options.props; |
43 | | - } |
44 | | - |
45 | | - const xhr = new XMLHttpRequest(); |
46 | | - xhr.open('POST', apiUrl, true); |
47 | | - xhr.setRequestHeader('Content-Type', 'text/plain'); |
48 | | - xhr.send(JSON.stringify(eventData)); |
49 | | - xhr.onreadystatechange = function () { |
50 | | - if (xhr.readyState === 4 && options && options.callback) { |
51 | | - options.callback(); |
52 | | - } |
53 | | - }; |
54 | | - } |
55 | | - } |
56 | | - |
57 | | - const eventQueue = (window.plausible && window.plausible.q) || []; |
58 | | - window.plausible = trackEvent; |
59 | | - for (let i = 0; i < eventQueue.length; i++) { |
60 | | - trackEvent.apply(this, eventQueue[i]); |
61 | | - } |
62 | | - |
63 | | - let currentPagePath; |
64 | | - function trackPageView() { |
65 | | - if (currentPagePath !== currentUrl.pathname) { |
66 | | - currentPagePath = currentUrl.pathname; |
67 | | - trackEvent('pageview'); |
68 | | - } |
69 | | - } |
70 | | - |
71 | | - let originalPushState; |
72 | | - const history = window.history; |
73 | | - if (history.pushState) { |
74 | | - originalPushState = history.pushState; |
75 | | - history.pushState = function () { |
76 | | - originalPushState.apply(this, arguments); |
77 | | - trackPageView(); |
| 2 | + window.plausible = |
| 3 | + window.plausible || |
| 4 | + function () { |
| 5 | + (plausible.q = plausible.q || []).push(arguments); |
| 6 | + }; |
| 7 | + plausible.init = |
| 8 | + plausible.init || |
| 9 | + function (i) { |
| 10 | + plausible.o = i || {}; |
78 | 11 | }; |
79 | | - window.addEventListener('popstate', trackPageView); |
80 | | - } |
| 12 | + plausible.init(); |
81 | 13 |
|
82 | | - if (document.visibilityState === 'prerender') { |
83 | | - document.addEventListener('visibilitychange', function () { |
84 | | - if (!currentPagePath && document.visibilityState === 'visible') { |
85 | | - trackPageView(); |
86 | | - } |
87 | | - }); |
88 | | - } else { |
89 | | - trackPageView(); |
90 | | - } |
| 14 | + var s = document.createElement('script'); |
| 15 | + s.async = true; |
| 16 | + s.src = '<scriptSrc>'; |
| 17 | + document.head.appendChild(s); |
91 | 18 | })(); |
0 commit comments