-
Notifications
You must be signed in to change notification settings - Fork 0
/
stat-hat-executor.js
212 lines (194 loc) · 5.15 KB
/
stat-hat-executor.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
function getSSP(name, pubConfigs) {
for (let pubConfig of pubConfigs) {
if (name.includes(pubConfig.searchName)) return pubConfig;
}
}
function getBrowserName() {
let userAgent = navigator?.userAgent;
if (userAgent.match(/firefox|fxios/i)) return "firefox";
if (userAgent.match(/chrome|chromium|crios/i)) return "chrome";
if (userAgent.match(/safari/i)) return "safari";
if (userAgent.match(/opr\//i)) return "opera";
if (userAgent.match(/edg/i)) return "edge";
return "others";
}
function getHostName() {
let hostName = window?.location?.hostname;
const replaceList = ['www.', 'https://', 'http://', '.com'];
for (let index = 0; index < replaceList.length; index++) {
hostName = hostName.replace(replaceList[index], '');
}
return hostName;
}
function getParameters() {
return [
{
key: "dns",
name: "DNS Lookup",
timeEndKey: "domainLookupEnd",
timeStartKey: "domainLookupStart"
},
{
key: "tcp",
name: "TCP Connection",
timeEndKey: "connectEnd",
timeStartKey: "connectStart"
},
{
key: "que_st",
name: "Queueing and stalled",
timeEndKey: "requestStart",
timeStartKey: "startTime"
},
{
key: "rs_wfs",
name: "Request Sent and Waiting For Server",
timeEndKey: "responseStart",
timeStartKey: "requestStart"
},
{
key: "cd",
name: "Content Download",
timeEndKey: "responseEnd",
timeStartKey: "responseStart"
},
{
key: "dur",
name: "Duration",
timeEndKey: "responseEnd",
timeStartKey: "startTime"
}
];
}
function getBuckets() {
return [
{
key: "0",
rangeStart: -1,
rangeEnd: 0
},
{
key: "0-10",
rangeStart: 0,
rangeEnd: 10
},
{
key: "10-20",
rangeStart: 10,
rangeEnd: 20
},
{
key: "20-30",
rangeStart: 20,
rangeEnd: 30
},
{
key: "30-40",
rangeStart: 30,
rangeEnd: 40
},
{
key: "40-50",
rangeStart: 40,
rangeEnd: 50
},
{
key: "50-100",
rangeStart: 50,
rangeEnd: 100
},
{
key: "100-200",
rangeStart: 100,
rangeEnd: 200
}
];
}
function getBucketKey(value) {
const limit = 3000;
const difference = 200;
// If value is above 3000
if (value > limit) return `${limit}_above`;
// Value between 0 to 200
const buckets = getBuckets();
for (let index = 0; index < buckets.length; index++) {
const bucket = buckets[index];
if (value > bucket.rangeStart) {
if (!bucket.hasOwnProperty("rangeEnd")) return bucket.key;
if (value <= bucket.rangeEnd) {
return bucket.key;
}
}
}
// Value between 200 to 3000
const isDivisible = value % difference === 0;
const rangeStart = difference * (isDivisible ? Math.floor(value / difference) - 1 : Math.floor(value / difference));
return `${rangeStart}-${rangeStart + difference}`;
}
function getPartners() {
return [{
key: "pm",
name: "Pubmatic",
searchName: "hbopenbid.pubmatic.com"
},
{
key: "tl",
name: "3 Lift",
searchName: "tlx.3lift.com"
},
{
key: "an",
name: "adnxs",
searchName: "ib.adnxs.com"
},
{
key: "rc",
name: "Rubicon",
searchName: "fastlane.rubiconproject.com"
},
{
key: "google",
name: "Google",
searchName: "securepubads.g.doubleclick.net"
}
];
}
function getTimeValue(endTime, startTime) {
if (isNaN(endTime) || isNaN(startTime)) return -1;
if (endTime === 0 || startTime === 0) return -1;
return endTime - startTime;
}
function statHatHatLogger(countryCode = "00") {
//("Country code:", countryCode);
let performanceResources = window.performance.getEntriesByType("resource");
//console.log("performanceResources: ", performanceResources);
const hostName = getHostName();
const browserName = getBrowserName();
for (let performanceResource of performanceResources) {
let pubConfig = getSSP(performanceResource.name, getPartners());
if (pubConfig) {
//console.log(pubConfig.name, performanceResource);
const stathatUserEmail = "[email protected]";
const url = "https://api.stathat.com/ez";
const parameters = getParameters();
for (let index = 0; index < parameters.length; index++) {
const parameter = parameters[index];
const value = getTimeValue(performanceResource[parameter.timeEndKey], performanceResource[parameter.timeStartKey]);
if (value < 0) {
continue;
}
const stathatKeyToUse = `${pubConfig.key}_${browserName}_${hostName}_${parameter.key}_${countryCode}_${getBucketKey(value)}`;
const data = "time=" + (new Date()).getTime() + "&stat=" + stathatKeyToUse + "&email=" + stathatUserEmail + "&count=1";
var statHatElement = document.createElement('script');
statHatElement.src = url + '?' + data;
statHatElement.async = true;
document.body.appendChild(statHatElement);
//console.log("StatHatElement: ", statHatElement);
}
}
};
};
//statHatHatLogger();
chrome.storage.local.get('countryCode', function (items) {
statHatHatLogger(items.countryCode);
});