Angular web analytics installation

  1. Install the package

    Required

    Install the PostHog JavaScript library using your package manager:

    npm install posthog-js
  2. Initialize PostHog

    Required

    In your src/main.ts, initialize PostHog using your project API key and instance address:

    src/main.ts
    import { bootstrapApplication } from '@angular/platform-browser';
    import { appConfig } from './app/app.config';
    import { AppComponent } from './app/app.component';
    import posthog from 'posthog-js'
    posthog.init(
    '<ph_project_api_key>',
    {
    api_host: 'https://us.i.posthog.com',
    defaults: '2026-01-30'
    }
    )
    bootstrapApplication(AppComponent, appConfig)
    .catch((err) => console.error(err));
  3. Send events

    Click around and view a couple pages to generate some events. PostHog automatically captures pageviews, clicks, and other interactions for you.

    If you'd like, you can also manually capture custom events:

    JavaScript
    posthog.capture('my_custom_event', { property: 'value' })
  4. Next steps

    Recommended

    After installing PostHog and ensuring autocapture is enabled, head to your web analytics dashboard to see your data. And then check out our getting started guide.

    PostHog tip: Web analytics works with anonymous events. This means if you are primarily using PostHog for web analytics, it can be significantly cheaper for you.

Community questions

Was this page useful?

Questions about this page? or post a community question.