This document explains how PostHog analytics is integrated into your movie app and how to use it.
Create a .env file in your project root with your PostHog credentials:
VITE_PUBLIC_POSTHOG_KEY=your_posthog_key_here
VITE_PUBLIC_POSTHOG_HOST=https://app.posthog.com- Sign up at PostHog
- Create a new project
- Copy your project API key from the project settings
- Replace
your_posthog_key_herewith your actual key
- Event:
search_performed - Properties:
query: Search termresults_count: Number of results foundsearch_type: "movie" or "actor"timestamp: When the search occurred
- Event:
navigation - Properties:
from_page: Current page URLto_page: Destination page URLtimestamp: When navigation occurred
- Event:
actor_card_clicked - Properties:
actor_id: Actor's TMDB IDactor_name: Actor's nameactor_character: Character they playedis_deceased: Whether the actor is deceasedvariant: Card variant (default, detailed, etc.)orientation: Card orientation (vertical, horizontal)timestamp: When the click occurred
import { usePostHog } from "posthog-js/react";
const MyComponent = () => {
const posthog = usePostHog();
const handleClick = () => {
posthog.capture("button_clicked", {
button_name: "search",
page: "home",
});
};
return <button onClick={handleClick}>Click me</button>;
};import {
trackMovieEvent,
trackActorEvent,
trackSearchEvent,
} from "../utils/posthog";
// Track movie-related events
trackMovieEvent("movie_viewed", {
movie_id: 123,
movie_title: "The Matrix",
release_year: 1999,
});
// Track actor-related events
trackActorEvent("actor_viewed", {
actor_id: 456,
actor_name: "Keanu Reeves",
is_deceased: false,
});
// Track search events
trackSearchEvent("matrix", 5, "movie");src/
├── main.tsx # PostHogProvider setup
├── utils/
│ └── posthog.ts # PostHog utility functions
└── components/
├── MovieSearch/ # Search tracking
└── ActorCard/ # Actor card tracking
PostHog is configured in src/main.tsx with these options:
const options = {
api_host: import.meta.env.VITE_PUBLIC_POSTHOG_HOST,
defaults: "2025-05-24",
};- Go to your PostHog dashboard
- Navigate to "Events" to see all tracked events
- Use "Insights" to create charts and funnels
- Set up "Feature Flags" for A/B testing
import { usePostHog } from "posthog-js/react";
const MovieDetails = () => {
const posthog = usePostHog();
useEffect(() => {
posthog.capture("page_viewed", {
page: "movie_details",
movie_id: movieId,
});
}, [movieId]);
// ... rest of component
};const handleFilterChange = (filterType: string, value: string) => {
posthog.capture("filter_applied", {
filter_type: filterType,
filter_value: value,
page: "movie_list",
});
};const handleError = (error: Error) => {
posthog.capture("error_occurred", {
error_message: error.message,
error_stack: error.stack,
page: "movie_details",
});
};- PostHog respects user privacy and doesn't collect personal information by default
- All tracking is anonymous unless you explicitly identify users
- Environment variables are kept secure and not committed to version control
- Check your PostHog API key is correct
- Verify the environment variables are loaded
- Check the browser console for any errors
- Ensure you're looking at the right project in PostHog
- PostHog automatically detects the environment
- Development events are marked differently in the dashboard
- Use feature flags to control tracking in different environments