~repos /website

#astro#js#html#css

git clone https://pyrossh.dev/repos/website.git

木 Personal website of pyrossh. Built with astrojs, shiki, vite.


1e66cc69 pyrossh

1 year ago
fix analytics
Files changed (2) hide show
  1. routes/stats.tsx +2 -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
- const ipRes: { countryCode: string } = await ipInfoClient.lookupIp(hostname);
15
+ const ipRes: { countryCode: string } = await ipInfoClient.lookupIp(
16
+ hostname,
17
+ );
15
- const parser = new UAParser(userAgent);
18
+ const parser = new UAParser(userAgent);
16
- const now = new Date();
19
+ const now = new Date();
17
- const prefix = [
20
+ const prefix = [
18
- "analytics",
21
+ "analytics",
19
- `${now.getFullYear()}-${now.getMonth() + 1}-${now.getDate()}`,
22
+ `${now.getFullYear()}-${now.getMonth() + 1}-${now.getDate()}`,
20
- ];
23
+ ];
21
- const newVisitor = await kv.get(["visitor", hostname]);
24
+ const visitor = await kv.get(["visitor", hostname]);
22
- const keysToIncrement = [
25
+ const keysToIncrement = [
23
- newVisitor.value ? [] : ["vistor", hostname],
26
+ visitor.value ? [] : ["visitor", hostname],
24
- [...prefix, "visitors"],
27
+ visitor.value ? [] : [...prefix, "visitors"],
25
- [...prefix, "views"],
28
+ [...prefix, "views"],
26
- [...prefix, "pages", pathname],
29
+ [...prefix, "pages", pathname],
27
- [...prefix, "referers", referer],
30
+ [...prefix, "referers", referer],
28
- [...prefix, "countries", ipRes.countryCode],
31
+ [...prefix, "countries", ipRes.countryCode],
29
- [...prefix, "os", parser.getOS().name],
32
+ [...prefix, "os", parser.getOS().name],
30
- [...prefix, "browsers", parser.getBrowser().name],
33
+ [...prefix, "browsers", parser.getBrowser().name],
31
- ].filter((arr) => arr.length === 0 || arr[arr.length - 1]);
34
+ ].filter((arr) => arr[arr.length - 1]);
32
- await kv
35
+ await kv
33
- .atomic()
36
+ .atomic()
34
- .mutate(
37
+ .mutate(
35
- ...keysToIncrement.map(
38
+ ...keysToIncrement.map(
36
- (arr) => ({
39
+ (arr) => ({
37
- type: "sum",
40
+ type: "sum",
38
- key: [...prefix, ...arr],
41
+ key: arr,
39
- value: new Deno.KvU64(1n),
42
+ value: new Deno.KvU64(1n),
40
- } as Deno.KvMutation),
43
+ } as Deno.KvMutation),
41
- ),
44
+ ),
42
- )
45
+ )
43
- .commit();
46
+ .commit();
47
+ } catch (err) {
48
+ console.error("err could not record visit", err);
49
+ }
44
50
  };
45
51
 
46
52
  export interface AnalyticsDataPoint {