A simple ng-9 wrapper to load Google Analytics dependency by angular way
npm install ngx-google-analyticsAn easy implementation to track ga on angular8+ apps.
Feedbacks on https://github.com/maxandriani/ngx-google-analytics
I'm investing a big amount of time studing new technologies for my daily job, and I am not able to invest a significant amount of time into maintaining ngx-google-analytics properly. I am looking for volunteers who would like to become active maintainers on the project. If you are interested, please shoot me a note.
* Setup
* NPM
* Simple Setup
* Routing Setup
* Advanced Routing Setup
* GoogleAnalyticsService
* Directives
* Changelog
To setup this package on you project, just call the following command.
```
npm install ngx-google-analytics
On your Angular Project, you shall include the NgxGoogleAnalyticsModule on your highest level application module. ie AddModule. The easiest install mode call the forRoot() method and pass the GA tracking code.
`ts
import { NgxGoogleAnalyticsModule } from 'ngx-google-analytics';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
NgxGoogleAnalyticsModule.forRoot('traking-code')
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
`
We provide a second Module Dependency to configure Router Event Bindings and perform automatic page view every time your application navigates to another page.
Add `NgxGoogleAnalyticsRouterModule` on AppModule enable auto track Router events.
IMPORTANT: This Module just subscribe to Router events when the bootstrap component is created, and then cleans up any subscriptions related to previous component when it is destroyed. You may get some issues if using this module on a server side rendering or multiple bootstrap components. If it is your case, I suggest you subscribe to events by yourself. You can use git repository as reference.
`ts
import { NgxGoogleAnalyticsModule, NgxGoogleAnalyticsRouterModule } from 'ngx-google-analytics';
...
@NgModule({
...
imports: [
...
NgxGoogleAnalyticsModule.forRoot(environment.ga),
NgxGoogleAnalyticsRouterModule
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
]
})
export class AppModule {}
`
You can customize some rules to include/exclude routes on NgxGoogleAnalyticsRouterModule. The include/exclude settings allow:{ include: [ '/full-uri-match' ] }
* Simple route match: ;{ include: [ '/public/*' ] }
Wildcard route match: ;{ include: [ /^\/public\/./ ] }
Regular Expression route match: ;
`ts
import { NgxGoogleAnalyticsModule, NgxGoogleAnalyticsRouterModule } from 'ngx-google-analytics';
...
@NgModule({
...
imports: [
...
NgxGoogleAnalyticsModule.forRoot(environment.ga),
NgxGoogleAnalyticsRouterModule.forRoot({ include: [...], exclude: [...] })
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
]
})
export class AppModule {}
`
This service provides an easy and strong typed way to call gtag() command. It does nothing else then convert a strong typed list of arguments into a standard gtag api call.
`ts
@Component( ... )
export class TestFormComponent {
constructor(
private $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
@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'
})
}
}
`
In a way to help you to be more productive on attach GA events on UI elements. We create some directives to handle GoogleAnalyticsService and add event listener by simple attributes.
The default behaviour is call gtag on click events, but you can change the trigger to any HTML Event (e.g. focus, blur or custom events) as well.
`js`
If you attach gaEvent directive on form elements, it will assume focus event as default trigger.
`js`
Sometimes your UX guy want to group several elements in the interface at same group to help his analysis and reports. Fortunately the gaCategory directive can be placed on the highest level group gaEvent
element and all child will assume the parent gaCategory as their parent.
`js``