@@ -4,6 +4,7 @@ import { NextRequest } from "next/server";
44import type { AnalyticsEvent , AnalyticsPageview } from "./interfaces" ;
55import type { AnalyticsMetadata } from "../interfaces" ;
66import { isDoNotTrackEnabled } from "./utils" ;
7+ import { parseHeaders } from "./headers" ;
78
89type ServerContext = { request : Request } | { headers : Headers } ;
910
@@ -18,17 +19,17 @@ export async function trackEvent(
1819 eventName : string ,
1920 options : TrackEventOptions ,
2021) {
21- const headers =
22- "request" in options ? options . request . headers : options . headers ;
23-
2422 const hostname = options . hostname ?? process . env . SIMPLE_ANALYTICS_HOSTNAME ;
2523
2624 if ( ! hostname ) {
2725 console . error ( "No hostname provided for Simple Analytics" ) ;
2826 return ;
2927 }
3028
29+ const headers = "request" in options ? options . request . headers : options . headers ;
30+
3131 if ( isDoNotTrackEnabled ( headers ) && ! options . collectDnt ) {
32+ console . log ( "Do not track enabled, not tracking event" ) ;
3233 return ;
3334 }
3435
@@ -37,9 +38,11 @@ export async function trackEvent(
3738 hostname,
3839 event : eventName ,
3940 metadata : options . metadata ,
40- ua : headers . get ( "user-agent" ) ?? "" ,
41+ ... ( parseHeaders ( headers , { } ) ) ,
4142 } ;
4243
44+ console . log ( "Tracking event" , payload ) ;
45+
4346 const response = await fetch ( "https://queue.simpleanalyticscdn.com/events" , {
4447 method : "POST" ,
4548 headers : {
@@ -95,6 +98,7 @@ export async function trackPageview(options: TrackPageviewOptions) {
9598 }
9699
97100 if ( isDoNotTrackEnabled ( headers ) && ! options . collectDnt ) {
101+ console . log ( "Do not track enabled, not tracking pageview" ) ;
98102 return ;
99103 }
100104
@@ -105,9 +109,11 @@ export async function trackPageview(options: TrackPageviewOptions) {
105109 hostname,
106110 event : "pageview" ,
107111 path,
108- ua : headers . get ( "user-agent" ) ?? "" ,
112+ ... ( parseHeaders ( headers , { } ) ) ,
109113 } ;
110114
115+ console . log ( "Tracking pageview" , payload ) ;
116+
111117 const response = await fetch ( "https://queue.simpleanalyticscdn.com/events" , {
112118 method : "POST" ,
113119 headers : {
0 commit comments