Generated banner for Integration Angular
Generated logo for Integration Angular

Integration Angular

PostHogOfficialCreated by PostHog

  • Skill
  • Tool integration
  • Data and AI
  • MIT
  • No issues found

From the author

PostHog integration for Angular applications
— the description by PostHog

AutoGineer summary

Written by AutoGineer from the skill’s own files, not by the author

This skill provides a structured approach to integrating the PostHog JavaScript SDK into Angular projects. It emphasizes creating a singleton service to wrap the SDK, ensuring SSR safety, and following best practices for environment configuration and user identification. The integration covers event tracking, session recording, and error monitoring, with specific patterns for modern Angular versions.

What it does

  • Provides a pattern for creating a singleton PostHog service in Angular.
  • Ensures SSR safety when using PostHog in Angular applications.
  • Guides the configuration of environment variables for API keys and hosts.
  • Demonstrates how to identify users during authentication flows.
  • Includes patterns for capturing custom events and tracking errors.
  • Offers guidance on initializing PostHog outside the Angular zone for performance.

When to use it

  • When you need to add product analytics to an Angular application.
  • When you want to implement session replay or error tracking in Angular.
  • When you need to identify users to link frontend events to backend data.
  • When you are setting up a new Angular project and want to include PostHog from the start.

Advantages

  • Promotes a clean, maintainable architecture using singleton services.
  • Prevents performance issues by running PostHog outside the Angular zone.
  • Ensures secure handling of API keys via environment variables.
  • Provides SSR-safe implementation patterns for universal Angular apps.

Usage examples

“How do I initialize PostHog in my Angular app?”
The skill guides you to create a PostHogService singleton and initialize it in the root component, ensuring it is ready before other components render.
“How do I track a user login in Angular with PostHog?”
The skill provides the pattern for using the identify method within your authentication service to link user sessions to specific IDs.

Install

  1. Download the skill as a ZIP:

    https://mp-staging.autogineer.com/agent-skills/posthog/integration-angular/download
  2. Unzip it into the cross-client skills folder of your project:

    mkdir -p .agents/skills && unzip integration-angular.zip -d .agents/skills

Documentation by PostHog

PostHog integration for Angular

This skill helps you add PostHog analytics to Angular applications.

Workflow

Follow these steps in order to complete the integration:

  1. basic-integration-1.0-begin.md - PostHog Setup - Begin ← Start here

  2. basic-integration-1.1-edit.md - PostHog Setup - Edit

  3. basic-integration-1.2-revise.md - PostHog Setup - Revise

  4. basic-integration-1.3-conclude.md - PostHog Setup - Conclusion

Reference files

  • references/EXAMPLE.md - Angular example project code

  • references/angular.md - Angular - docs

  • references/identify-users.md - Identify users - docs

  • references/basic-integration-1.0-begin.md - PostHog setup - begin

  • references/basic-integration-1.1-edit.md - PostHog setup - edit

  • references/basic-integration-1.2-revise.md - PostHog setup - revise

  • references/basic-integration-1.3-conclude.md - PostHog setup - conclusion

The example project shows the target implementation pattern. Consult the documentation for API details.

Key principles

  • Environment variables: Always use environment variables for PostHog keys. Never hardcode them.

  • Minimal changes: Add PostHog code alongside existing integrations. Don't replace or restructure existing code.

  • Match the example: Your implementation should follow the example project's patterns as closely as possible.

Framework guidelines

  • Use inject() instead of constructor injection. PostHog service should be injected via inject() in components/services that need it.

  • Create a dedicated PosthogService as a singleton root service that wraps the PostHog SDK.

  • Always use standalone components over NgModules.

  • Configure PostHog credentials in src/environments/environment.ts files, as Angular reads environment variables from these configuration files

Identifying users

Identify users during login and signup events. Refer to the example code and documentation for the correct identify pattern for this framework. If both frontend and backend code exist, pass the client-side session and distinct ID using X-POSTHOG-DISTINCT-ID and X-POSTHOG-SESSION-ID headers to maintain correlation.

