~repos /website
git clone https://pyrossh.dev/repos/website.git
木 Personal website of pyrossh. Built with astrojs, shiki, vite.
1e66cc69
—
pyrossh 1 year ago
fix analytics
- routes/stats.tsx +2 -2
- services/analytics.ts +36 -30
routes/stats.tsx
CHANGED
|
@@ -23,8 +23,8 @@ export default function Stats({ data }: PageProps<AnalyticsData>) {
|
|
|
23
23
|
<div class="charts no-select hidden">
|
|
24
24
|
<div class="headline">
|
|
25
25
|
<div class="metrics">
|
|
26
|
-
<Tile title="Visitors" value={data.vistors.toString()} />
|
|
26
|
+
<Tile title="Unique Visitors" value={data.vistors.toString()} />
|
|
27
|
-
<Tile title="Views" value={data.views.toString()} />
|
|
27
|
+
<Tile title="Page Views" value={data.views.toString()} />
|
|
28
28
|
</div>
|
|
29
29
|
</div>
|
|
30
30
|
</div>
|
services/analytics.ts
CHANGED
|
@@ -11,36 +11,42 @@ export const recordVisit = async (
|
|
|
11
11
|
pathname: string,
|
|
12
12
|
referer: string,
|
|
13
13
|
) => {
|
|
14
|
+
try {
|
|
14
|
-
|
|
15
|
+
const ipRes: { countryCode: string } = await ipInfoClient.lookupIp(
|
|
16
|
+
hostname,
|
|
17
|
+
);
|
|
15
|
-
|
|
18
|
+
const parser = new UAParser(userAgent);
|
|
16
|
-
|
|
19
|
+
const now = new Date();
|
|
17
|
-
|
|
20
|
+
const prefix = [
|
|
18
|
-
|
|
21
|
+
"analytics",
|
|
19
|
-
|
|
22
|
+
`${now.getFullYear()}-${now.getMonth() + 1}-${now.getDate()}`,
|
|
20
|
-
|
|
23
|
+
];
|
|
21
|
-
|
|
24
|
+
const visitor = await kv.get(["visitor", hostname]);
|
|
22
|
-
|
|
25
|
+
const keysToIncrement = [
|
|
23
|
-
|
|
26
|
+
visitor.value ? [] : ["visitor", hostname],
|
|
24
|
-
|
|
27
|
+
visitor.value ? [] : [...prefix, "visitors"],
|
|
25
|
-
|
|
28
|
+
[...prefix, "views"],
|
|
26
|
-
|
|
29
|
+
[...prefix, "pages", pathname],
|
|
27
|
-
|
|
30
|
+
[...prefix, "referers", referer],
|
|
28
|
-
|
|
31
|
+
[...prefix, "countries", ipRes.countryCode],
|
|
29
|
-
|
|
32
|
+
[...prefix, "os", parser.getOS().name],
|
|
30
|
-
|
|
33
|
+
[...prefix, "browsers", parser.getBrowser().name],
|
|
31
|
-
|
|
34
|
+
].filter((arr) => arr[arr.length - 1]);
|
|
32
|
-
|
|
35
|
+
await kv
|
|
33
|
-
|
|
36
|
+
.atomic()
|
|
34
|
-
|
|
37
|
+
.mutate(
|
|
35
|
-
|
|
38
|
+
...keysToIncrement.map(
|
|
36
|
-
|
|
39
|
+
(arr) => ({
|
|
37
|
-
|
|
40
|
+
type: "sum",
|
|
38
|
-
|
|
41
|
+
key: arr,
|
|
39
|
-
|
|
42
|
+
value: new Deno.KvU64(1n),
|
|
40
|
-
|
|
43
|
+
} as Deno.KvMutation),
|
|
41
|
-
|
|
44
|
+
),
|
|
42
|
-
|
|
45
|
+
)
|
|
43
|
-
|
|
46
|
+
.commit();
|
|
47
|
+
} catch (err) {
|
|
48
|
+
console.error("err could not record visit", err);
|
|
49
|
+
}
|
|
44
50
|
};
|
|
45
51
|
|
|
46
52
|
export interface AnalyticsDataPoint {
|