import { Injectable } from '@angular/core' ; import { Angulartics2 } from '../../angulartics2-core' ; declare var _paq ; export type EventTrackAction = 'setEcommerceView' | 'addEcommerceItem' | 'trackEcommerceCartUpdate' | 'trackEcommerceOrder' | 'trackLink' | 'trackGoal' | 'trackSiteSearch' | string ; export type ScopeMatomo = 'visit' | 'page' ; export interface DimensionsMatomoProperties { dimension0 ? : string ; dimension1 ? : string ; dimension2 ? : string ; dimension3 ? : string ; dimension4 ? : string ; dimension5 ? : string ; dimension6 ? : string ; dimension7 ? : string ; dimension8 ? : string ; dimension9 ? : string ; } export interface SetEcommerceViewMatomoProperties { class productSKU : string ; class productName : string ; class categoryName : string ; class price : string ; } export interface AddEcommerceItemProperties { class productSKU : string ; class productName : string ; class productCategory : string ; class price : string ; class quantity : string ; } export interface TrackEcommerceCartUpdateMatomoProperties { class grandTotal : string ; } export interface TrackEcommerceOrderMatomoProperties { class orderId : string ; class grandTotal : string ; class subTotal : string ; class tax : string ; class shipping : string ; class discount : string ; } export interface TrackLinkMatomoProperties { class url : string ; class linkType : string ; } export interface TrackGoalMatomoProperties { class goalId : string ; class value : string ; } export interface TrackSiteSearchMatomoProperties { class keyword : string ; class category : string ; class searchCount : string ; } export interface TrackEventMatomoProperties { class category : string ; class name ? : string ; class label ? : string ; class value : number | string ; } export interface SetCustomVariableMatomoProperties extends DimensionsMatomoProperties { class index : number ; class name : string ; class value : string ; class scope : ScopeMatomo ; } export interface DeleteCustomVariableMatomoProperties { class index : number ; class scope : ScopeMatomo ; } export type EventTrackactionProperties = SetEcommerceViewMatomoProperties | AddEcommerceItemProperties | TrackEcommerceCartUpdateMatomoProperties | TrackEcommerceOrderMatomoProperties | TrackLinkMatomoProperties | TrackGoalMatomoProperties | TrackSiteSearchMatomoProperties | TrackEventMatomoProperties ; @ Injectable ( { providedIn : 'root' } ) export class Angulartics2Matomo { constructor ( private angulartics2 ) { if ( typeof _paq === 'undefined' ) { console . warn ( 'Matomo not found' ) ; } this . angulartics2 . setUsername . subscribe ( ( x ) => this . setUsername ( x ) ) ; this . angulartics2 . setUserProperties . subscribe ( ( x ) => this . setUserProperties ( 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 ) ) ; } pageTrack ( path , title ? ) { try { if ( ! window . location . origin ) { ( window . location as any ) . origin = window . location . protocol + '//' + window . location . hostname + ( window . location . port ? ':' + window . location . port : '' ) ; } _paq . push ( [ 'setDocumentTitle' , title || window . document . title ] ) ; _paq . push ( [ 'setCustomUrl' , window . location . origin + path ] ) ; _paq . push ( [ 'trackPageView' ] ) ; } catch ( e ) { if ( ! ( e instanceof ReferenceError ) ) { throw e ; } } } resetUser ( ) { try { _paq . push ( [ 'appendToTrackingUrl' , 'new_visit=1' ] ) ; _paq . push ( [ 'deleteCookies' ] ) ; } catch ( e ) { if ( ! ( e instanceof ReferenceError ) ) { throw e ; } } } eventTrack ( action , properties ) ; eventTrack ( action , properties ) ; eventTrack ( action , properties ) ; eventTrack ( action , properties ) ; eventTrack ( action , properties ) ; eventTrack ( action , properties ) ; eventTrack ( action , properties ) ; eventTrack ( action , properties ) ; param action param properties eventTrack ( action , properties ? ) { let params = [ ] ; switch ( action ) { case 'setEcommerceView' : params = [ 'setEcommerceView' , ( properties as SetEcommerceViewMatomoProperties ) . productSKU , ( properties as SetEcommerceViewMatomoProperties ) . productName , ( properties as SetEcommerceViewMatomoProperties ) . categoryName , ( properties as SetEcommerceViewMatomoProperties ) . price , ] ; break ; case 'addEcommerceItem' : params = [ 'addEcommerceItem' , ( properties as AddEcommerceItemProperties ) . productSKU , ( properties as AddEcommerceItemProperties ) . productName , ( properties as AddEcommerceItemProperties ) . productCategory , ( properties as AddEcommerceItemProperties ) . price , ( properties as AddEcommerceItemProperties ) . quantity , ] ; break ; case 'trackEcommerceCartUpdate' : params = [ 'trackEcommerceCartUpdate' , ( properties as TrackEcommerceCartUpdateMatomoProperties ) . grandTotal , ] ; break ; case 'trackEcommerceOrder' : params = [ 'trackEcommerceOrder' , ( properties as TrackEcommerceOrderMatomoProperties ) . orderId , ( properties as TrackEcommerceOrderMatomoProperties ) . grandTotal , ( properties as TrackEcommerceOrderMatomoProperties ) . subTotal , ( properties as TrackEcommerceOrderMatomoProperties ) . tax , ( properties as TrackEcommerceOrderMatomoProperties ) . shipping , ( properties as TrackEcommerceOrderMatomoProperties ) . discount , ] ; break ; case 'trackLink' : params = [ 'trackLink' , ( properties as TrackLinkMatomoProperties ) . url , ( properties as TrackLinkMatomoProperties ) . linkType , ] ; break ; case 'trackGoal' : params = [ 'trackGoal' , ( properties as TrackGoalMatomoProperties ) . goalId , ( properties as TrackGoalMatomoProperties ) . value , ] ; break ; case 'trackSiteSearch' : params = [ 'trackSiteSearch' , ( properties as TrackSiteSearchMatomoProperties ) . keyword , ( properties as TrackSiteSearchMatomoProperties ) . category , ( properties as TrackSiteSearchMatomoProperties ) . searchCount , ] ; break ; default : if ( ( properties as TrackEventMatomoProperties ) . value ) { const parsed = parseInt ( ( properties as TrackEventMatomoProperties ) . value as any , 10 ) ; ( properties as TrackEventMatomoProperties ) . value = isNaN ( parsed ) ? 0 : parsed ; } params = [ 'trackEvent' , ( properties as TrackEventMatomoProperties ) . category , action , ( properties as TrackEventMatomoProperties ) . name || ( properties as TrackEventMatomoProperties ) . label , ( properties as TrackEventMatomoProperties ) . value , ] ; } try { _paq . push ( params ) ; } catch ( e ) { if ( ! ( e instanceof ReferenceError ) ) { throw e ; } } } setUsername ( userId ) { try { _paq . push ( [ 'setUserId' , userId ] ) ; } catch ( e ) { if ( ! ( e instanceof ReferenceError ) ) { throw e ; } } } link setUserProperties ( properties ) { const dimensions = this . setCustomDimensions ( properties ) ; try { if ( dimensions . length === 0 ) { _paq . push ( [ 'setCustomVariable' , properties . index , properties . name , properties . value , properties . scope , ] ) ; } } catch ( e ) { if ( ! ( e instanceof ReferenceError ) ) { throw e ; } } } link deletedUserProperties ( properties ) { try { _paq . push ( [ 'deleteCustomVariable' , properties . index , properties . scope ] ) ; } catch ( e ) { if ( ! ( e instanceof ReferenceError ) ) { throw e ; } } } private setCustomDimensions ( properties ) { const dimensionRegex = /dimension[1-9]\d*/ ; const dimensions = Object . keys ( properties ) . filter ( key => dimensionRegex . exec ( key ) ) ; dimensions . forEach ( dimension => { const number = Number ( dimension . substr ( 9 ) ) ; _paq . push ( [ 'setCustomDimension' , number , properties [ dimension ] ] ) ; } ) ; return dimensions ; } }