> AI agents: this is one page from PostHog's docs. Full index of Markdown docs for LLMs: https://posthog.com/llms.txt # Build customer-facing analytics Canonical URL: https://posthog.com/docs/endpoints/customer-facing-analytics > **Cloud region:** This page uses US Cloud hosts. For an EU Cloud project, use `https://eu.posthog.com` instead of `https://us.posthog.com` (app host for links). ## On this page - [Step 1: Create an endpoint with a user variable](#step-1-create-an-endpoint-with-a-user-variable) - [Step 2: Track events with user context](#step-2-track-events-with-user-context) - [Step 3: Query the endpoint from your backend](#step-3-query-the-endpoint-from-your-backend) - [Security considerations](#security-considerations) - [Next steps](#next-steps) 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. **Choose your project setup first** 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](https://posthog.com/docs/data/embedded-analytics-projects.md) for details. ![Customer-facing analytics demo app](https://res.cloudinary.com/dmukukwp6/image/upload/w_1000,c_limit,q_auto,f_auto/pasted_image_2026_02_09_T10_05_55_325_Z_015ac1dc57.png) **Prerequisites** Before starting, make sure [a PostHog SDK is installed](https://posthog.com/docs/getting-started/install.md) in your app. ## Step 1: Create an endpoint with a user variable Open the [SQL editor](https://us.posthog.com/sql) and write this query: ```sql SELECT count() as click_count FROM events WHERE event = 'hedgehog_clicked' AND properties.user_id = {variables.user_id} ``` 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: ```jsx posthog.capture("hedgehog_clicked", { user_id: currentUser.id, }); ``` ## Step 3: Query the endpoint from your backend **Keep API keys server-side** 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: ```typescript // Example: Next.js API route export async function POST(request: Request) { const { user_id } = await request.json(); const response = await fetch( `https://us.posthog.com/api/projects/${PROJECT_ID}/endpoints/hedgehog_clicks_by_user/run`, { method: "POST", headers: { "Content-Type": "application/json", Authorization: `Bearer ${process.env.POSTHOG_API_KEY}`, }, body: JSON.stringify({ variables: { user_id }, }), } ); const data = await response.json(); return Response.json({ click_count: data.results?.[0]?.[0] ?? 0 }); } ``` Then fetch from your frontend: ```jsx const response = await fetch("/api/hedgehog-clicks", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ user_id: currentUser.id }), }); const { click_count } = await response.json(); ``` ## Security considerations 1. **Never expose API keys client-side** - Always proxy through your backend 2. **Variables are required** - Endpoints with variables require values, preventing accidental data leaks 3. **Validate user IDs** - In production, verify the user is authorized to query data for the requested `user_id` ## Next steps - [Materialize your endpoint](https://posthog.com/docs/endpoints/materialization.md) for faster response times - [Generate a typed SDK](https://posthog.com/docs/endpoints/openapi-sdk-generation.md) for type-safe API calls - Learn about [caching](https://posthog.com/docs/endpoints/caching.md) to reduce costs