-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtracker.js
161 lines (139 loc) · 3.83 KB
/
tracker.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
function init() {
const endpoint =
process.env.ENDPOINT ?? "http://localhost:4003/api/analytics";
const hook = (_this, method, before, after) => {
const orig = _this[method];
return (...args) => {
before?.apply(null, args);
const res = orig.apply(_this, args);
after?.apply(null, args);
return res;
};
};
const { screen, navigator, location, document, history } = window;
const { wid } = document.currentScript.dataset;
let sessionId;
let enterTimestamp;
let prevPathname;
const pageViewsData = new Map();
const eventData = new Map();
const positionData = new Set();
//init visible refresh
function handleEnter() {
enterTimestamp = Date.now();
prevPathname = location.pathname;
const body = {
wid,
sessionId,
};
const headers = {
"Content-Type": "application/json",
};
window
.fetch(endpoint + "/enter", {
method: "POST",
body: JSON.stringify(body),
headers,
})
.then((res) =>
res.json().then((data) => {
sessionId = data.sessionId;
})
);
}
// hidden refresh quit
function handleLeave() {
updatePageViewData();
const pageViewsDataToObj = Object.fromEntries(pageViewsData);
// todo: event tracker
// const eventDataToObj = eventData.size
// ? Object.fromEntries(eventData)
// : null;
// const positionDataToObj = positionData.size
// ? Array.from(positionData)
// : null;
pageViewsData.clear();
// eventData.clear();
// positionData.clear();
const body = {
pageViewsData: pageViewsDataToObj,
sessionId,
wid,
screen: `${screen.width}x${screen.height}`,
language: navigator.language,
referrer: document.referrer,
};
const headers = {
type: "application/json",
};
const blob = new Blob([JSON.stringify(body)], headers);
navigator.sendBeacon(endpoint + "/leave", blob);
}
// 进入页面时触发 "enter"
document.addEventListener("readystatechange", () => {
if (document.readyState === "complete") {
handleEnter();
}
});
// 页面切换,关闭时触发 "leave"
document.addEventListener("visibilitychange", function logData() {
if (document.visibilityState === "hidden") {
handleLeave();
}
if (document.visibilityState === "visible") {
handleEnter();
}
});
// 命令式路由切换时触发 "leave"
history.pushState = hook(history, "pushState", handleBefore, handleAfter);
history.replaceState = hook(
history,
"replaceState",
handleBefore,
handleAfter
);
function updatePageViewData() {
if (!prevPathname) {
prevPathname = location.pathname;
}
if (!enterTimestamp) {
enterTimestamp = Date.now();
}
const dt = Date.now() - enterTimestamp;
pageViewsData.set(prevPathname, {
count: (pageViewsData.get(prevPathname)?.count ?? 0) + 1,
duration: (pageViewsData.get(prevPathname)?.duration ?? 0) + dt,
});
}
// function update
function handleBefore() {}
function handleAfter() {
if (prevPathname !== location.pathname) {
updatePageViewData();
enterTimestamp = Date.now();
prevPathname = location.pathname;
}
}
// 鼠标点击浏览器前进后退时触发
window.addEventListener("popstate", () => {
updatePageViewData();
prevPathname = location.pathname;
enterTimestamp = Date.now();
});
window.addEventListener("click", (e) => {
if (!e.target || !e.target?.dataset) return;
positionData.add({
x: e.clientX,
y: e.clientY,
});
for (const keyname in e.target.dataset) {
if (keyname.startsWith("analytics")) {
const key = keyname.slice(9);
eventData.set(key, {
count: (eventData.get(key)?.count ?? 0) + 1,
});
}
}
});
}
init();