Skip to content

Commit

Permalink
feat: add setPageViewId and getPageViewId to tracker api
Browse files Browse the repository at this point in the history
Note: those methods should probably not be called in a single-page app!
  • Loading branch information
EmmanuelRoux committed Nov 12, 2023
1 parent 8b92fc2 commit b234a7b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
Expand Up @@ -222,6 +222,20 @@ describe('MatomoTracker', () => {

it('should reset user id', expectSimpleMethod('resetUserId', []));

it('should set page view id', expectSimpleMethod('setPageViewId', ['my-id']));

it('should get page view id', done => {
expectGetter(
'getPageViewId',
{
getPageViewId(): string {
return 'fake-id';
},
},
'fake-id',
).then(done);
});

it(
'should set custom variable',
expectSimpleMethod('setCustomVariable', [1, 'name', 'value', 'page']),
Expand Down
20 changes: 20 additions & 0 deletions projects/ngx-matomo-client/core/tracker/matomo-tracker.service.ts
Expand Up @@ -82,6 +82,8 @@ export interface MatomoInstance {

getUserId(): string;

getPageViewId(): string;

getCustomVariable(index: number, scope: string): string;

getCustomDimension(customDimensionId: number): string;
Expand Down Expand Up @@ -748,6 +750,24 @@ export abstract class MatomoTracker {
this.push(['resetUserId']);
}

/**
* Override PageView id for every use of logPageView() <b>THIS SHOULD PROBABLY NOT BE CALLED IN A SINGLE-PAGE APP!</b>
*
* Do not use this if you call trackPageView() multiple times during tracking (e.g. when tracking a single page application)
*
* @param pageView
*/
setPageViewId(pageView: string): void {
this.push(['setPageViewId', pageView]);
}

/**
* Returns the PageView id. If not set manually using setPageViewId, this method will return the dynamic PageView id, used in the last tracked page view, or undefined if no page view was tracked yet
*/
getPageViewId(): Promise<string> {
return this.get('getPageViewId');
}

/**
* Set a custom variable.
*
Expand Down

0 comments on commit b234a7b

Please sign in to comment.