Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
895686e
feat(tracking): Add analytics tracking module to Common-UI
abhijeetw035 Jun 18, 2025
41adfaa
fix(tracking): fixed some issues
abhijeetw035 Jun 18, 2025
dfe5e5a
fix: added environment
abhijeetw035 Jun 18, 2025
1f22172
fix: fixing memory leak due the subscription to router using rxJs sui…
abhijeetw035 Jun 18, 2025
366b134
fix: added type-check the userId
abhijeetw035 Jun 18, 2025
f9cd2cc
fix: added conditions when label and value is undefined
abhijeetw035 Jun 18, 2025
e6792ab
fix: added error handling and logging menthod
abhijeetw035 Jun 18, 2025
82e90a0
fix: checking for undefined parameter
abhijeetw035 Jun 18, 2025
47b311b
fix: added undefined checking in label and value in ga tracking
abhijeetw035 Jun 18, 2025
864bc6f
fix: using environment based tokens
abhijeetw035 Jun 19, 2025
3f5da09
fix: added enable flag
abhijeetw035 Jun 19, 2025
e40ae47
fix: added error handling cases
abhijeetw035 Jun 19, 2025
7535689
fix(tracking): improve type safety in page view tracking
abhijeetw035 Jun 28, 2025
cd631ff
feat: Added Dynamic matomo Script injection in html and added error h…
Jul 9, 2025
9232b48
feat: Added tracking to registraion form
Jul 9, 2025
c136ff0
feat: added console.error statements at required places
Jul 9, 2025
e5bc4ee
Merge pull request #20 from abhijeetw035/feature/common-ui-tracking-m…
drtechie Jul 21, 2025
48fc251
feat: Added userID function in amrit tracking service
Aug 1, 2025
460e897
fix: amm-1573 search option fix for location feilds in registration m…
snehar-nd Aug 12, 2025
e8b7b19
Merge branch 'release-3.5.0' into sn/dev
snehar-nd Aug 12, 2025
72871ce
fix: circular dependency injection issue in ui analytics
snehar-nd Aug 13, 2025
7602023
fix: amm-1572 added the block and village fields
snehar-nd Aug 13, 2025
20c5ed4
fix: removed commented and unused code
snehar-nd Aug 13, 2025
c2121ed
fix: removed commented code and logs
snehar-nd Aug 13, 2025
1827cdf
Merge pull request #26 from PSMRI/sn/dev
snehar-nd Aug 13, 2025
8548997
feat: Added matomo documentation file
Aug 22, 2025
533dd2f
Fix: Added documentation for Ubuntu
Aug 23, 2025
646c243
Merge branch 'PSMRI:main' into feature/common-ui-tracking-module
abhijeetw035 Aug 23, 2025
3d17432
Merge pull request #25 from abhijeetw035/feature/common-ui-tracking-m…
5Amogh Aug 28, 2025
4f93b9c
Added Tracking Service Documentation
Sep 2, 2025
5360f61
Improved Diagram of Architecture
Sep 2, 2025
cc25ad6
fix(feat): added abha for mmu serviceline too
helenKaryamsetty Sep 3, 2025
d6ef384
feat: AMM-1337 work in progress for feedback module
5Amogh Sep 4, 2025
300acc3
changes mmu abha enabling
helenKaryamsetty Sep 4, 2025
0296793
fix: modified abha check for enabling in mmu
helenKaryamsetty Sep 4, 2025
12edaa3
feat: Added tracking functions to the Google Analytics
Sep 8, 2025
4235e46
feat: amm-1337 work-in progress
5Amogh Sep 9, 2025
b8a0d22
feat: amm-1337 feedback module updation
5Amogh Sep 10, 2025
b17be50
feat: amm-1337 translation added
5Amogh Sep 11, 2025
2655965
Merge pull request #28 from PSMRI/AMM-1337
5Amogh Sep 11, 2025
1219af8
Merge pull request #27 from abhijeetw035/feature/common-ui-tracking-m…
drtechie Sep 30, 2025
5fcc1a4
Merge pull request #29 from PSMRI/release-3.5.0
5Amogh Dec 3, 2025
27a185b
Merge pull request #31 from PSMRI/release-3.9.1
5Amogh Dec 3, 2025
93c9157
platform feedback - close btn added
5Amogh Dec 10, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
307 changes: 307 additions & 0 deletions matomo-documentation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,307 @@
# Matomo Tracking System Documentation

