-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Load initial configuration async? #31
Comments
Hi @csgulyas, this seems to be an Angular issue, not a ngx-matomo issue. And you're right, Angular does not have such a feature currently (see Angular feature request: angular/angular#23279) Have you tried using |
You may end up with something like this: @Injectable({ providedIn: 'root' })
export class MatomoConfigService {
myBackendConfig: any;
async preload(): Promise<void> {
this.myBackendConfig = await ......; // http call
}
buildConfig(): MatomoConfiguration {
return {
siteId: this.myBackendConfig.siteId,
// ...
};
}
}
export function initializeMatomoFactory(service: MatomoConfigService) {
return () => service.preload();
}
export function matomoConfigFactory(service: MatomoConfigService) {
return service.buildConfig();
}
@NgModule({
// ...
providers: [
{
provide: APP_INITIALIZER,
useFactory: initializeMatomoFactory,
deps: [MatomoConfigService],
multi: true
},
{
provide: MATOMO_CONFIGURATION,
useFactory: matomoConfigFactory,
deps: [MatomoConfigService]
}
]
})
export class AppModule {} |
Thanks for your fast response. I tried your suggestion, but I don't think that approach is doable. After reading about I've made a minimal repo in case I was doing something wrong: https://github.com/csgulyas/matomotest. The |
Oh yes you're right, factory providers don't wait for But, an alternative is see, would be load load your backend config yourself before you start Angular. First declare a config type + injection token: export class MyBackendConfig {
// ...
}
const MY_BACKEND_CONFIG = new InjectionToken<MyBackendConfig>('My Backend Configuration'); Then in // main.ts
fetch('/my-backend-config.json')
.then(response => response.json())
.then((config: MyBackendConfig) => {
// Here include the classic bootstrap process
if (environment.production) {
enableProdMode();
}
// Just add a provider for the config as an argument to platformBrowserDynamic() call:
platformBrowserDynamic([
{ provide: MY_BACKEND_CONFIG, useValue: config },
])
.bootstrapModule(AppModule)
.catch(err => console.error(err));
}); Finally, you can declare your matomo config factory using // app.module.ts
export function matomoConfigFactory(myBackendConfig: MyBackendConfig) {
return {
siteId: myBackendConfig.siteId,
trackerUrl: myBackendConfig.trackerUrl,
// ...
};
}
@NgModule({
// ...
providers: [
{
provide: MATOMO_CONFIGURATION,
useFactory: matomoConfigFactory,
deps: [MY_BACKEND_CONFIG]
}
]
})
export class AppModule {} Drawback is you can't use here anything defined in your app (such as other providers, etc...) |
Appreciate your help. Very interesting approach. |
# [2.5.0](v2.4.2...v2.5.0) (2022-07-22) ### Bug Fixes * **tracker:** prevent initializing Matomo more than once ([8b1e773](8b1e773)) ### Code Refactoring * **tracker:** replace init() method with initialized() for naming consistency ([30c415d](30c415d)) ### Features * **tracker:** allow deferred trackers configuration ([77a377f](77a377f)), closes [#31](#31) [#54](#54) ### Deprecations * **tracker:** Method `MatomoInitializerService.init()` has been deprecated. Use `MatomoInitializerService.initialize()` instead.
🎉 This issue has been resolved in version 2.5.0 🎉 The release is available on:
Your semantic-release bot 📦🚀 |
# [3.1.0](v3.0.0...v3.1.0) (2022-07-22) ### Bug Fixes * **tracker:** prevent initializing Matomo more than once ([784b1de](784b1de)) ### Code Refactoring * **tracker:** replace init() method with initialized() for naming consistency ([8e23baf](8e23baf)) ### Features * **tracker:** allow deferred trackers configuration ([cd51156](cd51156)), closes [#31](#31) [#54](#54) ### Deprecations * **tracker:** Method `MatomoInitializerService.init()` has been deprecated. Use `MatomoInitializerService.initialize()` instead.
🎉 This issue has been resolved in version 3.1.0 🎉 The release is available on:
Your semantic-release bot 📦🚀 |
Is it currently possible to load the Site ID and Tracker URL in an async manner?
I have an Angular 10 project that stores every config in a database, and would need to query the backend before I can start setting up Matomo.
I looked at the docs and was hopeful maybe via provider useFactory I could return a Promise, but doesn't look doable.
The text was updated successfully, but these errors were encountered: