Skip to content

Commit a047f0f

Browse files
committed
Fix rpc calls
1 parent 7450a1b commit a047f0f

File tree

2 files changed

+21
-24
lines changed

2 files changed

+21
-24
lines changed

apps/web/.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,6 @@ yarn-error.log*
4343
.vscode
4444

4545
# Sentry Auth Token
46-
.sentryclirc
46+
.sentryclirc
47+
48+
tsconfig.tsbuildinfo

apps/web/pages/pages/[page_id]/analytics.tsx

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -196,29 +196,24 @@ export async function getServerSideProps({ req, res, query }) {
196196

197197
console.log(`Fetching stats for page ${page_id} from ${date.toISOString()}`);
198198

199-
// Get current period stats
200-
const {
201-
// @ts-ignore
202-
data: { page_views, visitors },
203-
} = await supabaseAdmin
204-
.rpc("page_view_stats", {
205-
pageid: String(page_id),
206-
date: date.toISOString(),
207-
})
208-
.maybeSingle()
209-
.throwOnError();
210-
211-
// Get previous period stats for comparison
212-
const {
213-
// @ts-ignore
214-
data: { page_views: prev_page_views, visitors: prev_visitors },
215-
} = await supabaseAdmin
216-
.rpc("page_view_stats", {
217-
pageid: String(page_id),
218-
date: prevDate.toISOString(),
219-
})
220-
.maybeSingle()
221-
.throwOnError();
199+
const { data: allPageViews } = await supabaseAdmin
200+
.from("page_views")
201+
.select("created_at, visitor_id")
202+
.eq("page_id", page_id)
203+
.gte("created_at", prevDate.toISOString());
204+
205+
const currentPeriodViews = allPageViews?.filter(
206+
(view) => new Date(view.created_at) >= date
207+
) || [];
208+
209+
const previousPeriodViews = allPageViews?.filter(
210+
(view) => new Date(view.created_at) >= prevDate && new Date(view.created_at) < date
211+
) || [];
212+
213+
const page_views = currentPeriodViews.length;
214+
const visitors = new Set(currentPeriodViews.map(v => v.visitor_id)).size;
215+
const prev_page_views = previousPeriodViews.length;
216+
const prev_visitors = new Set(previousPeriodViews.map(v => v.visitor_id)).size;
222217

223218
// Calculate growth rates
224219
const pageViewsGrowth =

0 commit comments

Comments
 (0)