'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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
