From e7bf2950b9229677bb60ec1585a0e1d9e1695409 Mon Sep 17 00:00:00 2001 From: Maxim Belov Date: Tue, 17 Sep 2024 17:12:48 +0300 Subject: [PATCH] feat(service): add service methods, extend settings --- .../ngx-pendo/src/lib/ngx-pendo.interfaces.ts | 3 +- .../ngx-pendo/src/lib/ngx-pendo.module.ts | 2 +- .../ngx-pendo/src/lib/ngx-pendo.provide.ts | 2 +- .../src/lib/ngx-pendo.service.spec.ts | 10 ++++++ .../ngx-pendo/src/lib/ngx-pendo.service.ts | 33 ++++++++++++++----- 5 files changed, 39 insertions(+), 11 deletions(-) diff --git a/projects/ngx-pendo/src/lib/ngx-pendo.interfaces.ts b/projects/ngx-pendo/src/lib/ngx-pendo.interfaces.ts index 40d7254..cbcc9a4 100644 --- a/projects/ngx-pendo/src/lib/ngx-pendo.interfaces.ts +++ b/projects/ngx-pendo/src/lib/ngx-pendo.interfaces.ts @@ -1,9 +1,10 @@ -import { InputSignal, WritableSignal } from '@angular/core'; +import { InputSignal, WritableSignal, Provider } from '@angular/core'; export interface IPendoSettings { pendoApiKey: string; pendoScriptOrigin?: string; pendoIdFormatter?: (pendoId: string) => string; + pendoInitializerProvider?: Provider; } export interface IPendoDirective { diff --git a/projects/ngx-pendo/src/lib/ngx-pendo.module.ts b/projects/ngx-pendo/src/lib/ngx-pendo.module.ts index 83ac0b5..e9b5e1b 100644 --- a/projects/ngx-pendo/src/lib/ngx-pendo.module.ts +++ b/projects/ngx-pendo/src/lib/ngx-pendo.module.ts @@ -18,7 +18,7 @@ export class NgxPendoModule { provide: NGX_PENDO_SETTINGS_TOKEN, useValue: settings }, - NGX_PENDO_INITIALIZER_PROVIDER + settings.pendoInitializerProvider || NGX_PENDO_INITIALIZER_PROVIDER ] }; } diff --git a/projects/ngx-pendo/src/lib/ngx-pendo.provide.ts b/projects/ngx-pendo/src/lib/ngx-pendo.provide.ts index 3017db8..7f10d14 100644 --- a/projects/ngx-pendo/src/lib/ngx-pendo.provide.ts +++ b/projects/ngx-pendo/src/lib/ngx-pendo.provide.ts @@ -9,6 +9,6 @@ export function provideNgxPendo(settings: IPendoSettings) { provide: NGX_PENDO_SETTINGS_TOKEN, useValue: settings }, - NGX_PENDO_INITIALIZER_PROVIDER + settings.pendoInitializerProvider || NGX_PENDO_INITIALIZER_PROVIDER ]); } diff --git a/projects/ngx-pendo/src/lib/ngx-pendo.service.spec.ts b/projects/ngx-pendo/src/lib/ngx-pendo.service.spec.ts index 2cc66cd..7371b15 100644 --- a/projects/ngx-pendo/src/lib/ngx-pendo.service.spec.ts +++ b/projects/ngx-pendo/src/lib/ngx-pendo.service.spec.ts @@ -19,6 +19,8 @@ describe('NgxPendoService', () => { 'identify', 'updateOptions', 'teardown', + 'isAnonymousVisitor', + 'clearSession', 'enableDebugging', 'disableDebugging' ]); @@ -70,6 +72,14 @@ describe('NgxPendoService', () => { service.teardown(); expect(spyOnPendo.teardown).toHaveBeenCalledOnceWith(); + service.isAnonymousVisitor(visitor.id); + service.isAnonymousVisitor(); + expect(spyOnPendo.isAnonymousVisitor).toHaveBeenCalledTimes(2); + expect(spyOnPendo.isAnonymousVisitor).toHaveBeenCalledWith(visitor.id); + + service.clearSession(); + expect(spyOnPendo.clearSession).toHaveBeenCalledOnceWith(); + service.enableDebugging(); expect(spyOnPendo.enableDebugging).toHaveBeenCalledOnceWith(); diff --git a/projects/ngx-pendo/src/lib/ngx-pendo.service.ts b/projects/ngx-pendo/src/lib/ngx-pendo.service.ts index c570832..6ad1ba3 100644 --- a/projects/ngx-pendo/src/lib/ngx-pendo.service.ts +++ b/projects/ngx-pendo/src/lib/ngx-pendo.service.ts @@ -12,6 +12,7 @@ export class NgxPendoService { * Constructor * * @param settings IPendoSettings + * @param pendo IPendo */ constructor( @Inject(NGX_PENDO_SETTINGS_TOKEN) settings: IPendoSettings, @@ -30,9 +31,9 @@ export class NgxPendoService { initialize(visitor: IVisitor, account?: IAccount): void; initialize(optionsOrVisitor: IPendoOptions | IVisitor, account?: IAccount): void { if ('id' in optionsOrVisitor) { - this.pendo.initialize({ visitor: optionsOrVisitor, account }); + this.pendo?.initialize({ visitor: optionsOrVisitor, account }); } else { - this.pendo.initialize(optionsOrVisitor); + this.pendo?.initialize(optionsOrVisitor); } } @@ -46,9 +47,9 @@ export class NgxPendoService { identify(visitor: IVisitor, account?: IAccount): void; identify(visitor: IVisitor | string, account?: IAccount | string): void { if (typeof visitor === 'string' && (!account || typeof account === 'string')) { - this.pendo.identify(visitor, account); + this.pendo?.identify(visitor, account); } else { - this.pendo.identify({ visitor: visitor, account: account }); + this.pendo?.identify({ visitor: visitor, account: account }); } } @@ -58,7 +59,7 @@ export class NgxPendoService { * @param options IPendoOptions */ updateOptions(options: IPendoOptions): void { - this.pendo.updateOptions(options); + this.pendo?.updateOptions(options); } /** @@ -74,20 +75,36 @@ export class NgxPendoService { * Shuts down the agent and cleans up all timers and listeners. */ teardown(): void { - this.pendo.teardown(); + this.pendo?.teardown(); + } + + /** + * Checks if a given visitor id string is anonymous. + * If no argument is given, calls with pendo.getVisitorId() to check current visitor status. + */ + isAnonymousVisitor(visitorId?: string): boolean { + return this.pendo?.isAnonymousVisitor(visitorId); + } + + /** + * Removes current visitor id and account id. + * Triggers an identify event and reloads with a new anonymous visitor. + */ + clearSession(): void { + this.pendo?.clearSession(); } /** * Loads Pendo Debugger and extends the global pendo object with additional functionality for debugging purposes. */ enableDebugging(): void { - this.pendo.enableDebugging(); + this.pendo?.enableDebugging(); } /** * Removes Pendo Debugger extension. */ disableDebugging(): void { - this.pendo.disableDebugging(); + this.pendo?.disableDebugging(); } }