1. Integrate
  2. Analytics

Integrate

Analytics

Setting up Analytics with PostHog

  1. PostHog Setup

    • Create a PostHog Account and create a project
    • Go to Project Settings and copy your Project API Key
    • Install the posthog client library
            cd [your_repo_name]
    npm i posthog-js
    
          
  2. Adding PostHog to your app

    Create a layout.js at the root of your project and paste the below, replacing the placeholder text with your project api key.

    /src/routes/+layout.js copy
            
    import posthog from 'posthog-js'
    import { browser } from '$app/environment';
    
    export const load = async () => {
    
      if (browser) {
        posthog.init(
          'REPLACE WITH YOUR PROJECT API KEY',
          {
            api_host: 'https://us.i.posthog.com',
            person_profiles: 'identified_only'
          }
        )
      }
      return
    };
    
          
  3. Checking Events

    • Restart your app and go back to your main landing page, you should start to see page events autocaptured into your PostHog dashboard.
    TIP

    By default user session recording is not enabled. You can go to Project Settings ->Session Replay and toggle on user sessions to see live recordings of users interacting with your site! To further identify users, add A/B testing and feature flags, check out this Tutorial.