Skip to content

Commit

Permalink
feat(service): add service methods, extend settings
Browse files Browse the repository at this point in the history
  • Loading branch information
MaximBelov authored and yociduo committed Sep 23, 2024
1 parent de54c23 commit e7bf295
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 11 deletions.
3 changes: 2 additions & 1 deletion projects/ngx-pendo/src/lib/ngx-pendo.interfaces.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion projects/ngx-pendo/src/lib/ngx-pendo.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export class NgxPendoModule {
provide: NGX_PENDO_SETTINGS_TOKEN,
useValue: settings
},
NGX_PENDO_INITIALIZER_PROVIDER
settings.pendoInitializerProvider || NGX_PENDO_INITIALIZER_PROVIDER
]
};
}
Expand Down
2 changes: 1 addition & 1 deletion projects/ngx-pendo/src/lib/ngx-pendo.provide.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
]);
}
10 changes: 10 additions & 0 deletions projects/ngx-pendo/src/lib/ngx-pendo.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ describe('NgxPendoService', () => {
'identify',
'updateOptions',
'teardown',
'isAnonymousVisitor',
'clearSession',
'enableDebugging',
'disableDebugging'
]);
Expand Down Expand Up @@ -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();

Expand Down
33 changes: 25 additions & 8 deletions projects/ngx-pendo/src/lib/ngx-pendo.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export class NgxPendoService {
* Constructor
*
* @param settings IPendoSettings
* @param pendo IPendo
*/
constructor(
@Inject(NGX_PENDO_SETTINGS_TOKEN) settings: IPendoSettings,
Expand All @@ -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);
}
}

Expand All @@ -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: <IVisitor>visitor, account: <IAccount>account });
this.pendo?.identify({ visitor: <IVisitor>visitor, account: <IAccount>account });
}
}

Expand All @@ -58,7 +59,7 @@ export class NgxPendoService {
* @param options IPendoOptions
*/
updateOptions(options: IPendoOptions): void {
this.pendo.updateOptions(options);
this.pendo?.updateOptions(options);
}

/**
Expand All @@ -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();
}
}

0 comments on commit e7bf295

Please sign in to comment.