import { IAnalytics , IEventArgs , TraitsMap , IPageEvent , IAnalyticsUser } from "../IAnalytics" ; import { StandardEventProperties } from "../StandardEventProperties" ; import { ISODateTimeStrings } from "polar-shared/src/metadata/ISODateTimeStrings" ; const standardEventProperties = StandardEventProperties . create ( ) ; export class IntercomAnalytics implements IAnalytics { private intercomClient = createIntercomClient ( ) ; private user : IAnalyticsUser | undefined = undefined ; public constructor ( ) { this . boot ( toIntercomData ( undefined ) ) ; } public event ( evt ) { } public event2 ( event , data ? ) { } public page ( event ) { } public identify ( user ) { this . user = user ; this . boot ( toIntercomData ( this . user ) ) ; } public traits ( traits ) { const intercomData = toIntercomData ( this . user ) ; if ( ! intercomData ) { console . warn ( "No intercom data" ) ; return ; } const data = { ... intercomData , ... traits , ... standardEventProperties } ; this . update ( data ) ; } public version ( version ) { } public heartbeat ( ) { } public logout ( ) { this . user = undefined ; this . boot ( toIntercomData ( undefined ) ) ; } private boot ( intercomData ) { if ( ! this . intercomClient ) { console . warn ( "No intercom client" ) ; return ; } if ( ! intercomData ) { console . warn ( "No intercom data" ) ; return ; } this . intercomClient . boot ( intercomData ) ; } private update ( intercomData ) { if ( ! this . intercomClient ) { console . warn ( "No intercom client" ) ; return ; } if ( ! intercomData ) { console . warn ( "No intercom data" ) ; return ; } this . intercomClient . update ( intercomData ) ; } } export interface IIntercomDataForAnonymousUser { readonly app_id : string ; [ key ] : string | number ; } export interface IIntercomDataForAuthenticatedUser extends IIntercomDataForAnonymousUser { readonly user_id : string ; readonly name : string ; readonly email : string ; readonly created_at : string ; } export type IntercomData = IIntercomDataForAnonymousUser | IIntercomDataForAuthenticatedUser ; export interface IIntercomClient { readonly boot : ( data : IntercomData ) => void ; readonly update : ( data : IntercomData ) => void ; } declare var window ; export function createIntercomClient ( ) { if ( window . Intercom ) { function boot ( data ) { window . Intercom ( 'boot' , data ) ; } function update ( data ) { window . Intercom ( 'update' , data ) ; } return { boot , update } ; } return undefined ; } export function toIntercomData ( user ) { const app_id = "wk5j7vo0" ; if ( ! user ) { return { app_id } ; } const created_at = Math . floor ( ISODateTimeStrings . parse ( user . created ) . getTime ( ) / 1000 ) ; const data = { app_id , user_id : user . uid , name : user . displayName || "" , email : user . email , created_at : `${ created_at }` } ; return data ; }