@ -11,38 +11,29 @@ import { Injectable } from '@angular/core';
import { NavigationEnd , Router } from '@angular/router' ;
import { NavigationEnd , Router } from '@angular/router' ;
import { filter } from 'rxjs/operators' ;
import { filter } from 'rxjs/operators' ;
import { AnalyticsIdConfig } from './../configurations' ;
import { UIOptions } from './../configurations' ;
import { Types } from './../utils/types' ;
import { Types } from './../utils/types' ;
import { ResourceLoaderService } from './resource-loader.service' ;
import { ResourceLoaderService } from './resource-loader.service' ;
export const AnalyticsServiceFactory = ( analyticsId : AnalyticsIdConfig , router : Router , resourceLoader : ResourceLoaderService ) = > {
export const AnalyticsServiceFactory = ( uiOptions : UIOptions , router : Router , resourceLoader : ResourceLoaderService ) = > {
return new AnalyticsService ( analyticsId , router , resourceLoader ) ;
return new AnalyticsService ( uiOptions , router , resourceLoader ) ;
} ;
} ;
@Injectable ( )
@Injectable ( )
export class AnalyticsService {
export class AnalyticsService {
private readonly gtag : any ;
private readonly gtag : any ;
private analyticsId : string ;
constructor ( analyticsId? : AnalyticsIdConfig , router? : Router , resourceLoader? : ResourceLoaderService ) {
constructor ( private readonly uiOptions? : UIOptions ,
private readonly router? : Router ,
private readonly resourceLoader? : ResourceLoaderService
) {
window [ 'dataLayer' ] = window [ 'dataLayer' ] || [ ] ;
window [ 'dataLayer' ] = window [ 'dataLayer' ] || [ ] ;
this . setAnalyticsId ( ) ;
this . gtag = function ( ) {
this . gtag = function ( ) {
window [ 'dataLayer' ] . push ( arguments ) ;
window [ 'dataLayer' ] . push ( arguments ) ;
} ;
} ;
this . configureGtag ( ) ;
if ( analyticsId && router && resourceLoader && window . location . hostname !== 'localhost' ) {
this . gtag ( 'config' , analyticsId . value , { anonymize_ip : true } ) ;
router . events . pipe (
filter ( e = > Types . is ( e , NavigationEnd ) ) )
. subscribe ( ( ) = > {
this . gtag ( 'config' , analyticsId . value , { page_path : window.location.pathname , anonymize_ip : true } ) ;
} ) ;
if ( document . cookie . indexOf ( 'ga-disable' ) < 0 ) {
resourceLoader . loadScript ( ` https://www.googletagmanager.com/gtag/js?id= ${ analyticsId . value } ` ) ;
}
}
}
}
public trackEvent ( category : string , action : string , label? : string , value? : number ) {
public trackEvent ( category : string , action : string , label? : string , value? : number ) {
@ -53,4 +44,30 @@ export class AnalyticsService {
value : value
value : value
} ) ;
} ) ;
}
}
private configureGtag() {
if ( this . analyticsId && this . router && this . resourceLoader && window . location . hostname !== 'localhost' ) {
this . gtag ( 'config' , this . analyticsId , { anonymize_ip : true } ) ;
this . router . events . pipe (
filter ( e = > Types . is ( e , NavigationEnd ) ) )
. subscribe ( ( ) = > {
this . gtag ( 'config' , this . analyticsId , { page_path : window.location.pathname , anonymize_ip : true } ) ;
} ) ;
this . loadGoogletagmanagerScript ( ) ;
}
}
private loadGoogletagmanagerScript() {
if ( document . cookie . indexOf ( 'ga-disable' ) < 0 && this . resourceLoader ) {
this . resourceLoader . loadScript ( ` https://www.googletagmanager.com/gtag/js?id= ${ this . analyticsId } ` ) ;
}
}
private setAnalyticsId() {
if ( this . uiOptions ) {
this . analyticsId = this . uiOptions . get ( 'google.analyticsId' ) ;
}
}
}
}