Skip to content

Commit

Permalink
fix(router): fix NgxMatomoRouterModule not being correctly enabled …
Browse files Browse the repository at this point in the history
…without `.forRoot()` call

fix #68
  • Loading branch information
EmmanuelRoux committed Jul 25, 2023
1 parent 0b467e0 commit 61603b6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 13 deletions.
11 changes: 3 additions & 8 deletions projects/ngx-matomo-client/src/lib/ngx-matomo-providers.ts
Expand Up @@ -2,7 +2,6 @@ import {
ENVIRONMENT_INITIALIZER,
EnvironmentProviders,
inject,
InjectionToken,
makeEnvironmentProviders,
Provider,
Type,
Expand Down Expand Up @@ -141,13 +140,7 @@ export function withScriptFactory(scriptFactory: MatomoScriptFactory): MatomoFea

/** Enable automatic page views tracking */
export function withRouter(config?: MatomoRouterConfiguration): MatomoFeature {
const providers = provideRouterInternal(config);

return createMatomoFeature(MatomoFeatureKind.Router, providers);
}

export function provideRouterInternal(config?: MatomoRouterConfiguration): Provider[] {
return [
const providers = [
{ provide: MATOMO_ROUTER_ENABLED, useValue: true },
{ provide: MATOMO_ROUTER_CONFIGURATION, useValue: config },
{
Expand All @@ -158,6 +151,8 @@ export function provideRouterInternal(config?: MatomoRouterConfiguration): Provi
},
},
];

return createMatomoFeature(MatomoFeatureKind.Router, providers);
}

/** Add some matomo router interceptors */
Expand Down
29 changes: 24 additions & 5 deletions projects/ngx-matomo-client/src/lib/ngx-matomo-router.module.ts
@@ -1,17 +1,36 @@
import { ModuleWithProviders, NgModule } from '@angular/core';
import { provideRouterInternal } from './ngx-matomo-providers';
import { MatomoRouterConfigurationWithInterceptors } from './router/configuration';
import { ModuleWithProviders, NgModule, Optional, SkipSelf } from '@angular/core';
import {
MATOMO_ROUTER_CONFIGURATION,
MatomoRouterConfigurationWithInterceptors,
} from './router/configuration';
import { provideInterceptors } from './router/interceptor';
import { MatomoRouter } from './router/matomo-router.service';
import { MATOMO_ROUTER_ENABLED } from './tracker/configuration';

@NgModule()
@NgModule({
providers: [{ provide: MATOMO_ROUTER_ENABLED, useValue: true }],
})
export class NgxMatomoRouterModule {
constructor(
private readonly router: MatomoRouter,
@Optional() @SkipSelf() parent?: NgxMatomoRouterModule
) {
if (!parent) {
// Do not initialize if it is already (by a parent module)
this.router.initialize();
}
}

static forRoot({
interceptors,
...config
}: MatomoRouterConfigurationWithInterceptors = {}): ModuleWithProviders<NgxMatomoRouterModule> {
return {
ngModule: NgxMatomoRouterModule,
providers: [provideRouterInternal(config), provideInterceptors(interceptors)],
providers: [
{ provide: MATOMO_ROUTER_CONFIGURATION, useValue: config },
provideInterceptors(interceptors),
],
};
}
}

0 comments on commit 61603b6

Please sign in to comment.