-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow providing Matomo in lazy-loaded components/modules (#98)
fixes #97
- Loading branch information
1 parent
3d002d8
commit 2982fc4
Showing
28 changed files
with
828 additions
and
860 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
projects/ngx-matomo-client/core/testing/testing-tracker.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { ApplicationInitStatus, inject, Injectable, Provider } from '@angular/core'; | ||
import { | ||
InternalMatomoTracker, | ||
InternalMatomoTrackerType, | ||
} from '../tracker/internal-matomo-tracker.service'; | ||
import { PrefixedType } from '../utils/types'; | ||
|
||
export function provideTestingTracker(): Provider[] { | ||
return [ | ||
MatomoTestingTracker, | ||
{ | ||
provide: InternalMatomoTracker, | ||
useExisting: MatomoTestingTracker, | ||
}, | ||
]; | ||
} | ||
|
||
@Injectable() | ||
export class MatomoTestingTracker<MATOMO = unknown, PREFIX extends string = ''> | ||
implements InternalMatomoTrackerType | ||
{ | ||
private readonly initStatus = inject(ApplicationInitStatus); | ||
|
||
/** Get list of all calls until initialization */ | ||
callsOnInit: unknown[][] = []; | ||
/** Get list of all calls after initialization */ | ||
callsAfterInit: unknown[][] = []; | ||
|
||
/** Get a copy of all calls since application startup */ | ||
get calls(): unknown[] { | ||
return [...this.callsOnInit, ...this.callsAfterInit]; | ||
} | ||
|
||
countCallsAfterInit(command: string): number { | ||
return this.callsAfterInit.filter(call => call[0] === command).length; | ||
} | ||
|
||
reset() { | ||
this.callsOnInit = []; | ||
this.callsAfterInit = []; | ||
} | ||
|
||
/** Asynchronously call provided method name on matomo tracker instance */ | ||
async get<K extends keyof PrefixedType<MATOMO, PREFIX>>(_: K): Promise<never> { | ||
return Promise.reject('MatomoTracker is disabled'); | ||
} | ||
|
||
push(arg: unknown[]): void { | ||
if (this.initStatus.done) { | ||
this.callsAfterInit.push(arg); | ||
} else { | ||
this.callsOnInit.push(arg); | ||
} | ||
} | ||
|
||
async pushFn<T>(_: (matomo: PrefixedType<MATOMO, PREFIX>) => T): Promise<T> { | ||
return Promise.reject('MatomoTracker is disabled'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.