PostHog makes it easy to get data about traffic and usage of your Angular app. Integrating PostHog into your site enables analytics about user behavior, custom events capture, session recordings, feature flags, and more.
This guide walks you through integrating PostHog into your Angular app using the JavaScript Web SDK.
Installation
Install posthog-js using your package manager:
In your src/main.ts, initialize PostHog using your project API key and instance address. You can find both in your project settings.  
Note: If you're using Typescript, you might have some trouble getting your types to compile because we depend on
rrwebbut don't ship all of their types. To accommodate that, you'll need to add@rrweb/types@2.0.0-alpha.17andrrweb-snapshot@2.0.0-alpha.17as a dependency if you want your Angular compiler to typecheck correctly.Given the nature of this library, you might need to completely clear your
.npmcache to get this to work as expected. Make sure your clear your CI's cache as well.In the rare case the versions above get out-of-date, you can check our JavaScript SDK's
package.jsonto understand what's the exact version you need to depend on.
Capture custom events
To capture custom events, import posthog and call posthog.capture(). Below is an example of how to do this in a component:
Tracking pageviews
PostHog only captures pageview events when a page load is fired. Since Angular creates a single-page app, this only happens once, and the Angular router handles subsequent page changes.
If we want to capture every route change, we must write code to capture pageviews that integrates with the router.
To start, we need to add capture_pageview: false to the PostHog initialization to avoid double capturing the first pageview.
Next, depending on your Angular version, add the following code to track pageviews:
Tracking pageviews in Angular v17 and above
Import the following in app.component.ts:
- posthog
- Router,- Event, and- NavigationEndfrom- @angular/router
- Observablefrom- rxjsand- filterfrom- rxjs/operators
With these, we set up a subscription to the router events that captures a $pageview event on every NavigationEnd event:
Tracking pageviews in Angular v16 and below
To track pageviews in Angular v16 and below, import posthog into app-routing.module.ts, subscribe to router events and then capture $pageview events on NavigationEnd events:
Now, every time a user moves between pages, PostHog captures a $pageview event, not just on the first page load.
Capturing pageleaves
Setting capture_pageview: false also disables automatic pageleave capture. To re-enable this, add capture_pageleave: true to your PostHog initialization.
Session replay
Session replay uses change detection to record the DOM. This can clash with Angular's change detection.
The recorder tool attempts to detect when an Angular zone is present and avoid the clash but might not always succeed.
If you see performance impact from recording in an Angular project, ensure that you use ngZone.runOutsideAngular. 
Idiomatic service for Angular v17 and above
For larger projects, you might prefer to set up PostHog as a singleton service. To do this, start by creating and injecting a PosthogService instance:
The service itself looks like this:
This service contains PostHog initialization, and upon success, enables pageviews.
Next steps
For any technical questions for how to integrate specific PostHog features into Angular (such as feature flags, A/B testing, surveys, etc.), have a look at our JavaScript Web SDK docs.
Alternatively, the following tutorials can help you get started: