retention
This commit is contained in:
49
.gitignore
vendored
49
.gitignore
vendored
@@ -8,10 +8,10 @@
|
|||||||
# testing
|
# testing
|
||||||
/coverage
|
/coverage
|
||||||
|
|
||||||
# next.js
|
# next.js
|
||||||
/.next/
|
/.next/
|
||||||
/.next-stale-module-cache/
|
/.next-stale-module-cache/
|
||||||
/out/
|
/out/
|
||||||
|
|
||||||
# production
|
# production
|
||||||
/build
|
/build
|
||||||
@@ -43,6 +43,9 @@ next-env.d.ts
|
|||||||
docker-compose.override.yml
|
docker-compose.override.yml
|
||||||
*.sql
|
*.sql
|
||||||
!prisma/migrations/**/*.sql
|
!prisma/migrations/**/*.sql
|
||||||
|
# Hand-applied schema changes and analysis queries belong in history.
|
||||||
|
# Backup dumps land in the repo root, so they stay ignored.
|
||||||
|
!sql/**/*.sql
|
||||||
/backups/
|
/backups/
|
||||||
|
|
||||||
# logs
|
# logs
|
||||||
@@ -71,22 +74,22 @@ tmp/
|
|||||||
.codex-temp/
|
.codex-temp/
|
||||||
*.report.html
|
*.report.html
|
||||||
*.report.json
|
*.report.json
|
||||||
tmp_*.js
|
tmp_*.js
|
||||||
test_email.py
|
test_email.py
|
||||||
meta-fix.js
|
meta-fix.js
|
||||||
read-inbox.mjs
|
read-inbox.mjs
|
||||||
quora_antwort_statisch_dynamisch.txt
|
quora_antwort_statisch_dynamisch.txt
|
||||||
|
|
||||||
# Local blog audit reports and temporary snapshots
|
# Local blog audit reports and temporary snapshots
|
||||||
scratch_blog_analysis.json
|
scratch_blog_analysis.json
|
||||||
scratch_scored_blog_posts.json
|
scratch_scored_blog_posts.json
|
||||||
src/lib/blog-data.snapshot-*.ts
|
src/lib/blog-data.snapshot-*.ts
|
||||||
|
|
||||||
# Local developer-package workspaces and unreferenced generated media
|
# Local developer-package workspaces and unreferenced generated media
|
||||||
/packages/
|
/packages/
|
||||||
/public/Events/
|
/public/Events/
|
||||||
/public/Gyms/
|
/public/Gyms/
|
||||||
/public/Hotels/
|
/public/Hotels/
|
||||||
/public/Real Estate/
|
/public/Real Estate/
|
||||||
/public/restaurant/
|
/public/restaurant/
|
||||||
/.qr-master-api-health-state
|
/.qr-master-api-health-state
|
||||||
|
|||||||
@@ -38,6 +38,11 @@ model User {
|
|||||||
thirtyDayNudgeSentAt DateTime?
|
thirtyDayNudgeSentAt DateTime?
|
||||||
limitReachedNudgeSentAt DateTime?
|
limitReachedNudgeSentAt DateTime?
|
||||||
firstScanNudgeSentAt DateTime?
|
firstScanNudgeSentAt DateTime?
|
||||||
|
qrPulseSentAt DateTime?
|
||||||
|
|
||||||
|
/// When the user last looked at their own scan numbers. A live session is not
|
||||||
|
/// the same as someone having seen a number, so this is what "inactive" means.
|
||||||
|
lastAnalyticsViewAt DateTime?
|
||||||
|
|
||||||
// RevOps attribution
|
// RevOps attribution
|
||||||
signupSource String?
|
signupSource String?
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import { NextRequest, NextResponse } from 'next/server';
|
|||||||
import { getSessionUserId } from '@/lib/session';
|
import { getSessionUserId } from '@/lib/session';
|
||||||
import { db } from '@/lib/db';
|
import { db } from '@/lib/db';
|
||||||
import { rateLimit, getClientIdentifier, RateLimits } from '@/lib/rateLimit';
|
import { rateLimit, getClientIdentifier, RateLimits } from '@/lib/rateLimit';
|
||||||
|
import { touchAnalyticsView } from '@/lib/analyticsActivity';
|
||||||
import { TrendData } from '@/types/analytics';
|
import { TrendData } from '@/types/analytics';
|
||||||
|
|
||||||
export const dynamic = 'force-dynamic';
|
export const dynamic = 'force-dynamic';
|
||||||
@@ -67,6 +68,10 @@ export async function GET(request: NextRequest) {
|
|||||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Serves both the analytics page and the dashboard, so this is the one
|
||||||
|
// place that knows the user actually saw their numbers. Fire and forget.
|
||||||
|
touchAnalyticsView(userId);
|
||||||
|
|
||||||
// Get date range from query params (default: last 30 days)
|
// Get date range from query params (default: last 30 days)
|
||||||
const { searchParams } = request.nextUrl;
|
const { searchParams } = request.nextUrl;
|
||||||
const range = searchParams.get('range') || '30';
|
const range = searchParams.get('range') || '30';
|
||||||
|
|||||||
41
src/lib/analyticsActivity.ts
Normal file
41
src/lib/analyticsActivity.ts
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
import { db } from '@/lib/db';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Records that a signed-in user looked at their own scan numbers.
|
||||||
|
*
|
||||||
|
* This exists because "inactive" cannot be answered from anything else we
|
||||||
|
* store. A session can stay alive for weeks without the user ever opening their
|
||||||
|
* analytics, so a login timestamp would call someone active who has not seen a
|
||||||
|
* number in a month. PostHog cannot answer it either - capture there is gated on
|
||||||
|
* cookie consent and runs client-side, so it covers an unknown subset.
|
||||||
|
*
|
||||||
|
* Written server-side, on the endpoint that serves the numbers. That endpoint is
|
||||||
|
* the single choke point for both the analytics page and the dashboard.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/** Repeat views inside this window do not cause another write. */
|
||||||
|
const THROTTLE_MS = 60 * 60 * 1000;
|
||||||
|
|
||||||
|
export function touchAnalyticsView(userId: string): void {
|
||||||
|
const now = new Date();
|
||||||
|
const staleBefore = new Date(now.getTime() - THROTTLE_MS);
|
||||||
|
|
||||||
|
// updateMany, not update: the throttle lives in the WHERE clause, so a repeat
|
||||||
|
// view inside the window matches no rows instead of racing a read.
|
||||||
|
db.user
|
||||||
|
.updateMany({
|
||||||
|
where: {
|
||||||
|
id: userId,
|
||||||
|
OR: [
|
||||||
|
{ lastAnalyticsViewAt: null },
|
||||||
|
{ lastAnalyticsViewAt: { lt: staleBefore } },
|
||||||
|
],
|
||||||
|
},
|
||||||
|
data: { lastAnalyticsViewAt: now },
|
||||||
|
})
|
||||||
|
.catch((error) => {
|
||||||
|
// Fire and forget. Analytics must still render if this write fails - it
|
||||||
|
// also fails harmlessly if the column has not been added yet.
|
||||||
|
console.error('Failed to record analytics view:', error);
|
||||||
|
});
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user