diff --git a/projects/ngx-matomo-client/form-analytics/matomo-form-analytics-initializer.service.spec.ts b/projects/ngx-matomo-client/form-analytics/matomo-form-analytics-initializer.service.spec.ts index 6b91c57..7acdf61 100644 --- a/projects/ngx-matomo-client/form-analytics/matomo-form-analytics-initializer.service.spec.ts +++ b/projects/ngx-matomo-client/form-analytics/matomo-form-analytics-initializer.service.spec.ts @@ -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, @@ -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(); + 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(); + }); }); diff --git a/projects/ngx-matomo-client/form-analytics/matomo-form-analytics-initializer.service.ts b/projects/ngx-matomo-client/form-analytics/matomo-form-analytics-initializer.service.ts index 2e2f5af..bbaad0e 100644 --- a/projects/ngx-matomo-client/form-analytics/matomo-form-analytics-initializer.service.ts +++ b/projects/ngx-matomo-client/form-analytics/matomo-form-analytics-initializer.service.ts @@ -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, @@ -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; @@ -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; diff --git a/projects/ngx-matomo-client/router/matomo-router.service.spec.ts b/projects/ngx-matomo-client/router/matomo-router.service.spec.ts index 972898e..33cef71 100644 --- a/projects/ngx-matomo-client/router/matomo-router.service.spec.ts +++ b/projects/ngx-matomo-client/router/matomo-router.service.spec.ts @@ -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'; @@ -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('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; + + // 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('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; + + // 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( diff --git a/projects/ngx-matomo-client/router/matomo-router.service.ts b/projects/ngx-matomo-client/router/matomo-router.service.ts index b87f005..b12024c 100644 --- a/projects/ngx-matomo-client/router/matomo-router.service.ts +++ b/projects/ngx-matomo-client/router/matomo-router.service.ts @@ -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 { @@ -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) @@ -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; }