Skip to content

Commit

Permalink
fix(router, form-analytics): disable page tracking on server (#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
pawcoding authored Sep 15, 2024
1 parent e5bedad commit 229083b
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Provider } from '@angular/core';
import { ɵPLATFORM_SERVER_ID } from '@angular/common';
import { PLATFORM_ID, Provider } from '@angular/core';
import { fakeAsync, TestBed, tick } from '@angular/core/testing';
import {
AutoMatomoConfiguration,
Expand Down Expand Up @@ -215,4 +216,25 @@ describe('MatomoFormAnalyticsInitializer', () => {
await initializer.initialize();
expect(formAnalytics.disableFormAnalytics).toHaveBeenCalledTimes(1);
});

it('should implicitly disable form analytics when not running in browser', async () => {
// Given
const pageViewTracked = new Subject<void>();
const initializer = await instantiate(
{},
{},
[{ provide: PLATFORM_ID, useValue: ɵPLATFORM_SERVER_ID }],
pageViewTracked,
);
const formAnalytics = TestBed.inject(MatomoFormAnalytics);

await initializer.initialize();
expect(formAnalytics.scanForForms).not.toHaveBeenCalled();

// When
pageViewTracked.next();
// Then
expect(formAnalytics.scanForForms).not.toHaveBeenCalled();
expect(formAnalytics.disableFormAnalytics).not.toHaveBeenCalled();
});
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { inject, Injectable, OnDestroy } from '@angular/core';
import { isPlatformBrowser } from '@angular/common';
import { inject, Injectable, OnDestroy, PLATFORM_ID } from '@angular/core';
import {
MatomoTracker,
ɵappendTrailingSlash as appendTrailingSlash,
Expand All @@ -24,6 +25,7 @@ export class MatomoFormAnalyticsInitializer implements OnDestroy {
private readonly scriptInjector = inject(ScriptInjector);
private readonly tracker = inject(MatomoTracker);
private readonly formAnalytics = inject(MatomoFormAnalytics);
private readonly platformId = inject(PLATFORM_ID);

private pageTrackedSubscription: Subscription | undefined;

Expand All @@ -32,6 +34,11 @@ export class MatomoFormAnalyticsInitializer implements OnDestroy {
}

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

if (this.config.disabled) {
this.formAnalytics.disableFormAnalytics();
return;
Expand Down
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 229083b

Please sign in to comment.