EmmanuelRoux / ngx-matomo-client

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

Lazyload forRoot #97

Open raphael22 opened 2 weeks ago

raphael22 commented 2 weeks ago

Hello, How a child module could initialize matomo?

I've got a module-federation architecture in which I want one of my module to use matomo. But, as forbidden by guidelines, I can't use forRoot in my child module nor provide a root configuration.

I could inject matomo in my shell app, but that's mean my child module would be dependent on my shell deployment.

Bon weekend :)

EmmanuelRoux commented 2 weeks ago

Hi @raphael22

You typically should import the module using .forRoot in your root module, and import the Matomo module without .forRoot in child (lazy) modules.

If you really want to call .forRoot in your child module, it should still work though... :) (but Matomo will not be accessible in other modules)

If what you want to achieve is lazy-initialization of Matomo (but still use it in the whole app), take a look at how to defer initialization

raphael22 commented 2 weeks ago

Well, I tried few settings with defer but always result in a No Matomo configuration found! error :/

EmmanuelRoux commented 2 weeks ago

Could you please provide a repro to investigate ? Or share your config ?

raphael22 commented 2 weeks ago

Will reproduce a basic module-federation later. Config is either provideMatomo({ mode: 'deferred' }) or forRoot equivalent. Maybe loadRemoteModule from "@angular-architects/module-federation-runtime" is not compatible with library as is. PS: it was okay with ngx-matomo lib.

EmmanuelRoux commented 2 weeks ago

If use Matomo only in one micro-front end (child) module, when you use .forRoot/provideMatomo, what is the error you are getting?

At least, share your config, otherwise it's difficult to understand the problem

raphael22 commented 1 week ago

No config found is the error I get if forRoot is coming from a lazyloaded MF module. Angular 17.3.0 @angular-architects/module-federation 17.0.8 ngx-matomo-client 6.0.2 node 20

EmmanuelRoux commented 1 week ago

if forRoot is coming from a lazyloaded MF module

What do you precisely mean?

Can you share your config please? So we can investigate

raphael22 commented 1 week ago

repro incoming I've got my master/shell module which lazyload a child/federation module. My master module doesn't know matomo, only my child module does and try to provide it in root. What config do you need? It's really just a basic provideMatomo with siteId/url or with mode: deferred, I tried a bunch of settings always resulting in the no config error.

raphael22 commented 1 week ago

Here a basic reproduction You should be stuck on "loading..." after install/serve with error in browser console. A module federation test component with it's ngModule and matomo is in angular-app/

EmmanuelRoux commented 1 week ago

@raphael22 Thank you for the repro, I understand the use case

It was not initially designed this way, but it seems a valid use case. I will take a look and probably publish a solution soon (PR welcome if you have some time !)

raphael22 commented 1 week ago

Not familiar with matomo.js but I could take a look if you have a branch to lead me a bit.

EmmanuelRoux commented 1 week ago

You can fork this project then create a PR.

In my mind, the required changes are to remove all {providedIn: ‘root’} on @Injectable annotations, and replace them by providers in existing provideXXX functions instead. You can sure start working on that, and I will take a look soon, when I have some time. Thank you so much