README
google-analytics
A bridge package between Angular application and Google Analytics API (gtag.js
).
Features
- Page view tracking
- Custom event tracking
- User timing tracking
[clAnalyticsOn]
directive for tracking DOM events
Install
yarn add @classi/ngx-google-analytics
Usage
Setup
0. Install gtag.js
ngx-google-analytics depends on window.gtag
variable.
Setup gtag.js following offitial guide: https://developers.google.com/analytics/devguides/collection/gtagjs
<!-- Global site tag (gtag.js) - Google Analytics -->
<script
async
src="https://www.googletagmanager.com/gtag/js?id=GA_MEASUREMENT_ID"
></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag() {
dataLayer.push(arguments);
}
gtag('js', new Date());
gtag('config', 'GA_MEASUREMENT_ID');
</script>
GoogleAnalyticsModule
into root NgModule.
1. Install import { GoogleAnalyticsModule } from '@classi/ngx-google-analytics';
@NgModule({
imports: [GoogleAnalyticsModule.forRoot(options)],
})
export class AppModule {}
GoogleAnalyticsModule.forRoot
Options
disableTracking?: boolean
: Disable all tracking. Usually for development.
2. Start tracking in bootstrapping
To track all page view events with Angular router, start tracking before the initial navigation.
Typically, the best place to start tracking is the constructor of AppComponent
.
export class AppComponent {
constructor(private readonly analytics: AnalyticsTracker) {
this.analytics.startTracking();
}
}
AnalyticsTracker
The central API of @classi/ngx-google-analytics
.
setUserContext(user)
/ clearUserContext()
Set user context: export class AppComponent {
constructor(private readonly analytics: AnalyticsTracker) {
this.analytics.startTracking();
}
setUserId(userId: string) {
this.analytics.setUserContext({ id: userId });
this.analytics.clearUserContext();
}
}
setCustomDimensions(dimensions)
Set custom dimensions: export class AppComponent {
constructor(private readonly analytics: AnalyticsTracker) {
this.analytics.startTracking();
}
setCustomDimensions(dimensions: Record<string, string>) {
this.analytics.setCustomDimensions(dimensions);
}
}
sendEvent(event)
Send events: export class AppComponent {
constructor(private readonly analytics: AnalyticsTracker) {
this.analytics.startTracking();
}
sendCustomEvent() {
this.analytics.sendEvent({
name: 'click',
// optional
params: {
event_category: 'test',
event_label: 'foobar',
value: 100,
},
});
}
}
sendUserTiming(eventFields)
Send user timing: export class AppComponent {
constructor(private readonly analytics: AnalyticsTracker) {
this.analytics.startTracking();
}
sendUserTiming() {
const before = window.performance.now();
doSomething().then(() => {
const after = window.performance.now();
this.analytics.sendUserTiming({
name: 'something',
params: {
value: Month.round(after - before), // * required
event_category: 'test',
event_label: 'foobar',
},
});
});
}
}
: AnalyticsOnDirective<button clAnalyticsOn>
A directive to bind single DOM event and event tracking.
Import GoogleAnalyticsModule
to enable it.
@NgModule({
imports: [GoogleAnalyticsModule],
})
export class SomeModule {}
<button clAnalyticsOn="click" [analyticsEvent]="clickEvent">Buy</button>
clAnalyticsOn="{{eventName}}"
: Set a DOM event to track.[analyticsEvent]="event"
: Set event fields. See details insendEvent
document.
Development
- Run tests:
nx test google-analytics