EmmanuelRoux / ngx-matomo-client

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

Passing async siteId and trackerUrl #54

Closed aicokleinovink closed 2 years ago

aicokleinovink commented 2 years ago

When importing the tracker module:

NgxMatomoTrackerModule.forRoot({ siteId: 'initialSiteId', trackerUrl: 'initialTrackerUrl'})

How can the siteId and trackerUrl first be retrieved async from a service, before matomo actually is trying to do something with these initial values?

I tried something like this:

function loadConfigurationFactory(configurationService: ConfigurationService): () => Observable<boolean> {
    return () => configurationService.configurationLoadedObservable; // this only emits when loading is complete
}

@NgModule({
    imports: [
        NgxMatomoTrackerModule.forRoot({ siteId: 'initialSiteId', trackerUrl: 'initialTrackerId' }),
        NgxMatomoRouterModule,
    ],
    providers: [
        ...
        {
            provide: APP_INITIALIZER,
            useFactory: loadConfigurationFactory,
            deps: [ConfigurationService],
            multi: true,
        }
    ]
})
export class AppModule {}

But how will I be able to set the url and site id after that??

EmmanuelRoux commented 2 years ago

Hi @aicokleinovink

Had you tried solution described in #31 ?

aicokleinovink commented 2 years ago

I just took a look at that solution, thanks for your quick reply. I understand this is an Angular shortcoming when importing a module like this. However, with the older versions of this package it was possible to initialize matomo in a later stage using the init function if I'm not wrong? I'm only running into this issue just now while upgrading from a 0.something version to 3.0.0.

The solution you provided doesn't really seem like a good way to do this my opinion.

EmmanuelRoux commented 2 years ago

with the older versions of this package it was possible to initialize matomo in a later stage using the init function

No, the initialization is done automatically when NgxMatomoTrackerModule is loaded.

I'm only running into this issue just now while upgrading from a 0.something version to 3.0.0.

There is no 0.x version of this lib: are you talking about version 1.x for Angular <= 12?

What version of angular are you using? Take a look at the compatibility table shown in the README

The solution you provided doesn't really seem like a good way to do this my opinion.

Why?

There is a (long) discussion about this on angular feature request 23279, especially comment by alxhub few days ago.

The problem is that some DI token may be instantiated BEFORE APP_INITIALIZER completes.

I understand the quick workaround using fetch before application bootstrap may not work in some environments (especially on mobile/WebView, etc…)

Maybe in the future a feature for asynchronous initialization will be implemented in this lib, of course feel free to provide a PR it will be greatly appreciated :)

aicokleinovink commented 2 years ago

There is no 0.x version of this lib: are you talking about version 1.x for Angular <= 12?

You're totally right, I just found out the application I'm working on was previously using this ngx-matomo package.

Fetching data may take quite a while unfortunately, so waiting for that before bootstrapping the application is a no-go.

Thanks so much for your time, I'll close this issue. I will take a look and see if I can provide a PR.

EmmanuelRoux commented 2 years ago

Fetching data may take quite a while unfortunately, so waiting for that before bootstrapping the application is a no-go.

I totally understand, but I have no simple solution for you right now.

I will try to find some time to work on it and I will keep you informed!

I will take a look and see if I can provide a PR.

Thank you so much! Even partial or draft work will be useful :)

EmmanuelRoux commented 2 years ago

@aicokleinovink I'm working on a solution, it should be released soon ;)

EmmanuelRoux commented 2 years ago

@aicokleinovink would it suit your needs if you can set tracker config properties (and only these: trackerUrl, siteId, etc.) asynchronously?

Other optional properties would still need to be set in forRoot (requireConsent, etc.)

EmmanuelRoux commented 2 years ago

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

The release is available on:

Your semantic-release bot :package::rocket:

EmmanuelRoux commented 2 years ago

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

The release is available on:

Your semantic-release bot :package::rocket:

EmmanuelRoux commented 2 years ago

@aicokleinovink @lousley New feature released: check the new section in README ;-) (Available only in versions ≥ 2.5.0 for Angular 13 and ≥ 3.1.0 for Angular 14)