import { Injectable } from '@angular/core' ; import { UserTimings } from '../../angulartics2-interfaces' ; import { GoogleGlobalSiteTagSettings } from '../../angulartics2-config' ; import { Angulartics2 } from '../../angulartics2-core' ; import { EventGst , UserTimingsGst } from './gst-interfaces' ; declare var gtag ; declare var ga ; export class GoogleGlobalSiteTagDefaults implements GoogleGlobalSiteTagSettings { trackingIds : string [ ] = [ ] ; constructor ( ) { if ( typeof ga !== 'undefined' && ga ) { ga ( ( ) => { ga . getAll ( ) . forEach ( ( tracker ) => { const id = tracker . get ( 'trackingId' ) ; if ( id !== undefined && this . trackingIds . indexOf ( id ) === - 1 ) { this . trackingIds . push ( id ) ; } } ) ; } ) ; } } } @ Injectable ( { providedIn : 'root' } ) export class Angulartics2GoogleGlobalSiteTag { private dimensionsAndMetrics : { [ key ] : any ; } = { } ; constructor ( protected angulartics2 ) { const defaults = new GoogleGlobalSiteTagDefaults ( ) ; this . angulartics2 . settings . gst = { ... defaults , ... this . angulartics2 . settings . gst } ; } 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 ) ) ; this . angulartics2 . userTimings . pipe ( this . angulartics2 . filterDeveloperMode ( ) ) . subscribe ( x => this . userTimings ( this . convertTimings ( x ) ) ) ; this . angulartics2 . setUsername . pipe ( this . angulartics2 . filterDeveloperMode ( ) ) . subscribe ( ( x ) => this . setUsername ( x ) ) ; this . angulartics2 . setUserProperties . pipe ( this . angulartics2 . filterDeveloperMode ( ) ) . subscribe ( ( x ) => this . setUserProperties ( x ) ) ; } param path pageTrack ( path ) { if ( typeof gtag !== 'undefined' && gtag ) { const params = { page_path : path , page_location : window . location . protocol + '//' + window . location . host + path , ... this . dimensionsAndMetrics , } ; if ( this . angulartics2 . settings . gst . customMap ) { params . custom_map = this . angulartics2 . settings . gst . customMap ; } if ( this . angulartics2 . settings . gst . userId ) { params . user_id = this . angulartics2 . settings . gst . userId ; } if ( this . angulartics2 . settings . gst . anonymizeIp ) { params . anonymize_ip = this . angulartics2 . settings . gst . anonymizeIp ; } for ( const id of this . angulartics2 . settings . gst . trackingIds ) { gtag ( 'config' , id , params ) ; } } } param action eventTrack ( action , properties = { } ) { this . eventTrackInternal ( action , { event_category : properties . category || 'interaction' , event_label : properties . label , value : properties . value , non_interaction : properties . noninteraction , ... properties . gstCustom , } ) ; } /**
     * Exception Track Event in GST. See:
     *
     * https://developers.google.com/analytics/devguides/collection/gtagjs/exceptions
     *
     */ exceptionTrack ( properties ) { if ( properties . fatal === undefined ) { console . log ( 'No "fatal" provided, sending with fatal=true' ) ; properties . fatal = true ; } properties . exDescription = properties . event ? properties . event . stack : properties . description ; this . eventTrack ( 'exception' , { gstCustom : { description : properties . exDescription , fatal : properties . fatal , ... properties . gstCustom , } , } ) ; } param properties link userTimings ( properties ) { if ( ! properties ) { console . error ( 'User timings - "properties" parameter is required to be set.' ) ; return ; } this . eventTrackInternal ( 'timing_complete' , { name : properties . name , value : properties . value , event_category : properties . category , event_label : properties . label , } ) ; } private convertTimings ( properties ) { return { name : properties . timingVar , value : properties . timingValue , category : properties . timingCategory , label : properties . timingLabel , } ; } setUsername ( userId ) { this . angulartics2 . settings . gst . userId = userId ; if ( typeof gtag !== 'undefined' && gtag ) { gtag ( 'set' , { user_id : typeof userId === 'string' || ! userId ? userId : userId . userId } ) ; } } setUserProperties ( properties ) { this . setDimensionsAndMetrics ( properties ) ; } private setDimensionsAndMetrics ( properties ) { this . dimensionsAndMetrics = { ... this . dimensionsAndMetrics , ... properties , } ; Object . keys ( this . dimensionsAndMetrics ) . forEach ( key => { const val = this . dimensionsAndMetrics [ key ] ; if ( val === undefined || val === null ) { delete this . dimensionsAndMetrics [ key ] ; } } ) ; if ( typeof gtag !== 'undefined' && gtag ) { gtag ( 'set' , this . dimensionsAndMetrics ) ; } } private eventTrackInternal ( action , properties = { } ) { this . cleanProperties ( properties ) ; if ( typeof gtag !== 'undefined' && gtag ) { gtag ( 'event' , action , properties ) ; } } private cleanProperties ( properties ) { if ( properties . value ) { const parsed = parseInt ( properties . value , 10 ) ; properties . value = isNaN ( parsed ) ? 0 : parsed ; } } }