Closed pawcoding closed 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!
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
:
npm run build:lib
cd dist/ngx-matomo-client
npm link
cd <your project directory>
npm link ngx-matomo-client
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
. 👍🏼
@pawcoding Thanks for you work!
FYI, I added another commit to also disable form-analytics module
:tada: This PR is included in version 6.3.1 :tada:
The release is available on:
v6.3.1
Your semantic-release bot :package::rocket:
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:
While adding an
isPlatformBrowser
check to the interceptor would solve this, the README states:So, it seems like the interceptor shouldn't be executed on the server at all.
The Fix
Upon investigating, I found that:
core
package correctly checks the platform to initialize the tracker.MatomoRouter
does not skip initializing itself on the server and continues listening to router events and executing interceptors, leading to the error.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_ID
s 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.