1
+
2
+ import { authOptions } from "@/app/api/auth/[...nextauth]/route" ;
3
+ import Chart from "@/components/Chart" ;
4
+ import SectionBox from "@/components/layout/SectionBox" ;
5
+ import { Event } from "@/models/Event" ;
6
+ import { Page } from "@/models/Page" ;
7
+
8
+ import { differenceInDays , formatISO9075 , isToday } from "date-fns" ;
9
+ import mongoose from "mongoose" ;
10
+ import { getServerSession } from "next-auth" ;
11
+ import { redirect } from "next/navigation" ;
12
+ import { FaLink } from "react-icons/fa" ;
13
+
14
+ // import {CartesianGrid, Legend, Line, LineChart, Tooltip, XAxis, YAxis} from "recharts";
15
+
16
+
17
+ export default async function AnalyticsPage ( ) {
18
+ mongoose . connect ( process . env . MONGO_URI ) ;
19
+ const session = await getServerSession ( authOptions ) ;
20
+
21
+ if ( ! session ) {
22
+ return redirect ( '/' ) ;
23
+ }
24
+ const page = await Page . findOne ( { owner : session . user . email } ) ;
25
+ await Event . create ( { uri :page . uri , page :page . uri , type :'view' } ) ;
26
+
27
+ const groupedViews = await Event . aggregate ( [
28
+ {
29
+ $match : {
30
+ type : 'view' ,
31
+ uri : page . uri ,
32
+ }
33
+ } ,
34
+ {
35
+ $group : {
36
+ _id : {
37
+ $dateToString : {
38
+ date : "$createdAt" ,
39
+ format : "%Y-%m-%d"
40
+ } ,
41
+ } ,
42
+ count : {
43
+ "$count" : { } ,
44
+ }
45
+ } ,
46
+ } ,
47
+ {
48
+ $sort : { _id : 1 }
49
+ }
50
+ ] ) ;
51
+
52
+ const clicks = await Event . find ( {
53
+ page : page . uri ,
54
+ type : 'click' ,
55
+ } ) ;
56
+
57
+ return (
58
+ < div >
59
+ < SectionBox >
60
+ < h2 className = "text-xl mb-6 text-center" > Views</ h2 >
61
+ < Chart data = { groupedViews . map ( o => ( {
62
+ 'date' : o . _id ,
63
+ 'views' : o . count ,
64
+ } ) ) } />
65
+ </ SectionBox >
66
+ < SectionBox >
67
+ < h2 className = "text-xl mb-6 text-center" > Clicks</ h2 >
68
+ { page . links . map ( link => (
69
+ < div key = { link . title } className = "md:flex gap-4 items-center border-t border-gray-200 py-4" >
70
+ < div className = "text-blue-500 pl-4" >
71
+
72
+ < FaLink />
73
+ </ div >
74
+ < div className = "grow" >
75
+ < h3 > { link . title || 'no title' } </ h3 >
76
+ < p className = "text-gray-700 text-sm" > { link . subtitle || 'no description' } </ p >
77
+ < a className = "text-xs text-blue-400" target = "_blank" href = "link.url" > { link . url } </ a >
78
+ </ div >
79
+ < div className = "text-center" >
80
+ < div className = "border rounded-md p-2 mt-1 md:mt-0" >
81
+ < div className = "text-3xl" >
82
+ {
83
+ clicks
84
+ . filter (
85
+ c => c . uri === link . url
86
+ && isToday ( c . createdAt )
87
+ )
88
+ . length
89
+ }
90
+ </ div >
91
+ < div className = "text-gray-400 text-xs uppercase font-bold" > clicks today</ div >
92
+ </ div >
93
+ </ div >
94
+ < div className = "text-center" >
95
+ < div className = "border rounded-md p-2 mt-1 md:mt-0" >
96
+ < div className = "text-3xl" >
97
+ { clicks . filter ( c => c . uri === link . url ) . length }
98
+ </ div >
99
+ < div className = "text-gray-400 text-xs uppercase font-bold" > clicks total</ div >
100
+ </ div >
101
+ </ div >
102
+ </ div >
103
+ ) ) }
104
+ </ SectionBox >
105
+ </ div >
106
+ ) ;
107
+ }
0 commit comments