@gptrends/track
v1.5.1
Published
Lightweight GEO tracking SDK for Next.js applications.
Readme
@gptrends/track
Lightweight GEO tracking SDK for Next.js and Astro applications.
Installation
npm install @gptrends/trackNext.js
Recommended: Automatic Tracking
Wraps your middleware to automatically track requests with response status and duration:
import { withGPTrendsTracking } from '@gptrends/track/nextjs';
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
export const middleware = withGPTrendsTracking(
{ websiteKey: 'gptrends_secret_...' },
async (request: NextRequest) => {
// Your middleware logic
return NextResponse.next();
}
);
export const config = {
matcher: ["/((?!api|_next/static|_next/image|.*\\.).*)", "/robots.txt", "/sitemap.xml"],
};Manual Tracking
For more control over tracking:
import { GPTrendsTrackerNextJS } from '@gptrends/track/nextjs';
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
const gptrendsTrack = GPTrendsTrackerNextJS({
websiteKey: 'gptrends_secret_...'
});
export async function middleware(request: NextRequest) {
const startTime = Date.now();
const response = NextResponse.next();
const duration = Date.now() - startTime;
gptrendsTrack(request, response, duration);
return response;
}Astro
Add tracking to your Astro middleware:
import { defineMiddleware } from 'astro:middleware';
import { GPTrendsTrackerAstro } from '@gptrends/track/astro';
const gptrendsTrack = GPTrendsTrackerAstro({
websiteKey: 'gptrends_secret_...'
});
export const onRequest = defineMiddleware((context, next) => {
const path = context.url.pathname;
// Track all requests except static assets
if (path === '/robots.txt' || path === '/sitemap.xml' || !path.match(/\.[^/]+$/)) {
gptrendsTrack(context);
}
return next();
});Configuration
| Option | Type | Required | Description |
|--------|------|----------|-------------|
| websiteKey | string | Yes | Your GPTrends website key |
| drainURL | string | No | Custom tracking endpoint URL |
Data Collected
- Page URL and referrer
- Response status and duration (Next.js only)
- User Agent (not stored)
- IP Address (not stored)
License
CC-BY-NC-SA-4.0 - NonCommercial use only
