Rajdip Khavad Logo
Rajdip Khavad
Back to all articles
Engineering

Building a Centralized Analytics Service in Angular

Rajdip Khavad•2026-09-01•8 min read

# Building a Centralized Analytics Service in Angular

Analytics integrations often start with a few calls inside components and gradually become difficult to maintain.

An Angular application may eventually integrate Google Tag Manager, GA4, Klaviyo and other providers. If every component communicates directly with every provider, the application becomes tightly coupled to analytics infrastructure.

A centralized analytics service provides a cleaner architecture.

1. The Problem With Distributed Tracking

Without centralization:

text
SearchComponent → GTM
SearchComponent → Klaviyo

HotelComponent → GTM
HotelComponent → Klaviyo

CheckoutComponent → GTM
CheckoutComponent → Klaviyo

This creates duplicated provider logic.

A centralized approach becomes:

📐 Architecture Diagram
SearchComponent ──┐
HotelComponent ───┼──> AnalyticsService
CheckoutComponent ┘          |
                             +--> GTM / GA4
                             |
                             +--> Klaviyo

2. Define Business-Level Methods

Components should communicate business actions:

ts
analyticsService.trackSearch(data);
analyticsService.trackHotelView(data);
analyticsService.trackCheckout(data);
analyticsService.trackPurchase(data);

They should not need to know how those actions are implemented by each provider.

3. Keep Provider Contracts Internal

The service can translate an application event into different provider formats.

📐 Architecture Diagram
Business Event
      |
      v
AnalyticsService
      |
      +----> GA4 event
      |
      +----> Klaviyo event

This avoids forcing the Angular domain model to match an external analytics schema.

4. Example Service Responsibilities

A centralized service can own:

  • GTM/dataLayer access
  • GA4 ecommerce payloads
  • Klaviyo identification
  • Klaviyo event dispatch
  • attribution handling
  • ecommerce state clearing
  • purchase deduplication
  • tracker initialization
  • internal-user identification
  • 5. Why This Matters in a Hotel Booking Platform

    A hotel booking funnel contains many interconnected actions.

    📐 Architecture Diagram
    Search
     ↓
    Results
     ↓
    Hotel
     ↓
    Rooms
     ↓
    Selection
     ↓
    Checkout
     ↓
    Payment
     ↓
    Purchase

    Having one service understand these analytics contracts makes the tracking architecture easier to review and evolve.

    6. Make Purchase Tracking Idempotent

    The analytics service is a natural place for transaction deduplication.

    ts
    if (alreadyTracked(bookingId)) {
      return;
    }
    
    trackPurchase(booking);
    markAsTracked(bookingId);

    This keeps duplicate protection out of the booking UI.

    7. Keep Attribution in the Analytics Layer

    Attribution is cross-cutting application state.

    The service can:

    📐 Architecture Diagram
    Capture
      ↓
    Normalize
      ↓
    Persist
      ↓
    Retrieve
      ↓
    Attach/synchronize

    This avoids implementing attribution logic separately in search, hotel and checkout components.

    8. Provider Loading

    The service can also own dynamic provider initialization.

    For Klaviyo, the implementation uses a dynamic proxy/queue through ensureKlaviyoProxy().

    This means components do not need to know whether the provider script has already loaded.

    9. Testing

    A centralized service creates a clear testing boundary.

    Test:

    📐 Architecture Diagram
    Business action
          ↓
    Analytics method
          ↓
    Expected provider payload

    This is easier than testing provider-specific logic across many UI components.

    10. Common Mistakes

  • Calling providers directly from components.
  • Creating different event names for the same business action.
  • Passing complete application objects to providers.
  • Mixing tracking state with UI state.
  • Implementing purchase deduplication in multiple places.
  • Loading providers independently from multiple components.
  • 11. Recommended Structure

    text
    core/
      services/
        analytics.service.ts

    The exact project structure can vary, but the principle is to place cross-cutting analytics infrastructure in a central application layer.

    12. Conclusion

    A centralized analytics service creates a stable boundary between Angular business logic and external analytics platforms.

    It improves consistency, reduces duplication and makes complex requirements such as ecommerce state management, purchase deduplication, attribution and dynamic provider loading much easier to control.

    Related Content

  • Proven Engineering

    Need enterprise architecture consulting?

    Explore our real-world case studies detailing production migrations, performance scaling, and high-throughput gateways.

    View Case Studies