-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalytics.js
102 lines (88 loc) · 3.22 KB
/
analytics.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Production
window.analyticsURL = "https://script.google.com/macros/s/AKfycbwo2Sv5xIGyzSoYyB09YMmUVNhzvEyNImHTS2q-KAx4RpZ_5_2_2i3SOncObZxD9e9XnA/exec";
//HELPER FUNCTIONS
function getOS() {
var userAgent = window.navigator.userAgent,
platform = window.navigator?.userAgentData?.platform || window.navigator.platform,
macosPlatforms = ['Macintosh', 'MacIntel', 'MacPPC', 'Mac68K'],
windowsPlatforms = ['Win32', 'Win64', 'Windows', 'WinCE'],
iosPlatforms = ['iPhone', 'iPad', 'iPod'],
os = userAgent || platform;
if (macosPlatforms.indexOf(platform) !== -1) {
os = 'Mac OS';
} else if (iosPlatforms.indexOf(platform) !== -1) {
os = 'iOS';
} else if (windowsPlatforms.indexOf(platform) !== -1) {
os = 'Windows';
} else if (/Android/.test(userAgent)) {
os = 'Android';
} else if (/Linux/.test(platform)) {
os = 'Linux';
}
return os;
}
function getScreenSize() {
const width = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
const height = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
return `${width}x${height}`;
}
function isTouchCapable() {
return window.matchMedia("(pointer: coarse)").matches;
}
function isInstalled() {
// Detects if device is in standalone mode
var isInStandaloneMode = false;
if (matchMedia('(display-mode: standalone)').matches) { // replace standalone with fullscreen or minimal-ui according to your manifest
isInStandaloneMode = true; // Android and iOS 11.3+
} else if ('standalone' in navigator) {
isInStandaloneMode = navigator.standalone; // useful for iOS < 11.3
}
return isInStandaloneMode;
}
window.addEventListener("load", (event) => {
window.loadData = true;
});
window.addEventListener("visibilitychange", (event)=> {
logData('timing', 'visibilityChange');
submitLogData();
});
//Capture function
function logData(header, information, time = new Date().getTime()) {
if(!window.user) { return }
let analytics = localStorage.getItem(window.user._id+'analytics');
if(analytics == 'false') { return }
let id = localStorage.getItem('analyticID');
let dataLog = JSON.parse(localStorage.getItem('dataLog')) || [];
information = encodeURIComponent(information);
if(information.trim() == '') {information = 'blank';}
dataLog.push(encodeURI(`Timestamp=${time}&analyticId=${id}&${header}=${information}`));
localStorage.setItem('dataLog', JSON.stringify(dataLog));
return;
}
//SUBMIT LOCATION FORM AFTER PROCESSING CURRENT PAGE
async function submitLogData(){
let dataLog = JSON.parse(localStorage.getItem('dataLog')) || [];
if(dataLog == []) {return}
let queryString = dataLog.join('+');
//submit everything - we can do this in one go.
await fetch(window.analyticsURL+`?${queryString}`, {
method: "GET",
dataType: "json",
}).then(handleErrors)
.then(json)
.then(function(data){
// console.log(data);
localStorage.removeItem('dataLog'); //reset datalogs
}).catch(function(error){
console.log(error.message);
});
}
function handleErrors(response) {
if(!response.ok) {
throw new Error("Request failed " + response.statusText);
}
return response;
}
function json(response) {
return response.json();
}