This document details the platform.analytics library. The module is built using Angular and Nx tooling, and it supports Google Analytics integration along with additional tracking functionalities for your application.
The platform.analytics package is designed to centralize analytics functionality such as tracking events, page views, and custom metrics using Google Analytics. It is divided into primary and secondary entry points:
Key features include:
The analytics package is organized as follows:
.eslintrc.json, jest.config.ts, ng-package.json, package.json, project.json, and various tsconfig files.google-analytics.event.directive.tsgoogle-analytics.event-category.directive.tsgoogle.analytics.router.initialiser.ts).common.google-analytics.4.action.event-name.ts).zwp.google-analytics.module.tszwp.google-analytics.router.module.tsgoogle-analytics.service.tsforRoot() method to pass in settings (ZWPGoogleAnalyticsSettings) and register the initialiser (ZWP_GOOGLE_ANALYTICS_INITIALISER).forRoot() method with optional routing settings (ZWPGoogleAnalyticsRoutingSettings).zwpGoogleAnalyticsEvent or zwpGoogleAnalyticsAction to specify the event name.zwpGoogleAnalyticsLabel and zwpGoogleAnalyticsValue for additional metadata.zwpGoogleAnalyticsNativeEvent allows customization of the native event (defaults to ‘click’).ngOnDestroy.zwpGoogleAnalyticsEventCategory to set a string category for analytics events.Both directives are aggregated and exported in the directives index.
event(...): Dispatches an event with parameters for action name, category, label, and value.pageView(path: string, title?: string, location?: string, options?: object): Sends a page view event to Google Analytics.exception(description: string, fatal?: boolean): Reports exceptions.appView(...): For app-specific views with additional context like app version.constructOptionsDict to build parameter objects. It relies on a GTag builder function (ZWP_GOOGLE_ANALYTICS_GTAG_BUILDER) for constructing and sending commands.trackingCode: The Google Analytics tracking code.initCommands: Commands to initialize tracking.initDelay, uri, enableTracing, and nonce.include and exclude rules for capturing router events.ZWPCommonGoogleAnalytics4ActionEventName and others from the auto, ecommerce, and games categories). These ensure consistent naming across the application..eslintrc.json and Prettier configurations.jest.config.ts and tsconfig.spec.json. The test-setup.ts file initializes necessary mocks and environment configurations.nx build platform.analyticsnx test platform.analyticsnx lint platform.analytics nx build platform.analytics
nx test platform.analytics
import { ZWPGoogleAnalyticsModule } from '@zwp/platform.analytics/google';
@NgModule({
imports: [
ZWPGoogleAnalyticsModule.forRoot({
trackingCode: 'UA-XXXXXXX-X',
initCommands: [{ command: 'config', values: ['UA-XXXXXXX-X'] }],
enableTracing: false
}),
// or for routing integration:
ZWPGoogleAnalyticsRouterModule.forRoot({
include: [/^\/dashboard/],
exclude: ['/login']
})
]
})
export class AppModule { }
The platform.analytics package offers an extensible approach to integrating analytics within your Angular applications by leveraging Google Analytics. It provides: