Is there a way to log an event on Google Analytics when an API route is accessed?
Currently, my gtag implementation looks like this:
export const logEvent = ({ action, category, label, value }: LogEventProps) => {
(window as any).gtag("event", action, {
event_category: category,
event_label: label,
value: value,
});
};
The issue is that it depends on the window object to trigger logs, which is undefined when calling this method from an api
route.
I haven't been able to find any useful information on how to log events from the server side.
One solution could be to have the API route redirect to a blank page that logs the event, but I'm not entirely satisfied with that and am wondering if there's a better approach.