import { Injectable , Inject , PLATFORM_ID } from '@angular/core' ; import { Location , isPlatformServer } from '@angular/common' ; import hashCode from '../../helpers/hash-code' ; import { Session } from '../../services/session' ; import { Client } from '../../services/api' ; import { ClientMetaDirective } from '../directives/client-meta.directive' ; /**
 * Client meta data structure
 */ export interface ClientMetaData { platform : string ; source : string ; timestamp : number ; salt : string ; medium : string ; campaign : string ; page_token : string ; delta : number ; position ? : number | string ; } /**
 * Helps with page token and API interaction
 */ @ Injectable ( ) export class ClientMetaService { param location param session param client param platformId constructor ( protected location , protected session , protected client , @ Inject ( PLATFORM_ID ) private platformId ) { } param salt param timestamp buildPageToken ( salt , timestamp ) { const user = this . session . getLoggedInUser ( ) || { } ; const tokenParts = [ salt , this . location . path ( ) || '/' , user . guid || '000000000000000000' , timestamp || 0 , ] ; return hashCode ( tokenParts . join ( ':' ) , 5 ) ; } param entity param clientMetaDirective param extraClientMetaData async recordView ( entity , clientMetaDirective , extraClientMetaData = { } ) { if ( isPlatformServer ( this . platformId ) ) { return ; } await this . client . post ( 'api/v2/analytics/views/entity/' + entity . guid , { client_meta : { ... ( clientMetaDirective && clientMetaDirective . build ( ) ) , ... extraClientMetaData , } , } ) ; } }