## Table of Contents

1. [Overview](#overview)
2. [How It Works](#how-it-works)
3. [Setup and Configuration](#setup-and-configuration)
4. [Adding Tracking to Components](#adding-tracking-to-components)
5. [Adding a New Website to Matomo](#adding-a-new-website-to-matomo)
6. [API Reference](#api-reference)
7. [Best Practices](#tips-we-can-use-while-adding-tracking)
8. [Troubleshooting](#troubleshooting)

## Overview

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.

### Key Features

- 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

## How It Works

### System Architecture Diagram

![Amrit Tracking System Architecture](src/assets/images/tracking-high-level-diagram.png)

*High-level architecture showing the complete data flow from Angular components to external analytics platforms*

### Architecture Overview

The Amrit tracking system follows a layered architecture pattern:

#### 1. Configuration Layer
- **TrackingModule**: Provides dependency injection configuration
- **Environment Configuration**: Defines platform-specific settings (Matomo/GA)
- **Injection Tokens**: Enable flexible provider configuration

#### 2. Platform Services Layer
- **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

#### 3. Amrit Tracking Interface Layer
- **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.)

#### 4. Application Components Layer
- **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

#### 5. External Analytics Layer
- **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 │
└─────────────────────┘
```

### Component Interaction Flow

1. **Initialization**: `AmritTrackingService` loads the appropriate tracking provider based on environment configuration
2. **Auto-tracking**: Router events are automatically captured for page view tracking
3. **Manual tracking**: Components can call tracking methods for custom events
4. **Provider delegation**: All tracking calls are forwarded to the configured provider (Matomo/GA)

## Setup and Configuration

### 1. Environment Configuration

Add tracking configuration to your environment files:

```typescript
// 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,
},
};
```

### 2. Module Import

Import the tracking module in your app module:

```typescript
// app.module.ts
import { TrackingModule } from "Common-UI/src/tracking";

@NgModule({
imports: [
// ... other imports
TrackingModule.forRoot(),
],
// ...
})
export class AppModule {}
```

## Adding Tracking to Components

### Basic Component Integration

Here's how to add tracking to any Angular component:

```typescript
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");
}
}
```

### Template Integration

Add tracking to template interactions:

```html
<!-- 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>
```

## Adding a New Website to Matomo

### Step 1: Access Matomo Admin Panel

1. Log into your Matomo instance admin panel
2. Navigate to **Administration** → **Websites** → **Manage**

### Step 2: Add New Website

1. Click **"Add a new website"**
2. 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
```

### Step 3: Get Tracking Information

After creating the website, Matomo will provide:

- **Site ID**: A unique number (e.g., 3)
- **Tracking URL**: Your Matomo instance URL

### Step 4: Configure Your Application

Update your environment configuration:

```typescript
// 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/",
},
};
```

### Step 5: Verify Tracking

1. Deploy your application with the new configuration
2. Visit your website
3. Check Matomo dashboard for incoming data
4. Navigate to **Visitors** → **Real-time** to see live visitors


## API Reference

### AmritTrackingService Methods

| 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 |

### Event Categories

| 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` |

### Tips we can use while adding tracking
- <b>Inputs:</b> Can use (focus) to call the tracking function so that it calls only once for the input.

- <b>Dropdowns (mat-select):</b> Can use (selectionChange) to call function to track events.

- <b>Buttons:</b> Can use (click) to call function to track events.
Use the field label plus 'Button' (e.g., 'Advance Search Button').

## Troubleshooting

### Common Issues

1. **Events not appearing in Matomo**

```typescript
// 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
```

2. **Script loading failures**

```typescript
// Check network connectivity to Matomo instance
// Verify CORS settings on Matomo server
// Check Content Security Policy (CSP) headers
```

3. **User ID not being set**

```typescript
// Verify session storage service is working
const userId = this.sessionStorage.getItem("userID");
console.log("Retrieved User ID:", userId);

### Support

For additional support:

1. Check Matomo documentation: https://matomo.org/docs/
2. Review browser developer tools for errors
3. Test with Matomo's real-time visitor log
4. Verify network requests to tracking endpoint

---

_Last updated: September 2025_
Loading
Loading