import { Injectable } from '@angular/core' ; import { Angulartics2 } from '../../angulartics2-core' ; import { GoogleTagManagerSettings } from '../../angulartics2-config' ; declare var dataLayer ; export class GoogleTagManagerDefaults implements GoogleTagManagerSettings { userId = null ; } @ Injectable ( { providedIn : 'root' } ) export class Angulartics2GoogleTagManager { constructor ( protected angulartics2 ) { if ( typeof dataLayer !== 'undefined' && dataLayer ) { dataLayer = ( window as any ) . dataLayer = ( window as any ) . dataLayer || [ ] ; } const defaults = new GoogleTagManagerDefaults ( ) ; this . angulartics2 . settings . gtm = { ... defaults , ... this . angulartics2 . settings . gtm } ; this . angulartics2 . setUsername . subscribe ( ( x ) => this . setUsername ( x ) ) ; } startTracking ( ) { this . angulartics2 . pageTrack . pipe ( this . angulartics2 . filterDeveloperMode ( ) ) . subscribe ( x => this . pageTrack ( x . path ) ) ; this . angulartics2 . eventTrack . pipe ( this . angulartics2 . filterDeveloperMode ( ) ) . subscribe ( x => this . eventTrack ( x . action , x . properties ) ) ; this . angulartics2 . exceptionTrack . pipe ( this . angulartics2 . filterDeveloperMode ( ) ) . subscribe ( ( x ) => this . exceptionTrack ( x ) ) ; } pageTrack ( path ) { this . pushLayer ( { event : 'Page View' , 'content-name' : path , userId : this . angulartics2 . settings . gtm . userId , } ) ; } layer pushLayer ( layer ) { if ( typeof dataLayer !== 'undefined' && dataLayer ) { dataLayer . push ( layer ) ; } } param action eventTrack ( action , properties ) { properties = properties || { } ; this . pushLayer ( { event : properties . event || 'interaction' , target : properties . category || 'Event' , action , label : properties . label , value : properties . value , interactionType : properties . noninteraction , userId : this . angulartics2 . settings . gtm . userId , ... properties . gtmCustom , } ) ; } /**
     * Exception Track Event in GTM
     *
     */ exceptionTrack ( properties ) { if ( ! properties || ! properties . appId || ! properties . appName || ! properties . appVersion ) { console . error ( 'Must be setted appId, appName and appVersion.' ) ; return ; } if ( properties . fatal === undefined ) { console . log ( 'No "fatal" provided, sending with fatal=true' ) ; properties . exFatal = true ; } properties . exDescription = properties . event ? properties . event . stack : properties . description ; this . eventTrack ( `Exception thrown for ${ properties . appName } <${ properties . appId }@${ properties . appVersion }>` , { category : 'Exception' , label : properties . exDescription , } ) ; } param userId setUsername ( userId ) { this . angulartics2 . settings . gtm . userId = userId ; } }