@@ -3,6 +3,23 @@ import { createClient } from '@supabase/supabase-js'
33import { Resend } from 'resend'
44import type { EventProperties , PageParams } from './types'
55
6+ type LogLevel = 'info' | 'debug' | 'none'
7+
8+ const LOG_LEVEL = ( process . env . BYTESLICE_LOG_LEVEL || 'none' ) as LogLevel
9+
10+ const logger = {
11+ info : ( ...args : unknown [ ] ) => {
12+ if ( LOG_LEVEL === 'info' || LOG_LEVEL === 'debug' ) {
13+ console . log ( '[ByteSlice Events]' , ...args )
14+ }
15+ } ,
16+ debug : ( ...args : unknown [ ] ) => {
17+ if ( LOG_LEVEL === 'debug' ) {
18+ console . debug ( '[ByteSlice Events]' , ...args )
19+ }
20+ } ,
21+ }
22+
623if (
724 ! process . env . NEXT_PUBLIC_SUPABASE_URL ||
825 ! process . env . NEXT_PUBLIC_SUPABASE_ANON_KEY
@@ -27,6 +44,9 @@ export function track(
2744) : Promise < Result < { id : string } , Error > > {
2845 return withResult (
2946 async ( ) => {
47+ logger . info ( 'Tracking event:' , event )
48+ logger . debug ( 'Event properties:' , properties )
49+
3050 const { data, error } = await supabase
3151 . from ( 'events' )
3252 . insert ( {
@@ -40,7 +60,11 @@ export function track(
4060 . select ( 'id' )
4161 . single ( )
4262
63+ logger . info ( 'Event tracked:' , data ?. id )
64+ logger . debug ( 'Event data:' , data )
65+
4366 if ( error ) {
67+ logger . debug ( 'Error tracking event:' , error )
4468 throw error
4569 }
4670
@@ -61,6 +85,9 @@ export function page(
6185) : Promise < Result < { id : string } , Error > > {
6286 return withResult (
6387 async ( ) => {
88+ logger . info ( 'Tracking page view' )
89+ logger . debug ( 'Page params:' , params )
90+
6491 const { data, error } = await supabase
6592 . from ( 'events' )
6693 . insert ( {
@@ -72,7 +99,11 @@ export function page(
7299 . select ( 'id' )
73100 . single ( )
74101
102+ logger . info ( 'Page view tracked:' , data ?. id )
103+ logger . debug ( 'Page view data:' , data )
104+
75105 if ( error ) {
106+ logger . debug ( 'Error tracking page view:' , error )
76107 throw error
77108 }
78109
@@ -98,14 +129,24 @@ export async function sendEmail(
98129) : Promise < Result < { id : string } , Error > > {
99130 return withResult (
100131 async ( ) => {
132+ logger . info ( 'Sending email to:' , params . to )
133+ logger . debug ( 'Email details:' , {
134+ subject : params . subject ,
135+ from : params . from ,
136+ } )
137+
101138 const { data, error } = await resend . emails . send ( {
102139 from : params . from ?? 'transactions@byteslice.co' ,
103140 to : params . to ,
104141 subject : params . subject ,
105142 html : params . html ,
106143 } )
107144
145+ logger . info ( 'Email sent:' , data ?. id )
146+ logger . debug ( 'Email data:' , data )
147+
108148 if ( error ) {
149+ logger . debug ( 'Error sending email:' , error )
109150 throw error
110151 }
111152
0 commit comments