Error tracking

Add PostHog error tracking to relevant files, particularly around critical user flows and API boundaries.

Files

references/angular.md

Angular - Docs

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:

PostHog AI

npm
npm install --save posthog-js
Yarn
yarn add posthog-js
pnpm
pnpm add posthog-js
Bun
bun add posthog-js
Initialize the PostHog client

Generate environment files for your project with ng g environments. Configure the following environment variables:

  • posthogKey: Your project token from your project settings.

  • posthogHost: Your project's client API host. Usually https://us.i.posthog.com for US-based projects and https://eu.i.posthog.com for EU-based projects.

Angular v17+

For Angular v17 and above, you can set up PostHog as a singleton service. To do this, start by creating and injecting a PosthogService instance.

Create a service by running ng g service services/posthog. The service should look like this:

posthog.service.ts

PostHog AI

// src/app/services/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: '2026-01-30',
      });
    });
  }
}

The service is initialized outside of the Angular zone to reduce change detection cycles. This is important to avoid performance issues with session recording.

Then, inject the service in your app's root component app.component.ts. This will make sure PostHog is initialized before any other component is rendered.

app.component.ts

PostHog AI

// src/app/app.component.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) {}
}

Angular v16 and below

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

main.ts

PostHog AI

// src/main.ts
import { bootstrapApplication } from '@angular/platform-browser';
import { appConfig } from './app/app.config';
import { AppComponent } from './app/app.component';
import { environment } from "./environments/environment";
import posthog from 'posthog-js'
posthog.init(environment.posthogKey, {
  api_host: environment.posthogHost,
  defaults: '2026-01-30'
})
bootstrapApplication(AppComponent, appConfig)
  .catch((err) => console.error(err));

Identifying users

Identifying users is required. Call posthog.identify('your-user-id') after login to link events to a known user. This is what connects frontend event captures, session replays, LLM traces, and error tracking to the same person — and lets backend events link back too.

See our guide on identifying users for how to set this up.

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/[email protected] and [email protected] 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, which is free for all PostHog Cloud users, 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.

Add IPs to Firewall/WAF allowlists (recommended)

For certain features like heatmaps, your Web Application Firewall (WAF) may be blocking PostHog’s requests to your site. Add these IP addresses to your WAF allowlist or rules to let PostHog access your site.

EU: 3.75.65.221, 18.197.246.42, 3.120.223.253

US: 44.205.89.55, 52.4.194.122, 44.208.188.173

These are public, stable IPs used by PostHog services (e.g., Celery tasks for snapshots).

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 2026-01-30.

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

PostHog AI

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 followed the installation instructions for Angular v17 and above, you don't need to do anything.

  • If you followed the installation instructions for Angular v16 and below and you see performance impact from recording in an Angular project, ensure that you use ngZone.runOutsideAngular.

posthog.service.ts

PostHog AI

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 */
      )
    })
  }
}

Angular with SSR

To use PostHog with Angular server-side rendering (SSR), you need to:

  1. Update the PostHog web JS client to only initialize on the client-side.

  2. Initialize PostHog Node on the server-side.

1. Update the PostHog web JS client

Update your posthog.service.ts to restrict the initialization of the PostHog web JS client to the client-side. The web SDK uses methods that are not available on the server side, so we need to check if we're on the client side before initializing PostHog.

posthog.service.ts

PostHog AI

