- Overview
- How It Works
- Setup and Configuration
- Adding Tracking to Components
- Adding a New Website to Matomo
- API Reference
- Best Practices
- Troubleshooting
The Amrit tracking system is a flexible analytics solution that supports multiple tracking providers (Matomo, Google Analytics) with a unified interface. It automatically tracks page views, user interactions, and custom events throughout your Angular application.
- Multiple provider support (Matomo, Google Analytics)
- Automatic page view tracking
- User identification tracking
- Environment-based configuration
- Built-in error handling and fallbacks
- Rich event tracking capabilities
High-level architecture showing the complete data flow from Angular components to external analytics platforms
The Amrit tracking system follows a layered architecture pattern:
- TrackingModule: Provides dependency injection configuration
- Environment Configuration: Defines platform-specific settings (Matomo/GA)
- Injection Tokens: Enable flexible provider configuration
- Matomo Tracking Service: Handles Matomo-specific tracking implementation
- Google Analytics Tracking Service: Handles GA-specific tracking implementation
- Provider Selection: Dynamically selects the appropriate service based on configuration
- Router Integration: Automatic page view tracking via Angular Router events
- Session Management: User session and ID management
- Error Handling: Graceful fallback mechanisms
- Method Abstraction: Unified tracking methods (trackEvent, trackButtonClick, etc.)
- Manual/Automatic Tracking: Components can trigger tracking events
- User Session Layer: Provides user ID for session tracking
- Enabled/Disable Flag: Conditional tracking based on environment settings
- Matomo Server: Receives HTTP tracking data via matomo.php endpoint
- Google Analytics: Receives tracking data via GA tracking endpoints
┌─────────────────────┐ ┌──────────────────────┐ ┌─────────────────────┐
│ Angular Component │───▶│ AmritTrackingService │───▶│ TrackingProvider │
└─────────────────────┘ └──────────────────────┘ │ (Matomo/GA) │
│ └─────────────────────┘
▼ │
┌──────────────────────┐ ▼
│ Router Events │ ┌─────────────────────┐
│ (Auto Page Tracking)│ │ External Analytics │
└──────────────────────┘ │ Platform │
└─────────────────────┘
- Initialization:
AmritTrackingServiceloads the appropriate tracking provider based on environment configuration - Auto-tracking: Router events are automatically captured for page view tracking
- Manual tracking: Components can call tracking methods for custom events
- Provider delegation: All tracking calls are forwarded to the configured provider (Matomo/GA)
Add tracking configuration to your environment files:
// src/environments/environment.ts
export const environment = {
// ... other config
tracking: {
platform: "matomo", // or 'ga'
siteId: 1,
trackerUrl: "https://matomo.piramalswasthya.org/",
trackingPlatform: "platform",
enabled: true,
},
};Import the tracking module in your app module:
// app.module.ts
import { TrackingModule } from "Common-UI/src/tracking";
@NgModule({
imports: [
// ... other imports
TrackingModule.forRoot(),
],
// ...
})
export class AppModule {}Here's how to add tracking to any Angular component:
import { Component } from "@angular/core";
import { AmritTrackingService } from "@common-ui/tracking";
@Component({
selector: "app-user-registration",
templateUrl: "./user-registration.component.html",
})
export class UserRegistrationComponent {
constructor(private trackingService: AmritTrackingService) {}
// Track button clicks
onSubmitRegistration() {
this.trackingService.trackButtonClick("Submit Registration");
this.trackingService.trackFormSubmit("User Registration Form");
}
// Track field interactions
trackFieldInteraction(fieldName: string) {
this.trackingService.trackFieldInteraction(fieldName, "Facility Selection");
}
}Add tracking to template interactions:
<!-- user-registration.component.html -->
<form (ngSubmit)="onSubmitRegistration()">
<input
type="text"
placeholder="Username"
(focus)="trackFieldInteraction('username')"
/>
<input
type="email"
placeholder="Email"
(focus)="trackFieldInteraction('email')"
/>
<button
type="button"
(click)="trackFieldInteraction('Show Password Requirements')"
>
Show Requirements
</button>
<button type="submit" (click)="trackFieldInteraction('Registration Form')">
Register
</button>
</form>- Log into your Matomo instance admin panel
- Navigate to Administration → Websites → Manage
- Click "Add a new website"
- Fill in the website details:
Website Name: Your Website Name Website URL: https://yourwebsite.com Time zone: Select appropriate timezone Currency: Select currency if e-commerce tracking needed
After creating the website, Matomo will provide:
- Site ID: A unique number (e.g., 3)
- Tracking URL: Your Matomo instance URL
Update your environment configuration:
// src/environments/environment.prod.ts
export const environment = {
tracking: {
enabled: true,
platform: "matomo",
siteId: 3, // The new Site ID from Matomo
trackerUrl: "https://matomo.piramalswasthya.org/",
},
};- Deploy your application with the new configuration
- Visit your website
- Check Matomo dashboard for incoming data
- Navigate to Visitors → Real-time to see live visitors
| Method | Parameters | Description |
|---|---|---|
trackEvent() |
category: string, action: string, label?: string, value?: number |
Track custom events |
trackButtonClick() |
buttonName: string |
Track button interactions |
trackFormSubmit() |
formName: string |
Track form submissions |
trackFeatureUsage() |
featureName: string |
Track feature utilization |
trackFieldInteraction() |
fieldName: string, category?: string |
Track form field interactions |
setUserId() |
userId: string | number |
Set user identifier for tracking |
| Category | Purpose | Example Actions |
|---|---|---|
UI |
User interface interactions | ButtonClick, MenuOpen, TabSwitch |
Form |
Form-related activities | Submit, Validation Error, Field Focus |
Feature |
Feature usage tracking | Usage, Enable, Configure |
Registration |
User registration flow | Field Interaction, Step Complete, Validation |
Navigation |
Page and route changes | Page View, Route Change, Back Button |
-
Inputs: Can use (focus) to call the tracking function so that it calls only once for the input.
-
Dropdowns (mat-select): Can use (selectionChange) to call function to track events.
-
Buttons: Can use (click) to call function to track events. Use the field label plus 'Button' (e.g., 'Advance Search Button').
-
Events not appearing in Matomo
// Check if tracking is enabled console.log("Tracking enabled:", environment.tracking.enabled); // Verify site ID and URL console.log("Site ID:", environment.tracking.siteId); console.log("Tracker URL:", environment.tracking.trackerUrl); // Check browser console for errors
-
Script loading failures
// Check network connectivity to Matomo instance // Verify CORS settings on Matomo server // Check Content Security Policy (CSP) headers
-
User ID not being set
// Verify session storage service is working const userId = this.sessionStorage.getItem("userID"); console.log("Retrieved User ID:", userId);
For additional support:
- Check Matomo documentation: https://matomo.org/docs/
- Review browser developer tools for errors
- Test with Matomo's real-time visitor log
- Verify network requests to tracking endpoint
Last updated: September 2025