Web product analytics installation

  1. Install the package

    Required

    Install the PostHog JavaScript library using your package manager:

    npm install posthog-js
  2. Initialize PostHog

    Required

    Import and initialize the PostHog library with your project API key and host:

    JavaScript
    import posthog from 'posthog-js'
    posthog.init('<ph_project_api_key>', {
    api_host: 'https://us.i.posthog.com',
    person_profiles: 'identified_only' // or 'always' to create profiles for anonymous users too
    })
  3. Send events

    Optional

    Once installed, PostHog will automatically start capturing events. You can also manually send events to test your integration:

    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' })

    By default, for backwards compatibility reasons, events are sent with person profile processing enabled. This means a person profile will be created for each user who triggers an event.

    If you want to disable person profile processing for certain events, send the event with the following property:

    Javascript
    "$process_person_profile": false
  4. Next steps

    Recommended
    ResourceDescription
    Capturing eventsLearn how to capture custom events beyond autocapture
    Identifying usersAssociate events with specific users
    Creating insightsBuild trends, funnels, and retention charts
    Group analyticsTrack events at the company or account level

Community questions

Was this page useful?

Questions about this page? or post a community question.