EmmanuelRoux / ngx-matomo-client

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

Disable page tracking on server #94

Closed pawcoding closed 2 months ago

pawcoding commented 2 months ago

The Problem

I've been using this package for various Angular projects, both with client-only and server-side rendering. However, I encountered an error where the server throws an error saying navigator is not defined.

This happens when I attempt to get the current language in an interceptor to add it as a custom dimension:

export const languageInterceptor: MatomoRouterInterceptorFn = () => {
  const tracker = inject(MatomoTracker);

  // This only works in the browser, because `navigator` is not defined on the server
  tracker.setCustomDimension(1, navigator.language);
}

While adding an isPlatformBrowser check to the interceptor would solve this, the README states:

Can I use ngx-matomo-client with Server-side rendering (SSR) / Angular Universal? Yes. ngx-matomo-client automatically disables itself on non-browser platforms.

So, it seems like the interceptor shouldn't be executed on the server at all.

The Fix

Upon investigating, I found that:

To align with the README, I have modified MatomoRouter to also skip initialization when running on the server.

Testing

I added two new test scenarios to cover both 'browser' and 'server' PLATFORM_ID values used by Angular. Both tests now pass as expected, confirming that the changes work correctly in both environments.

_Unfortunately Angular doesn't export the PLATFORM_IDs via a proper API but only with the internal ɵ prefix. Nevertheless I thought it's better to use them than hard-coding the strings just in case they might change the values in the future._

I haven't been able to test these changes in my project because I'm unsure how to install a local version without deploying it to npm.

Please let me know if this fix is acceptable or if the current behavior was indeed intended.

EmmanuelRoux commented 2 months ago

@pawcoding you're right, the lib was designed to disable only the low level tracking in SSR mode. It was not designed to also disable higher logic and prevent any client code call.

I agree we may also disable client code defined as library components, such as interceptors. I will take a look at your PR soon!

EmmanuelRoux commented 2 months ago

I haven't been able to test these changes in my project because I'm unsure how to install a local version without deploying it to npm.

@pawcoding You can build the lib locally then reference it in your project with npm link:

pawcoding commented 2 months ago

Thanks for the quick reply and the explanation for local links.

Unfortunately I use pnpm for my project and the link didn't work initially. Though I got it working now and the code works as expected.


I also updated the code with your suggestion to use !isPlatformBrowser. 👍🏼

EmmanuelRoux commented 2 months ago

@pawcoding Thanks for you work!

FYI, I added another commit to also disable form-analytics module

EmmanuelRoux commented 2 months ago

:tada: This PR is included in version 6.3.1 :tada:

The release is available on:

Your semantic-release bot :package::rocket: