An easy implementation to track ga on angular6+ apps.
npm install @khoa002/ngx-google-analytics-sdkAn easy implementation to track ga on angular6+ apps.
@TODO:
* Create unit tests;
```
npm install ngx-google-analytics
https://github.com/maxandriani/ngx-google-analytics
`ts
import { NgxGoogleAnalyticsModule } from 'ngx-google-analytics';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
NgxGoogleAnalyticsModule.forRoot('traking-code')
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
`
`ts
import { Component } from '@angular/core';
import { GoogleAnalyticsService } from 'ngx-google-analytics';
@Component( ... )
export class TestFormComponent {
constructor(
protected $gaService: GoogleAnalyticsService
) {}
onUserInputName() {
...
this.$gaService.event('enter_name', 'user_register_form', 'Name');
}
onUserInputEmail() {
...
this.$gaService.event('enter_email', 'user_register_form', 'Email');
}
onSubmit() {
...
this.$gaService.event('submit', 'user_register_form', 'Enviar');
}
}
`
`ts
import { Component, OnInit } from '@angular/core';
import { GoogleAnalyticsService } from 'ngx-google-analytics';
@Component(...)
export class TestPageComponent implements OnInit {
constructor(
protected $gaService: GoogleAnalyticsService
) {}
ngOnInit() {
this.$gaService.pageView('/teste', 'Teste de Title')
}
onUserLogin() {
...
this.$gaService.pageView('/teste', 'Teste de Title', undefined, {
user_id: 'my-user-id'
})
}
}
`
You can use angular directives to call GA events.
`js`
js
`$3
`js
`Tracking Angular Router
Add
`NgxGoogleAnalyticsRouterModule` on AppModule to auto track Angular's Router class and trigger a page view after nagivation.`ts
import { NgxGoogleAnalyticsModule, NgxGoogleAnalyticsRouterModule } from 'ngx-google-analytics';
...@NgModule({
...
imports: [
...
NgxGoogleAnalyticsModule.forRoot(environment.ga),
NgxGoogleAnalyticsRouterModule
]
})
export class AppModule {}
``