Skip to content

Commit

Permalink
Add analytics event emission for specific routes
Browse files Browse the repository at this point in the history
Added an `emitAnalytics` function to send events to a link gateway for paths starting with "/abs". Updated the middleware to call this function and avoid it for certain paths.
  • Loading branch information
thostetler committed Oct 18, 2024
1 parent 57f703c commit a06d298
Showing 1 changed file with 34 additions and 3 deletions.
37 changes: 34 additions & 3 deletions src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,34 @@ const protectedRoute = async (req: NextRequest, res: NextResponse) => {
return redirect(url, req, { message: 'login-required', clearParams: false });
};

/**
* Sends an analytics event for specific routes to a designated link gateway.
*
* If the request's path starts with '/abs', an event is sent to the link gateway with
* the adjusted path. Otherwise, no event is sent.
*
* @param {NextRequest} req - The request object containing the URL information.
* @returns {Promise<void>} - A promise that resolves when the operation completes,
* either successfully or with an error.
*/
const emitAnalytics = async (req: NextRequest): Promise<void> => {
const path = req.nextUrl.pathname;

// For abs/ routes we want to send emit an event to the link gateway
if (path.startsWith('/abs')) {
const url = `${process.env.BASE_URL}/link_gateway${path.replace('/abs', '')}`;
log.debug({ path, url }, 'Emitting abs route event to link gateway');

try {
await fetch(url, { method: 'GET' });
log.debug('Event successfully sent to link gateway');
} catch (err) {
log.error({ err: err as Error }, 'Error sending event to link gateway');
}
}
return Promise.resolve();
};

export async function middleware(req: NextRequest) {
const path = req.nextUrl.pathname;
log.info(
Expand All @@ -112,8 +140,11 @@ export async function middleware(req: NextRequest) {

const res = NextResponse.next();

// Skip middleware for the root path
if (path === '/') {
// Emit analytics
void emitAnalytics(req);

// Skip middleware for some paths
if (path === '/' || path.startsWith('/_next/data')) {
return res;
}

Expand Down Expand Up @@ -161,7 +192,7 @@ export const config = {
matcher: [
{
source:
'/((?!api|_next/static|_next/data|light|dark|_next/image|favicon|android|images|mockServiceWorker|site.webmanifest|error|feedback|classic-form|paper-form).*)',
'/((?!api|_next/static|light|dark|_next/image|favicon|android|images|mockServiceWorker|site.webmanifest|error|feedback|classic-form|paper-form).*)',
missing: [
{ type: 'header', key: 'next-router-prefetch' },
{ type: 'header', key: 'purpose', value: 'prefetch' },
Expand Down

0 comments on commit a06d298

Please sign in to comment.