Skip to content

Commit

Permalink
fix(portal): seint-533 use track instead of the Analytics component f…
Browse files Browse the repository at this point in the history
…or vercel web analytics (#351)
  • Loading branch information
Tzal3x authored Jan 13, 2025
1 parent a37b05d commit 32de65a
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
5 changes: 4 additions & 1 deletion portal/server/app/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,21 @@ import integrateLoggerWithSentry from "sentry_logger";
import blocklistChecker from "custom_blocklist_checker";
import { config } from "configuration_loader";
import { standardUrlFetcher, premiumUrlFetcher } from "url_fetcher_factory";
import { NextRequest } from "next/server";
import { send_to_web_analytics } from "web_analytics";

if (config.enableSentry) {
// Only integrate Sentry on production.
integrateLoggerWithSentry();
}

export async function GET(req: Request) {
export async function GET(req: NextRequest) {
const originalUrl = req.headers.get("x-original-url");
if (!originalUrl) {
throw new Error("No original url found in request headers");
}
const url = new URL(originalUrl);
await send_to_web_analytics(req);

const objectIdPath = getObjectIdLink(url.toString());
const portalDomainNameLength = config.portalDomainNameLength;
Expand Down
50 changes: 50 additions & 0 deletions portal/server/web_analytics.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

import { NextRequest, NextResponse } from 'next/server'
import { track } from '@vercel/analytics/server'

export async function send_to_web_analytics(request: NextRequest) {
// Extract various details from the request
const trackingData = extract_tracking_data(request)

// Track the event with comprehensive data
await track('route-access', trackingData)
}

function extract_tracking_data(request: NextRequest): TrackingData {
const geo = request.geo || {}

return {
originalUrl: request.headers.get('x-original-url') || 'Unknown User Agent',

// Network Information
ip: request.ip || 'Unknown IP',
country: geo.country || 'Unknown',
region: geo.region || 'Unknown',
city: geo.city || 'Unknown',

// Client Information
userAgent: request.headers.get('user-agent') || 'Unknown User Agent',
referer: request.headers.get('referer') || 'Direct',

// Additional Context
timestamp: new Date().toISOString(),
protocol: request.nextUrl.protocol,
}
}

type TrackingData = {
originalUrl: string
city: string

ip: string
country: string
region: string

userAgent: string
referer: string

timestamp: string
protocol: string
}

0 comments on commit 32de65a

Please sign in to comment.