This repository was archived by the owner on Jul 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAnalytics.ts
More file actions
101 lines (82 loc) · 2.67 KB
/
Analytics.ts
File metadata and controls
101 lines (82 loc) · 2.67 KB
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
import axios from 'axios';
interface AnalyticsModel {
run: typeof run
}
var Analytics:AnalyticsModel = {
run: run
};
function run():any {
let saffronDomain:string = 'https://saffroncodesdk.com/api/Projects/versioncontrol';
let hostName:string = window.location.hostname;
// if on localhost, stop all actions.
if( hostName === 'localhost' ||
(hostName.indexOf('192.168') > -1) ) {
console.log('localhost: analytics stop');
return;
}
let interval:number;
let frequency:number = 200;
// set up interval and send api call on initialization
// setAnalyticInterval();
sendApiRequest();
document.addEventListener("visibilitychange", onVisibleChange);
function setAnalyticInterval():any {
let prevLocation:string = window.location.href;
let presentLocation:string = window.location.href;
interval = setInterval(() => {
presentLocation = window.location.href;
if( prevLocation === presentLocation ) {
return;
}
// call api and set prevLocation to presentLocation
sendApiRequest();
prevLocation = presentLocation;
}, frequency)
}
function sendApiRequest():any {
let body = {
AppId: hostName,
PageName: '',
Enter: false
};
let pathName: string = window.location.pathname;
let pathArray: string[] = pathName.split('/');
let pageName: string = pathArray[1];
if(pageName && (pageName.length > 0)) {
body = {
AppId: hostName,
PageName: pageName,
Enter: true
};
}
axios.post(saffronDomain, body)
.then(response => {
let parser = new DOMParser();
let xmlDoc = parser.parseFromString(response.data,"text/xml");
let id = xmlDoc.getElementsByTagName('id')[0].childNodes[0].nodeValue || '3001';
// if id is greater than 3000 the proccess is invalid
// and user should be redirected to saffron page
if(parseFloat(id) > 3000) {
let text = xmlDoc.getElementsByTagName('text')[0].childNodes[0].nodeValue;
let url = xmlDoc.getElementsByTagName('url_ios')[0].childNodes[0].nodeValue || '/';
alert(text);
window.location.replace(url);
}
});
}
function onVisibleChange () {
// if page is hidden to user (minimize or on another tab)
// clear interval and set interval again if page is visible
// to user and also send api request
let isHidden:boolean = document.hidden;
if(isHidden) {
// clearInterval(interval);
return;
}
// clearInterval(interval);
// setAnalyticInterval();
sendApiRequest();
// console.log(document.hidden, 'isHidden', document.visibilityState, 'state');
}
}
export default Analytics;