@@ -13,28 +13,33 @@ const isBrowser = (): boolean => {
1313} ;
1414
1515export function inHouseAnalytics ( event : string , eventRef : Dict ) {
16- if ( ! isBrowser ( ) ) return ;
17- // if localhost, return
18- if ( window . location . hostname === "localhost" ) return ;
16+ if ( ! isBrowser ( ) ) {
17+ console . log ( 'Analytics not sent: Not in browser environment' ) ;
18+ return ;
19+ }
20+
21+ // Debug logs (remove in production)
22+ // console.log(`Analytics event: ${event}`, eventRef);
1923
24+ // Skip tracking on localhost unless in debug mode
25+ const isLocalhost = window . location . hostname === "localhost" ;
26+ if ( isLocalhost ) {
27+ return ;
28+ }
29+ event = "docs_" + event
2030 const cId = localStorage . getItem ( "cId" ) ;
21- let email = localStorage . getItem ( "aId" ) ;
22- // capture all query params in eventRef
23- const queryParams = new URLSearchParams ( window . location . search ) ;
24- queryParams . forEach ( ( value , key ) => {
25- if ( key !== "oid" && key !== "ot" && key !== "eid" ) {
26- eventRef [ key ] = value ;
27- }
28- } ) ;
31+ let email = localStorage . getItem ( "email" ) ;
2932
3033 if ( ! email ) {
3134 // Generate a unique anonymous ID if email doesn't exist
32- let anonymousId = localStorage . getItem ( "aId" ) ;
35+ let anonymousId = getQueryParameter ( "aId" ) || localStorage . getItem ( "aId" ) ;
36+
3337 if ( ! anonymousId ) {
3438 // Create unique ID combining timestamp and random string
3539 anonymousId = `an_${ Date . now ( ) } _${ Math . random ( ) . toString ( 36 ) . substring ( 2 , 10 ) } ` ;
36- localStorage . setItem ( "aId" , anonymousId ) ;
40+ // localStorage.setItem("aId", anonymousId);
3741 }
42+ localStorage . setItem ( "aId" , anonymousId ) ;
3843 email = anonymousId ; // Use this as the identifier instead of email
3944 }
4045
@@ -53,51 +58,103 @@ export function inHouseAnalytics(event: string, eventRef: Dict) {
5358 url : isBrowser ( ) ? window . location . href : '' ,
5459 referrer : isBrowser ( ) ? document . referrer : '' ,
5560 eId : eId ,
61+ timestamp : new Date ( ) . toISOString ( ) ,
62+ userAgent : navigator . userAgent
5663 } ) ,
5764 } ;
5865
5966 try {
60- // axiosInstance.post("v1/analytics/track", data);
67+ console . log ( 'Sending analytics data:' , data ) ;
68+
6169 fetch ( "https://production-gateway.snorkell.ai/api/v1/analytics/track" , {
6270 method : 'POST' ,
6371 headers : {
6472 'Content-Type' : 'application/json'
6573 } ,
6674 body : JSON . stringify ( data ) ,
75+ // Add these options for better cross-origin support
76+ mode : 'cors' ,
77+ credentials : 'omit'
6778 } )
6879 . then ( response => {
69- // Handle response if needed
80+ console . log ( 'Analytics response status:' , response . status ) ;
81+ if ( ! response . ok ) {
82+ console . error ( 'Analytics API error:' , response . status ) ;
83+ }
84+ return response . text ( ) ;
85+ } )
86+ . then ( text => {
87+ if ( text ) {
88+ try {
89+ const json = JSON . parse ( text ) ;
90+ console . log ( 'Analytics API response:' , json ) ;
91+ } catch ( e ) {
92+ console . log ( 'Analytics API response (text):' , text ) ;
93+ }
94+ }
7095 } )
7196 . catch ( error => {
72- console . log ( error ) ;
97+ console . error ( 'Analytics fetch error:' , error ) ;
7398 } ) ;
7499 } catch ( error ) {
75- console . log ( error ) ;
100+ console . error ( 'Analytics error:' , error ) ;
76101 }
77102}
78103
79104export const pageView = ( url : string ) => {
80- inHouseAnalytics ( "pageView_docs " , { url} ) ;
105+ inHouseAnalytics ( "pageView " , { url} ) ;
81106} ;
82107
83108export const getQueryParameter = ( name : string ) => {
84- if ( ! isBrowser ( ) ) { console . log ( "server side rendering" ) ; return null } ;
109+ if ( ! isBrowser ( ) ) {
110+ console . log ( "Server side rendering - can't access URL parameters" ) ;
111+ return null
112+ } ;
85113 const urlParams = new URLSearchParams ( window . location . search ) ;
86114 return urlParams . get ( name ) ;
87115} ;
88116
89117export const trackLinkClick = ( url : string , email : string = "" , cId : string = "" ) => {
90- inHouseAnalytics ( "linkClick_docs" , { url, email, cId, home_url : window . location . href } ) ;
118+ inHouseAnalytics ( "linkClick" , {
119+ url,
120+ referrer : document . referrer || window . location . href
121+ } ) ;
91122} ;
92123
93124export const trackScroll = ( value : number ) => {
94- inHouseAnalytics ( "scroll_docs" , { event :"scrolled 50% on homepage" , home_url : window . location . href } ) ;
125+ inHouseAnalytics ( "scroll" , {
126+ depth : `${ value } %` ,
127+ url : window . location . href
128+ } ) ;
95129} ;
96130
97131export const trackFormSubmission = ( value : [ string ] ) => {
98- inHouseAnalytics ( "formSubmission_docs" , { event :"contact us form submission" , home_url : window . location . href } ) ;
132+ inHouseAnalytics ( "formSubmission" , {
133+ event : "contact us form submission" ,
134+ formData : value
135+ } ) ;
99136} ;
100137
101138export const trackVideoStart = ( value : boolean ) => {
102- inHouseAnalytics ( "videoView_docs" , { event :"Penify.dev video tuts" , home_url : window . location . href } ) ;
103- } ;
139+ inHouseAnalytics ( "videoView" , {
140+ event : "Penify.dev video tuts" ,
141+ play : value
142+ } ) ;
143+ } ;
144+
145+ // Enable debug mode for localhost testing
146+ export const enableAnalyticsDebug = ( ) => {
147+ if ( isBrowser ( ) ) {
148+ sessionStorage . setItem ( 'analytics_debug' , 'true' ) ;
149+ console . log ( 'Analytics debug mode enabled' ) ;
150+ }
151+ } ;
152+
153+ // Initialize analytics on page load
154+ if ( isBrowser ( ) ) {
155+ // Auto-initialize
156+ window . addEventListener ( 'load' , ( ) => {
157+ console . log ( 'Analytics initialized on page load' ) ;
158+ pageView ( window . location . href ) ;
159+ } ) ;
160+ }
0 commit comments