import { Injectable, Inject, PLATFORM_ID } from '@angular/core'; import { Effect, Actions, ofType } from '@ngrx/effects'; import { tap, withLatestFrom, filter, map } from 'rxjs/operators'; import { AdobeAnalyticsService } from '../services/adobe-analytics.service'; import { AdobeAnalyticsPayloadService } from '../services/adobe-analytics-payload.service'; import { WebPerformanceService } from '../services/web-performance-service'; import { EventTransceiverService } from '../services/event-transceiver.service'; import { EventTrackerPayloadService } from '../services/event-tracker-payload.service'; import * as eventActions from '../actions/event-transceiver.actions'; import * as webPerformanceActions from '../actions/web-performance.actions'; import * as WebPerformanceStore from '../reducers/web-performance.reducer'; import { EventTransceiverState } from '../reducers/event-transceiver.reducer'; import { Store, select, Action } from '@ngrx/store'; import { selectAdobeAnalyticsDTMReady, selectWebPerformance, selectAnalyticsWhitelist, selectHttpPerformance, selectWebPerformanceWhitelist } from '../selectors/event-transceiver.selectors'; import { isPlatformBrowser } from '@angular/common'; import { HttpReturnRecord } from '../models/web-performance.interface'; import { EventTrackerUpdateLog } from '../actions'; import { EventTrackerLogEntry } from '../models/event-tracker.interface'; import { Observable } from 'rxjs/Observable'; @Injectable() export class EventTransceiverEffect { constructor( private actions$: Actions, private store$: Store, private adobeService: AdobeAnalyticsService, private adobePayloadService: AdobeAnalyticsPayloadService, private webPerformanceService: WebPerformanceService, private eventTransceiverService: EventTransceiverService, private eventTrackerPayloadService: EventTrackerPayloadService, @Inject(PLATFORM_ID) private platformId ) { } @Effect({ dispatch: false }) receiveRegisterComponentEvent$: Observable = this.actions$ .pipe( ofType(eventActions.EventTransceiverActions.EventTransceiverReceiveRegisterComponentEvent), filter(action => isPlatformBrowser(this.platformId)), withLatestFrom( this.store$.pipe(select(selectWebPerformance)) ), tap(([action, webPerformanceState]) => { this.webPerformanceService.processRegisterComponentEvent(action.componentName, webPerformanceState); const trackerPayload = this.eventTrackerPayloadService.getComponentEventLogWebPerformancePayload(action); this.store$.dispatch(new EventTrackerUpdateLog(trackerPayload)); }) ); @Effect({ dispatch: false }) receiveComponentRenderEvent$: Observable = this.actions$ .pipe( ofType(eventActions.EventTransceiverActions.EventTransceiverReceiveComponentRenderEvent), filter(action => isPlatformBrowser(this.platformId)), withLatestFrom( this.store$.pipe(select(selectWebPerformance)), this.store$.pipe(select(selectWebPerformanceWhitelist)) ), tap(([action, webPerformanceState]) => { this.webPerformanceService.processComponentRenderEvent(action.componentName, webPerformanceState); const trackerPayload = this.eventTrackerPayloadService.getComponentEventLogWebPerformancePayload(action); this.store$.dispatch(new EventTrackerUpdateLog(trackerPayload)); }) ); @Effect({ dispatch: false }) receiveHTTPEvent$: Observable = this.actions$ .pipe( ofType(eventActions.EventTransceiverActions.EventTransceiverReceiveHTTPReturnEvent), filter(action => isPlatformBrowser(this.platformId)), withLatestFrom( this.store$.pipe(select(selectHttpPerformance)), this.store$.pipe(select(selectWebPerformanceWhitelist)) ), tap(([action, httpReturnRecordList]) => { this.webPerformanceService.processHTTPEvent(action.payload, httpReturnRecordList); const trackerPayload = this.eventTrackerPayloadService.getHttpEventLogWebPerformancePayload(action); this.store$.dispatch(new EventTrackerUpdateLog(trackerPayload)); }) ); @Effect({ dispatch: false }) updateMeaningfulPaint$: Observable = this.actions$ .pipe( ofType(webPerformanceActions.WebPerformanceActions.WebPerformanceCalculateFirstMeaningfulPaint), filter(action => isPlatformBrowser(this.platformId)), withLatestFrom( this.store$.pipe(select(selectWebPerformance)) ), tap(([action, webPerformanceState]) => { this.webPerformanceService.calculateFirstMeaningfulPaint(webPerformanceState, action.componentPayload, action.httpPayloadFlag, action.httpPayload); }) ); @Effect({ dispatch: false }) returnWebPerformanceStore$: Observable = this.actions$ .pipe( ofType(webPerformanceActions.WebPerformanceActions.WebPerformanceReturnStore), filter(action => isPlatformBrowser(this.platformId)), tap(action => { this.webPerformanceService.returnWebPerformanceState(); }) ); @Effect({ dispatch: false }) receivePageLoadEvent$: Observable = this.actions$ .pipe( ofType(eventActions.EventTransceiverActions.EventTransceiverReceivePageLoadEvent), filter(action => isPlatformBrowser(this.platformId)), tap(action => { // "pageMeta" is intended for general page info like breadcrumbs and page name (data that wouldn't be provided by payloads like "productLocalized") // "Certona": the certona carousel data present on a page (introducing in Search Page, possible retrofit to Product Page) // "nullSearchPage": carousel data specific to the Null Search Page const payload = this.adobePayloadService.formPageLoadPayload( action.event, action.dataWrapper, action.pageMeta ); this.adobeService.transmitPageLoadDataToAdobe(payload); this.webPerformanceService.processSSRHttpPerformanceList(); const trackerPayload = this.eventTrackerPayloadService.getEventLogAnalyticsPayload(payload); this.store$.dispatch(new EventTrackerUpdateLog(trackerPayload)); }) ); @Effect({ dispatch: false }) receivePIPElementClickEvent$: Observable = this.actions$ .pipe( ofType(eventActions.EventTransceiverActions.EventTransceiverReceiveClickEvent), tap(action => { const payload = this.adobePayloadService.formClickDataPayload(action.event, action.elementType, action.elementSubType, action.elementValue); this.store$.dispatch(new eventActions.EventTransceiverTransmitAnalyticsEvent(payload)); }) ); @Effect({ dispatch: false }) receiveSiteErrorEvent$: Observable = this.actions$ .pipe( ofType(eventActions.EventTransceiverActions.EventTransceiverReceiveSiteErrorEvent), tap(action => { const payload = this.adobePayloadService.formSiteErrorPayload(action.event, action.errorType, action.userInformed, action.errorCode, action.errorValue, action.target, action.errorPayload); this.store$.dispatch(new eventActions.EventTransceiverTransmitAnalyticsEvent(payload)); }) ); @Effect({ dispatch: false }) receiveAddToCartEvent$: Observable = this.actions$ .pipe( ofType(eventActions.EventTransceiverActions.EventTransceiverReceiveAddToCartEvent), tap(action => { const payload = this.adobePayloadService.formAddToCartPayload(action.event, action.analyticsData); this.store$.dispatch(new eventActions.EventTransceiverTransmitAnalyticsEvent(payload)); }) ); @Effect({ dispatch: false }) receiveSuperSkuSwitchEvent$: Observable = this.actions$ .pipe( ofType(eventActions.EventTransceiverActions.EventTransceiverReceiveSuperSkuSwitch), tap(action => { const payload = this.adobePayloadService.formSuperSkuSwitchPayload( action.page, action.productId, action.attributeName, action.attributeId, action.targetProductId, action.targetValueId, action.targetValueName); this.store$.dispatch(new eventActions.EventTransceiverTransmitAnalyticsEvent(payload)); }) ); @Effect({ dispatch: false }) initializeWhitelist$: Observable = this.actions$ .pipe( ofType(eventActions.EventTransceiverActions.EventTransceiverInitializeWhitelist), filter(action => isPlatformBrowser(this.platformId)), tap((action) => { this.eventTransceiverService.initializeWhitelist(); }) ); @Effect({ dispatch: false }) SetWhitelist$: Observable = this.actions$ .pipe( ofType(eventActions.EventTransceiverActions.EventTransceiverSetWhitelistEvent), tap(action => { const trackerPayload: EventTrackerLogEntry = this.eventTrackerPayloadService.getEventLogWhitelistPayload(action); this.store$.dispatch(new EventTrackerUpdateLog(trackerPayload)); this.store$.dispatch(new eventActions.EventTransceiverSetWhitelistSuccess(action.payload)); }) ); @Effect({ dispatch: false }) receiveCarouselItemClickEvent$: Observable = this.actions$ .pipe( ofType(eventActions.EventTransceiverActions.EventTransceiverReceiveCarouselItemClick), tap(action => { const payload = this.adobePayloadService.formCarouselItemClickPayload( action.payload.productId, action.payload.contextProductId, action.payload.slotLocation, action.payload.productInteractionLocation, action.payload.productInteractionPageType, action.payload.productInteractionLocationTitle, action.payload.productInteractionLocationOrder ); this.store$.dispatch(new eventActions.EventTransceiverTransmitAnalyticsEvent(payload)); }) ); @Effect({ dispatch: false }) UpdatePETAEvent$: Observable = this.actions$ .pipe( ofType(eventActions.EventTransceiverActions.EventTransceiverUpdatePETAEvent), tap(action => { const payload = this.adobePayloadService.formPETAUpdate(action.analyticsData); this.store$.dispatch(new eventActions.EventTransceiverTransmitAnalyticsEvent(payload)); }) ); /** * Transmit Analytics Event - Common pathway to transmit analytics data * - Updates event log * - Transmits data if conditions are satisfied */ @Effect({ dispatch: false }) transmitAnalyticsEvent$: Observable = this.actions$ .pipe( ofType(eventActions.EventTransceiverActions.EventTransceiverTransmitAnalyticsEvent), map(action => action.payload), withLatestFrom( this.store$.pipe(select(selectAdobeAnalyticsDTMReady)), this.store$.pipe(select(selectAnalyticsWhitelist)) ), tap(([payload, DTMReady, whitelist]) => { const trackerPayload: EventTrackerLogEntry = this.eventTrackerPayloadService.getEventLogAnalyticsPayload(payload, >whitelist); this.store$.dispatch(new EventTrackerUpdateLog(trackerPayload)); }), filter(([payload, DTMReady, whitelist]) => DTMReady && (>whitelist).includes(payload.event.type) && isPlatformBrowser(this.platformId) ), tap(([payload]) => { this.adobeService.transmitDataToAdobe(payload); }) ); }