Skip to content

Commit

Permalink
fix(router): disable page tracking on server
Browse files Browse the repository at this point in the history
  • Loading branch information
pawcoding committed Sep 13, 2024
1 parent e5bedad commit f357f79
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 4 deletions.
45 changes: 44 additions & 1 deletion projects/ngx-matomo-client/router/matomo-router.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Provider } from '@angular/core';
import { ɵPLATFORM_BROWSER_ID, ɵPLATFORM_SERVER_ID } from '@angular/common';
import { PLATFORM_ID, Provider } from '@angular/core';
import { fakeAsync, flush, TestBed, tick } from '@angular/core/testing';
import { Event, NavigationEnd, Router } from '@angular/router';
import { MATOMO_CONFIGURATION, MatomoTracker } from 'ngx-matomo-client/core';
Expand Down Expand Up @@ -296,6 +297,48 @@ describe('MatomoRouter', () => {
expect(tracker.setReferrerUrl).not.toHaveBeenCalled();
}));

it('should track page view if in browser', fakeAsync(() => {
// Given
const interceptor = jasmine.createSpyObj<MatomoRouterInterceptor>('interceptor', [
'beforePageTrack',
]);
const service = instantiate({}, {}, [
{ provide: PLATFORM_ID, useValue: ɵPLATFORM_BROWSER_ID },
{ provide: MATOMO_ROUTER_INTERCEPTORS, multi: true, useValue: interceptor },
]);
const tracker = TestBed.inject(MatomoTracker) as jasmine.SpyObj<MatomoTracker>;

// When
service.initialize();
triggerEvent('/');
tick(); // Tracking is asynchronous by default

// Then
expect(tracker.trackPageView).toHaveBeenCalled();
expect(interceptor.beforePageTrack).toHaveBeenCalled();
}));

it('should not track page view if on server', fakeAsync(() => {
// Given
const interceptor = jasmine.createSpyObj<MatomoRouterInterceptor>('interceptor', [
'beforePageTrack',
]);
const service = instantiate({}, {}, [
{ provide: PLATFORM_ID, useValue: ɵPLATFORM_SERVER_ID },
{ provide: MATOMO_ROUTER_INTERCEPTORS, multi: true, useValue: interceptor },
]);
const tracker = TestBed.inject(MatomoTracker) as jasmine.SpyObj<MatomoTracker>;

// When
service.initialize();
triggerEvent('/');
tick(); // Tracking is asynchronous by default

// Then
expect(tracker.trackPageView).not.toHaveBeenCalled();
expect(interceptor.beforePageTrack).not.toHaveBeenCalled();
}));

it('should track page view if navigated to the same url with different query params', fakeAsync(() => {
// Given
const service = instantiate(
Expand Down
9 changes: 6 additions & 3 deletions projects/ngx-matomo-client/router/matomo-router.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Inject, Injectable, Optional } from '@angular/core';
import { isPlatformBrowser } from '@angular/common';
import { Inject, Injectable, Optional, PLATFORM_ID } from '@angular/core';
import { Event, NavigationEnd, Router } from '@angular/router';
import { MatomoTracker, ɵrunOnce as runOnce } from 'ngx-matomo-client/core';
import {
Expand Down Expand Up @@ -79,6 +80,8 @@ function getNavigationEndComparator(config: InternalRouterConfiguration): Naviga
export class MatomoRouter {
constructor(
private readonly router: Router,
@Inject(PLATFORM_ID)
private readonly platformId: object,
@Inject(INTERNAL_ROUTER_CONFIGURATION)
private readonly config: InternalRouterConfiguration,
@Inject(MATOMO_PAGE_TITLE_PROVIDER)
Expand All @@ -101,8 +104,8 @@ export class MatomoRouter {
}

readonly initialize = runOnce(() => {
if (this.config.disabled) {
// Do not set-up router if globally disabled
if (this.config.disabled || !isPlatformBrowser(this.platformId)) {
// Do not set-up router if globally disabled or running on server
return;
}

Expand Down

0 comments on commit f357f79

Please sign in to comment.