Angular

Last updated:

|Edit this page|

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:

Terminal
yarn add posthog-js
# or
npm install --save posthog-js

In your src/main.ts, initialize PostHog using your project API key and instance address. You can find both in your project settings.

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: '2025-05-24'
})
bootstrapApplication(AppComponent, appConfig)
.catch((err) => console.error(err));

Note: If you're using Typescript, you might have some trouble getting your types to compile because we depend on rrweb but don't ship all of their types. To accommodate that, you'll need to add @rrweb/types@2.0.0-alpha.17 and rrweb-snapshot@2.0.0-alpha.17 as a dependency if you want your Angular compiler to typecheck correctly.

Given the nature of this library, you might need to completely clear your .npm cache 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.json to understand what's the exact version you need to depend on.

Set up a reverse proxy (recommended)

We recommend setting up a reverse proxy, so that events are less likely to be intercepted by tracking blockers.

We have our own managed reverse proxy service included in the platform add-ons, which routes through our infrastructure and makes setting up your proxy easy.

If you don't want to use our managed service then there are several other options for creating a reverse proxy, including using Cloudflare, AWS Cloudfront, and Vercel.

Grouping products in one project (recommended)

If you have multiple customer-facing products (e.g. a marketing website + mobile app + web app), it's best to install PostHog on them all and group them in one project.

This makes it possible to track users across their entire journey (e.g. from visiting your marketing website to signing up for your product), or how they use your product across multiple platforms.

Tracking pageviews

PostHog automatically tracks your pageviews by hooking up to the browser's navigator API as long as you initialize PostHog with the defaults config option set after 2025-05-24.

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:

app.component.ts
import { Component } from '@angular/core';
import posthog from 'posthog-js'
@Component({
// existing component code
})
export class AppComponent {
handleClick() {
posthog.capture(
'home_button_clicked',
)
}
}

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.

posthog.service.ts
import { Injectable } from '@angular/core';
import posthog from 'posthog-js'
@Injectable({ providedIn: 'root' })
export class PostHogSessionRecordingService {
constructor(private ngZone: NgZone) {}
initPostHog() {
this.ngZone.runOutsideAngular(() => {
posthog.init(
/* your config */
)
})
}
}

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:

main.ts
import { Component } from "@angular/core";
import { RouterOutlet } from "@angular/router";
import { PosthogService } from "./services/posthog.service";
@Component({
selector: "app-root",
styleUrls: ["./app.component.scss"],
template: `
<router-outlet />`,
imports: [RouterOutlet],
})
export class AppComponent {
title = "angular-app";
constructor(posthogService: PosthogService) {}
}

The service itself looks like this:

posthog.service.ts
import { DestroyRef, Injectable, NgZone } from "@angular/core";
import posthog from "posthog-js";
import { environment } from "../../environments/environment";
import { Router } from "@angular/router";
@Injectable({ providedIn: "root" })
export class PosthogService {
constructor(
private ngZone: NgZone,
private router: Router,
private destroyRef: DestroyRef,
) {
this.initPostHog();
}
private initPostHog() {
this.ngZone.runOutsideAngular(() => {
posthog.init(environment.posthogKey, {
api_host: environment.posthogHost,
defaults: '2025-05-24'
debug: false,
cross_subdomain_cookie: true,
});
});
}
}

This service contains PostHog initialization.

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:

Questions? Ask Max AI.

It's easier than reading through 661 pages of documentation

Community questions

Was this page useful?

Next article

Astro

PostHog makes it easy to get data about traffic and usage of your Astro 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 Astro app using the JavaScript Web SDK . Installation Next steps For any technical questions for how to integrate specific PostHog features into Astro (such as analytics, feature flags, A/B testing, surveys, etc.), have…

Read next article