Google Analytics for your Angular application
$ npm install --save angular-ga
The Google Analytics tracking script is not included in this module. Make sure to add it to your page.
import{BrowserModule}from'@angular/platform-browser';import{NgModule}from'@angular/core';import{GoogleAnalyticsModule,GA_TOKEN}from'angular-ga';import{AppComponent}from'./app.component';
@NgModule({imports: [BrowserModule,GoogleAnalyticsModule.forRoot()],declarations: [AppComponent],bootstrap: [AppComponent],providers: [{provide: GA_TOKEN,useValue: 'UA-TOKEN-1'}]})exportclassAppModule{}It's also possible to leave the configuration empty and configure the library later on through the service.
@NgModule({imports: [BrowserModule,GoogleAnalyticsModule.forRoot()],declarations: [AppComponent],bootstrap: [AppComponent]})exportclassAppModule{}Inject the GoogleAnalyticsService into your components or services.
import{Component,OnInit}from'@angular/core';import{GoogleAnalyticsService}from'angular-ga';
@Component({templateUrl: 'app.component.html'})exportclassAppComponentimplementsOnInit{constructor(privategaService: GoogleAnalyticsService){}ngOnInit(){this.gaService.event.emit({category: 'app',action: 'bootstrap'});}}import{Component,OnInit}from'@angular/core';import{GoogleAnalyticsService}from'angular-ga';
@Component({templateUrl: 'app.component.html'})exportclassAppComponentimplementsOnInit{constructor(privategaService: GoogleAnalyticsService){}ngOnInit(){this.gaService.configure('UA-TOKEN-1');}}Type: string
Tracking Id.
Type: Objectstring
Default: auto
Any of the Create Only Fields.
Emit a Google Analytics event.
Type: string
Typically the object that was interacted with (e.g. Video)
Type: string
The type of interaction (e.g. play)
Optional
Type: string
Useful for categorizing events (e.g. Fall Campaign)
Optional
Type: number
A numeric value associated with the event (e.g. 42)
Emit a Google Analytics page view.
Type: string
The path portion of a URL. This value should start with a slash (/) character.
Optional
Type: string
The title of the page (e.g. homepage)
MIT © Sam Verschueren