-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(portal): seint-533 use track instead of the Analytics component f…
…or vercel web analytics (#351)
- Loading branch information
Showing
2 changed files
with
54 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |