Skip to content

platform-analytics

Provider-agnostic analytics and error tracking module supporting multiple backends. Ships with Sentry, Azure Application Insights, and Datadog clients out of the box, plus a no-op fallback for environments where analytics is disabled.

Package: @breadstone/archipel-platform-analytics

npm install @breadstone/archipel-platform-analytics

Quick Start

typescript
import { Module } from '@nestjs/common';
import { AnalyticsModule } from '@breadstone/archipel-platform-analytics';
import { SentryAnalyticsClient, SENTRY_CONFIG_ENTRIES } from '@breadstone/archipel-platform-analytics/sentry';

@Module({
  imports: [
    AnalyticsModule.register({
      analyticsClient: SentryAnalyticsClient,
      configEntries: SENTRY_CONFIG_ENTRIES,
      isGlobal: true,
    }),
  ],
})
export class AppModule {}

Module Configuration

IAnalyticsModuleOptions

PropertyTypeRequiredDefaultDescription
analyticsClientType<AnalyticsClientPort>YesConcrete analytics client implementation
configEntriesReadonlyArray<IConfigRegistryEntry>No[]Provider-specific configuration entries
isGlobalbooleanNofalseRegister as a global module

AnalyticsClientPort API

The abstract port that all analytics clients implement. Inject it anywhere:

typescript
import { AnalyticsClientPort } from '@breadstone/archipel-platform-analytics';

@Injectable()
export class PaymentService {
  constructor(private readonly _analytics: AnalyticsClientPort) {}

  public async processPayment(): Promise<void> {
    try {
      // ... payment logic
    } catch (error) {
      this._analytics.captureException(error as Error, { feature: 'payments' });
    }
  }
}

Methods

MethodSignatureDescription
captureException(error: Error, context?: Record<string, unknown>) => voidCapture an error with optional context
captureMessage(message: string, level: AnalyticsLevel, context?: Record<string, unknown>) => voidCapture a message at a severity level
setUser(user: IAnalyticsUser) => voidSet the current user context
clearUser() => voidClear the current user context
addBreadcrumb(breadcrumb: IAnalyticsBreadcrumb) => voidAdd a breadcrumb to the analytics trail

Built-in Providers

SentryAnalyticsClient

Error tracking and performance monitoring via Sentry.

typescript
import { SentryAnalyticsClient, SENTRY_CONFIG_ENTRIES } from '@breadstone/archipel-platform-analytics/sentry';

AnalyticsModule.register({
  analyticsClient: SentryAnalyticsClient,
  configEntries: SENTRY_CONFIG_ENTRIES,
});

Environment variables: SENTRY_DSN, SENTRY_ENVIRONMENT

AppInsightsAnalyticsClient

Azure Application Insights integration.

typescript
import {
  AppInsightsAnalyticsClient,
  APPINSIGHTS_CONFIG_ENTRIES,
} from '@breadstone/archipel-platform-analytics/appinsights';

AnalyticsModule.register({
  analyticsClient: AppInsightsAnalyticsClient,
  configEntries: APPINSIGHTS_CONFIG_ENTRIES,
});

Environment variables: APPLICATIONINSIGHTS_CONNECTION_STRING

DatadogAnalyticsClient

Datadog APM and error tracking.

typescript
import { DatadogAnalyticsClient, DATADOG_CONFIG_ENTRIES } from '@breadstone/archipel-platform-analytics/datadog';

AnalyticsModule.register({
  analyticsClient: DatadogAnalyticsClient,
  configEntries: DATADOG_CONFIG_ENTRIES,
});

Environment variables: DD_SERVICE, DD_ENV

NoopAnalyticsClient

Silent no-op client for environments where analytics is disabled:

typescript
import { NoopAnalyticsClient } from '@breadstone/archipel-platform-analytics';

AnalyticsModule.register({
  analyticsClient: NoopAnalyticsClient,
});

Sub-path Imports

Each provider is tree-shakeable via sub-path imports:

Import PathContents
@breadstone/archipel-platform-analyticsCore module, port, models, NoopAnalyticsClient
@breadstone/archipel-platform-analytics/sentrySentryAnalyticsClient, SENTRY_CONFIG_ENTRIES
@breadstone/archipel-platform-analytics/appinsightsAppInsightsAnalyticsClient, APPINSIGHTS_CONFIG_ENTRIES
@breadstone/archipel-platform-analytics/datadogDatadogAnalyticsClient, DATADOG_CONFIG_ENTRIES

Lifecycle & Shutdown

All built-in clients implement OnModuleInit and OnModuleDestroy:

PhaseBehavior
InitSDK initialization is deferred to onModuleInit() — no constructor I/O
DestroyPending events are flushed and the SDK is closed with a 5-second timeout

This ensures no events are lost during application shutdown and prevents hanging processes if the analytics backend is unreachable.


Health Check

The AnalyticsHealthIndicator verifies that an AnalyticsClientPort is injected. If no analytics client is registered, it reports disabled: true. Import it from the /health subpath:

typescript
import { AnalyticsHealthIndicator } from '@breadstone/archipel-platform-analytics/health';
import { HealthModule } from '@breadstone/archipel-platform-health';

@Module({
  imports: [
    AnalyticsModule.register({ /* ... */ }),
    HealthModule.withIndicators([AnalyticsHealthIndicator]),
  ],
})
export class AppModule {}
KeyCheckDependencies
analyticsup if AnalyticsClientPort injected, else disabled@Optional() AnalyticsClientPort

Exports Summary

ExportTypeDescription
AnalyticsModuleNestJS ModuleMain module with register()
AnalyticsClientPortPortAbstract analytics contract
NoopAnalyticsClientClientNo-op fallback implementation
SentryAnalyticsClientClientSentry provider (sub-path)
AppInsightsAnalyticsClientClientAzure Application Insights provider (sub-path)
DatadogAnalyticsClientClientDatadog provider (sub-path)
AnalyticsLevelTypeSeverity level union
IAnalyticsBreadcrumbInterfaceBreadcrumb entry model
IAnalyticsUserInterfaceUser context model
AnalyticsHealthIndicatorHealthAnalytics readiness check (/health subpath)

Released under the MIT License.