Matomo Client for Angular (ngx-matomo-client)
ngx-matomo-client is a robust, actively maintained library providing seamless integration of Matomo (formerly Piwik) analytics into Angular applications. The current stable version is 9.0.1, offering compatibility with Angular 21. The library generally follows Angular's release cadence, providing updates for new major Angular versions, typically within weeks of their release. Key differentiators include its focus on privacy-respecting analytics, explicit support for Angular's module-less applications (since v8.0.0), and the ability to run tracking operations outside Angular's change detection zone for improved performance and stability. This package consolidates functionality previously split across `@ngx-matomo/tracker` and `@ngx-matomo/router` since version 5, simplifying usage and reducing overhead for developers integrating Matomo with Angular's routing.
Common errors
-
NG0201: No provider for MatomoService!
cause The MatomoService has not been properly provided at the application root or the relevant feature module.fixEnsure `provideMatomo()` is called in your `main.ts` or `app.config.ts` (for standalone applications) or `MatomoModule.forRoot()` (for legacy module-based applications) is imported in your root module. -
Can't resolve '@angular/common' in 'node_modules/ngx-matomo-client'
cause A mismatch exists between the `ngx-matomo-client` version and your Angular version, leading to an incompatible peer dependency.fixCheck the `ngx-matomo-client` README's compatibility table and align your `ngx-matomo-client` package version with your Angular version. For example, if on Angular 21, use `ngx-matomo-client@9`. -
Property 'trackPageView' does not exist on type 'MatomoService'.
cause You might be using an older version of the library or have a corrupted `node_modules` installation, or there's a TypeScript type inference issue.fixEnsure you are using `ngx-matomo-client` (not legacy `@ngx-matomo/tracker`). Verify your `ngx-matomo-client` version is up-to-date and run `npm install` or `yarn install` to refresh dependencies. Check your `tsconfig.json` for proper type resolution. -
ERROR TypeError: Cannot read properties of undefined (reading 'trackPageView')
cause The Matomo tracker instance (window._paq) might not be initialized when `MatomoService` methods are called, often due to deferred configuration or incorrect initialization timing.fixEnsure Matomo is initialized before calling tracking methods. For deferred or manual initialization, use `MatomoInitializerService.initialize()` once the necessary configuration (e.g., from an API) is available. For standard setup, ensure `provideMatomo` is correctly configured.
Warnings
- breaking The package structure changed significantly in v5. The library was consolidated from `@ngx-matomo/tracker` and `@ngx-matomo/router` into a single `ngx-matomo-client` package. Direct imports from the old packages will fail.
- breaking Angular module-based setup (e.g., `MatomoModule.forRoot()`) is largely superseded by standalone `provideMatomo` since v8.0.0, aligning with modern Angular practices. While legacy modules might still work for compatibility, the recommended approach is the standalone API.
- breaking Compatibility with Angular versions is strict. Each major `ngx-matomo-client` version typically supports a specific major Angular version. For instance, v9.x requires Angular 21, v8.x requires Angular 20, and so on. Mismatched versions will lead to peer dependency errors.
- gotcha Since v9.0.0, Matomo tracking runs outside Angular's zone by default. This is generally a performance improvement but might affect debugging or change detection if you rely on tracker side effects to trigger Angular updates without manual intervention.
- gotcha Incorrect `trackerUrl` or `siteId` configuration will result in Matomo tracking calls failing silently or reporting to the wrong Matomo instance. This is a common setup error.
Install
-
npm install ngx-matomo-client -
yarn add ngx-matomo-client -
pnpm add ngx-matomo-client
Imports
- provideMatomo
import { provideMatomo } from '@ngx-matomo/tracker';import { provideMatomo } from 'ngx-matomo-client'; - MatomoService
import { MatomoService } from '@ngx-matomo/tracker';import { MatomoService } from 'ngx-matomo-client'; - MatomoRouterInitializer
import { NgxMatomoRouterModule } from '@ngx-matomo/router';import { MatomoRouterInitializer } from 'ngx-matomo-client';
Quickstart
import { Component, OnInit } from '@angular/core';
import { MatomoService, provideMatomo, withRouter } from 'ngx-matomo-client';
import { bootstrapApplication } from '@angular/platform-browser';
import { provideRouter, RouterOutlet } from '@angular/router';
@Component({
selector: 'app-root',
standalone: true,
imports: [RouterOutlet],
template: `
<h1>Welcome to Matomo Tracking!</h1>
<p>Check your Matomo dashboard for tracking data.</p>
<button (click)="trackCustomEvent()">Track Custom Event</button>
`
})
export class AppComponent implements OnInit {
constructor(private matomoService: MatomoService) {}
ngOnInit(): void {
// Manually track a page view if not using router integration
// this.matomoService.trackPageView();
}
trackCustomEvent(): void {
this.matomoService.trackEvent('UserInteraction', 'ButtonClick', 'CustomButton');
console.log('Custom event tracked!');
}
}
bootstrapApplication(AppComponent, {
providers: [
provideRouter([]),
provideMatomo({
trackerUrl: 'https://your-matomo-domain.com/matomo.php',
siteId: 1
}, withRouter())
]
});