-
Notifications
You must be signed in to change notification settings - Fork 9
/
loader.js
62 lines (54 loc) · 1.92 KB
/
loader.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import {sendBeacon} from './polyfill.js';
import {iframeAnnounce} from './lib/announce.js';
// defer extra scripts until after 'load' event
window.addEventListener('load', (ev) => {
iframeAnnounce('load');
window.requestAnimationFrame(() => {
const scriptPWACompat = document.createElement('script');
scriptPWACompat.src = 'https://unpkg.com/pwacompat';
scriptPWACompat.setAttribute('crossorigin', 'anonymous');
document.head.appendChild(scriptPWACompat);
// nb. the outerHTML trick doesn't seem to work
if (!document.currentScript) {
// absence indicates we're in module mode, so load extended code
const scriptExtended = document.createElement('script');
scriptExtended.src = document.body.getAttribute('data-ext');
scriptExtended.type = 'module';
document.head.appendChild(scriptExtended);
}
if (window._dev) {
return; // don't analytics in dev
}
const scriptAnalytics = document.createElement('script');
scriptAnalytics.src = 'https://www.google-analytics.com/analytics.js';
document.head.appendChild(scriptAnalytics);
});
});
// create local GA, this matches the 'isogram' code
const tempGA = function() {
tempGA['q'].push(arguments);
};
tempGA['q'] = [];
tempGA['l'] = 1 * new Date;
window['ga'] = tempGA;
window['GoogleAnalyticsObject'] = 'ga';
window.addEventListener('message', (ev) => {
const type = ev.data.type || '';
switch (type) {
case 'focus':
// used by extension to trigger focus
// Chrome disallows autofocus, but programatic works (as of 75): I don't think it'll last.
typer.focus();
break;
case 'ga':
// used by extension to log load time
ga.apply(null, ev.data.payload);
break;
default:
console.debug('unhandled message', ev.data);
}
});
// enqueue initial GA events
sendBeacon || ga('set', 'transport', 'beacon');
ga('create', 'UA-39885839-6', 'auto');
ga('send', 'pageview');