How to install PostHog in your project on PostHog
Installing PostHog in your project involves creating a project in your PostHog dashboard, copying your API key, and adding the PostHog JavaScript snippet to your application. This enables automatic event tracking and analytics for your web application.
Prerequisites
- A PostHog account
- A web application project
- Basic knowledge of JavaScript
- Access to your project's codebase
Step-by-Step Instructions
Create a PostHog account and project
Get your project API key
Choose your installation method
Install PostHog JavaScript library
<head> section:<script>
!function(t,e){var o,n,p,r;e.__SV||(window.posthog=e,e._i=[],e.init=function(i,s,a){function g(t,e){var o=e.split(".");2==o.length&&(t=t[o[0]],e=o[1]);t[e]=function(){t.push([e].concat(Array.prototype.slice.call(arguments,0)))}}(p=t.createElement("script")).type="text/javascript",p.async=!0,p.src=s.api_host+"/static/array.js",(r=t.getElementsByTagName("script")[0]).parentNode.insertBefore(p,r);var u=e;for(void 0!==a?u=e[a]=[]:a="posthog",u.people=u.people||[],u.toString=function(t){var e="posthog";return"posthog"!==a&&(e+='.'+a),e},u.people.toString=function(){return u.toString(1)+".people (stub)"},o="capture identify alias people.set people.set_once set_config register register_once unregister opt_out_capturing has_opted_out_capturing opt_in_capturing reset isFeatureEnabled onFeatureFlags getFeatureFlag getFeatureFlagPayload reloadFeatureFlags group updateEarlyAccessFeatureEnrollment getEarlyAccessFeatures getActiveMatchingSurveys getSurveys".split(" "),n=0;n<o.length;n++)g(u,o[n]);e._i.push([i,s,a])},e.__SV=1)}(document,window.posthog||[]);
posthog.init('YOUR_API_KEY',{api_host:'https://app.posthog.com'})
</script>Replace
YOUR_API_KEY with your actual API key.Configure PostHog settings
posthog.init('YOUR_API_KEY', {
api_host: 'https://app.posthog.com',
autocapture: true,
capture_pageview: true,
disable_session_recording: false
})Set autocapture to automatically track clicks and form submissions.
Verify installation
Set up custom event tracking
// Track a custom event
posthog.capture('button_clicked', {
button_name: 'signup',
page: 'homepage'
});
// Identify users
posthog.identify('user123', {
email: 'user@example.com',
plan: 'premium'
});Place these calls where relevant user actions occur in your application.
Common Issues & Troubleshooting
Events not appearing in PostHog dashboard
Check your browser's developer console for JavaScript errors. Verify your API key is correct and ensure you're not blocking the PostHog script with ad blockers. Events may take up to 5 minutes to appear.
PostHog script blocked by content security policy
Add https://app.posthog.com to your CSP header: script-src 'self' https://app.posthog.com; connect-src 'self' https://app.posthog.com
Duplicate events being tracked
Ensure you're only initializing PostHog once in your application. For single-page applications, avoid calling posthog.init() multiple times during navigation.
Session recordings not working
Verify that disable_session_recording is set to false in your configuration and check that your plan includes session recording features in your PostHog project settings.