11import json
22import jwt
33from typing import Dict , Any
4- from fastapi import APIRouter , HTTPException , Depends
4+ from fastapi import APIRouter , HTTPException , Depends , Request
55from fastapi .responses import JSONResponse
66
77from dependencies import SessionData , require_auth
88from db import store_canvas_data , get_canvas_data
9+ import posthog
910
1011canvas_router = APIRouter ()
1112
@@ -19,14 +20,49 @@ def get_default_canvas_data():
1920 detail = f"Failed to load default canvas: { str (e )} "
2021 )
2122
23+ @canvas_router .get ("/default" )
24+ async def get_default_canvas (auth : SessionData = Depends (require_auth )):
25+ try :
26+ with open ("default_canvas.json" , "r" ) as f :
27+ canvas_data = json .load (f )
28+ return canvas_data
29+ except Exception as e :
30+ return JSONResponse (
31+ status_code = 500 ,
32+ content = {"error" : f"Failed to load default canvas: { str (e )} " }
33+ )
34+
2235@canvas_router .post ("" )
23- async def save_canvas (data : Dict [str , Any ], auth : SessionData = Depends (require_auth )):
36+ async def save_canvas (data : Dict [str , Any ], auth : SessionData = Depends (require_auth ), request : Request = None ):
2437 access_token = auth .token_data .get ("access_token" )
2538 decoded = jwt .decode (access_token , options = {"verify_signature" : False })
2639 user_id = decoded ["sub" ]
2740 success = await store_canvas_data (user_id , data )
2841 if not success :
2942 raise HTTPException (status_code = 500 , detail = "Failed to save canvas data" )
43+ # PostHog analytics: capture canvas_saved event
44+ try :
45+ app_state = data .get ("appState" , {})
46+ width = app_state .get ("width" )
47+ height = app_state .get ("height" )
48+ zoom = app_state .get ("zoom" , {}).get ("value" )
49+ api_path = str (request .url .path ) if request else None
50+ full_url = None
51+ if request :
52+ full_url = str (request .base_url ).rstrip ("/" ) + str (request .url .path )
53+ posthog .capture (
54+ distinct_id = user_id ,
55+ event = "canvas_saved" ,
56+ properties = {
57+ "pad_width" : width ,
58+ "pad_height" : height ,
59+ "pad_zoom" : zoom ,
60+ "$current_url" : full_url ,
61+ }
62+ )
63+ except Exception as e :
64+ print (f"Error capturing canvas_saved event: { str (e )} " )
65+ pass
3066 return {"status" : "success" }
3167
3268@canvas_router .get ("" )
0 commit comments