Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

326
Views
NextJS call gtag("event") from api route

I'd like to log an event on Google Analytics when an api route is opened.

My gtag implementation is as follows at the moment:

export const logEvent = ({ action, category, label, value }: LogEventProps) => {
  (window as any).gtag("event", action, {
    event_category: category,
    event_label: label,
    value: value,
  });
};

Problem is that it relies on a window for logs to be fired, which is undefined, understandably so, whenever I'm calling this method from an api route.

I couldn't find any valuable information on how to log an event from the server side.

A work-around might be to have the api redirect to a blank page that logs the event, but I don't love the idea and wonder if there's a better approach.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!