EmmanuelRoux / ngx-matomo-client

Matomo analytics client for Angular applications
https://matomo.org/
MIT License
74 stars 16 forks source link

Window is not defined (ssr prerendering) / Angular Universal #23

Closed Terriuss closed 2 years ago

Terriuss commented 2 years ago

Hi,

when using SSR and prerendering I get the issue, that "window" is not definded. I'm using ngx-matomo version 2.1.0

Console error: Prerendering 1 route(s) to C:\Development\xxx\dist\xxx\browser\be...Unhandled Promise rejection: window is not defined ; Zone: <root> ; Task: Promise.then ; Value: ReferenceError: window is not defined at initializeMatomoHolder (C:\Development\xxx\dist\xxx\server\be\main.js:3:8382901) at new MatomoInitializerService (C:\Development\xxx\dist\xxx\server\be\main.js:3:8399437) at createMatomoInitializer (C:\Development\xxx\dist\xxx\server\be\main.js:3:8399140) at Object.MatomoInitializerService_Factory [as factory] (C:\Development\xxx\dist\xxx\server\be\main.js:3:8401113) at R3Injector.hydrate (C:\Development\xxx\dist\xxx\server\be\main.js:3:6371603) at R3Injector.get (C:\Development\xxx\dist\xxx\server\be\main.js:3:6368724) at injectInjectorOnly (C:\Development\xxx\dist\xxx\server\be\main.js:3:6263915) at Object.ɵɵinject (C:\Development\xxx\dist\xxx\server\be\main.js:3:6264085) at Object.NgxMatomoTrackerModule_Factory [as factory] (C:\Development\xxx\dist\xxx\server\be\main.js:3:8401652) at R3Injector.hydrate (C:\Development\xxx\dist\xxx\server\be\main.js:3:6371603) ReferenceError: window is not defined at initializeMatomoHolder (C:\Development\xxx\dist\xxx\server\be\main.js:3:8382901) at new MatomoInitializerService (C:\Development\xxx\dist\xxx\server\be\main.js:3:8399437) at createMatomoInitializer (C:\Development\xxx\dist\xxx\server\be\main.js:3:8399140) at Object.MatomoInitializerService_Factory [as factory] (C:\Development\xxx\dist\xxx\server\be\main.js:3:8401113) at R3Injector.hydrate (C:\Development\xxx\dist\xxx\server\be\main.js:3:6371603) at R3Injector.get (C:\Development\xxx\dist\xxx\server\be\main.js:3:6368724) at injectInjectorOnly (C:\Development\xxx\dist\xxx\server\be\main.js:3:6263915) at Object.ɵɵinject (C:\Development\xxx\dist\xxx\server\be\main.js:3:6264085) at Object.NgxMatomoTrackerModule_Factory [as factory] (C:\Development\xxx\dist\xxx\server\be\main.js:3:8401652) at R3Injector.hydrate (C:\Development\xxx\dist\xxx\server\be\main.js:3:6371603) × Prerendering routes to C:\Development\xxx\dist\xxx\browser\be failed.

EmmanuelRoux commented 2 years ago

Hi @Terriuss,

Yes, as you might guess, Ngx-matomo cannot be used with SSR: you have to disable it when using SSR rendering.


In your root module, update your NgxMatomoTrackerModule import using the alternative way of providing configuration as described in the README and update your configuration provider to something like this :

import { isPlatformBrowser } from '@angular/common';
import { PLATFORM_ID } from '@angular/core';
import { NgxMatomoTrackerModule } from '@ngx-matomo/tracker';

@NgModule({
  // Import module without .forRoot() 
  imports: [NgxMatomoTrackerModule],

  // Provide your config as following
  providers: [
    {
      provide: MATOMO_CONFIGURATION,
      useFactory: matomoConfigFactory,
      deps: [PLATFORM_ID]
    },
  ],
})
export class AppModule {}

export function matomoConfigFactory(platformId: Object) {
  return {
    disabled: !isPlatformBrowser(platformId),
    // Put your existing configuration here instead of .forRoot()
    siteId: '…',
    trackerUrl: '…',
  };
}

Please let me know if this solves your issue

Maybe I will update this lib soon to handle such case automatically/silently :-)

Terriuss commented 2 years ago

@EmmanuelRoux thanks for the super fast reply. Works like a charm :)

github-actions[bot] commented 2 years ago

:tada: This issue has been resolved in version 2.2.0 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

EmmanuelRoux commented 2 years ago

@Terriuss With version 2.2.0, you should be able to remove this workaround. Tracker now auto-disables itself on SSR platform.