Set Up PostHog for GTM Tracking
Set up a PostHog project with proper configuration for GTM tracking
Instructions
Set Up PostHog for GTM Tracking
Prerequisites
- PostHog account (cloud or self-hosted)
- Access to your application codebase
Steps
-
Create a project. Use the PostHog API or dashboard to create a new project named after your product. For separate staging/production environments, create one project per environment. Retrieve the project API key via
GET /api/organizations/<org-id>/projects/or from project settings. -
Install the SDK. Add PostHog to your application:
- Web:
npm install posthog-jsand initialize at your app entry point:posthog.init('<project-api-key>', { api_host: 'https://app.posthog.com' }) - Node:
npm install posthog-nodeand initialize with project API key. - Python:
pip install posthogand setposthog.project_api_key = '<key>'.
- Web:
-
Configure autocapture. PostHog autocaptures pageviews, form submissions, and interactions by default. To reduce noise, pass config options during init:
posthog.init('<key>', { autocapture: { dom_event_allowlist: ['submit'] } })Keep pageviews and form submissions enabled -- they power funnel analysis.
-
Set up user identification. Call
posthog.identify(userId)after login with a stable user ID (not email). Pass user properties:posthog.identify(userId, { plan: 'pro', signup_date: '2025-01-15', company: 'Acme', role: 'founder' })This links anonymous pre-signup activity to the identified user for full-journey visibility.
-
Configure group analytics. For B2B account-level analysis:
posthog.group('company', companyId, { name: 'Acme', plan: 'enterprise', employee_count: 150 })This enables analyzing behavior at the account level, not just per-user.
-
Verify data flow. Use the PostHog MCP
query_eventsoperation or the API (GET /api/projects/<id>/events/?limit=10) to confirm events are arriving. Verify pageviews and identify calls appear with correct user properties in thepropertiesobject.