Build customer-facing analytics
Contents
This guide shows how to build a customer-facing analytics feature where each user sees their own personalized stats. We'll track button clicks per user and query personalized counts from a PostHog endpoint.
This guide stores all customer data in one project. If you identify users, make each distinct ID unique to the customer to prevent person profiles from merging. See project setup for embedded analytics for details.


Before starting, make sure a PostHog SDK is installed in your app.
Step 1: Create an endpoint with a user variable
Open the SQL editor and write this query:
In the Variables tab, add a variable named user_id with type String.
Create the endpoint with the name hedgehog_clicks_by_user.
Step 2: Track events with user context
When tracking events, include a user_id property to associate them with specific users:
Step 3: Query the endpoint from your backend
Never expose your PostHog API key in client-side code. Always proxy endpoint calls through your backend.
Create an API route that queries the endpoint with the user's ID:
Then fetch from your frontend:
Security considerations
- Never expose API keys client-side - Always proxy through your backend
- Variables are required - Endpoints with variables require values, preventing accidental data leaks
- Validate user IDs - In production, verify the user is authorized to query data for the requested
user_id
Next steps
- Materialize your endpoint for faster response times
- Generate a typed SDK for type-safe API calls
- Learn about caching to reduce costs