import { isPlatformBrowser } from "@angular/common";
import { PLATFORM_ID } from "@angular/core";
import { isPlatformBrowser } from "@angular/common";
import { PLATFORM_ID } from "@angular/core";
@Injectable({ providedIn: "root" })
export class PosthogService {
  constructor(
    private ngZone: NgZone,
    private router: Router,
    private destroyRef: DestroyRef,
    @Inject(PLATFORM_ID) private platformId: Object
  ) {
    // Only initialize PostHog in browser environment
    if (isPlatformBrowser(this.platformId)) {
      this.initPostHog(); //+
    }
2. Add server-side initialization

Angular SSR uses a server.ts file to handle requests. We can add any server-side initialization code to this file.

First, install the posthog-node package to run on the server side.

PostHog AI

npm
npm install posthog-node --save
Yarn
yarn add posthog-node
pnpm
pnpm add posthog-node
Bun
bun add posthog-node

Then, add the following code to the server.ts file:

server.ts

PostHog AI

// src/server.ts
import { environment } from './environments/environment';
import { PostHog } from 'posthog-node'
/**
 * Extract distinct ID from PostHog cookie
 */
function getDistinctIdFromCookie(cookieHeader: string | undefined): string | null {
  if (!cookieHeader) return null;
  const cookieMatch = cookieHeader.match(`ph_${environment.posthogKey}_posthog=([^;]+)`);
  if (cookieMatch) {
    try {
      const parsed = JSON.parse(decodeURIComponent(cookieMatch[1]));
      return parsed?.distinct_id || null;
    } catch (error) {
      console.error('Error parsing PostHog cookie:', error);
      return null;
    }
  }
  return null;
}
/**
 * Handle all other requests by rendering the Angular application.
 */
app.get('**', async (req, res, next) => {
  const { protocol, originalUrl, baseUrl, headers } = req;
  const distinctId = getDistinctIdFromCookie(headers.cookie);
  let isFeatureEnabled = false;
  const client = new PostHog(
      const client = new PostHog(
      environment.posthogKey,
      { host: environment.posthogHost }
  )
  )
  if (distinctId) {
    client.capture({
      distinctId: distinctId,
      event: 'test_ssr_event',
      properties: {
        message: 'Hello from Angular SSR!'
      }
    })
    isFeatureEnabled = await client.isFeatureEnabled(
      'your_feature_flag_key', distinctId) || false;
  }
  commonEngine
    .render({
      bootstrap,
      documentFilePath: indexHtml,
      url: `${protocol}://${headers.host}${originalUrl}`,
      publicPath: browserDistFolder,
      providers: [
        { provide: APP_BASE_HREF, useValue: baseUrl },
        { provide: 'FEATURE_FLAG_ENABLED', useValue: isFeatureEnabled }
      ],
    })
    .then((html) => res.send(html))
    .catch((err) => next(err));
  await client.shutdown()
});

This code does the following:

  • Extracts the distinct ID from the cookie header. This is set by the web JS client.

  • Captures an event on the server side.

  • Evaluates a feature flag on the server side. This can be passed as a provider to the Angular application.

  • Calls shutdown on the PostHog Node client to ensure all events are flushed.

Using PostHog in server-side code

Angular SSR does not allow Node.js code to be bundled into client-side components. Even though resolvers and other server-side code can be written along with client-side components, you cannot use PostHog Node in those components.

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:

Community questions

Ask a question

Was this page useful?

HelpfulCould be better

Requirements and compatibility

Works with: Claude Code, Claude apps, OpenAI Codex, GitHub Copilot, Cursor, Gemini CLI, Kiro, OpenCode, Goose, Amp, Roo Code, JetBrains Junie, Factory Droid, Devin (ex-Windsurf), Mistral Vibe, OpenHands, Letta

“Works with” means the tool reads the open Agent Skills format; a skill written for one tool may use features others lack.

Security

No issues found by our static scan· No issues found

A static scan is not a guarantee. Skills are third-party content: review a skill before you let an agent use it.

Attribution and licence

Created by
PostHog
Publisher
PostHog (Official)
Source
PostHog/skills · skills/posthog/integration/skills/angular · commit a5d30e7
Licence
MIT License (redistribution allowed)
  • LICENSE (added by AutoGineer from LICENSE)
  • ATTRIBUTION.md (written by AutoGineer)
Synced
Also distributed in
PostHog/skills

AutoGineer is not affiliated with or endorsed by PostHog.

Download ZIP

The ZIP holds the skill’s files, its licence files and an ATTRIBUTION.md. Download integration-angular.zip