Skip to content

Commit 3c20676

Browse files
committed
revert: feat: allow providing Matomo in lazy-loaded components/modules (#98)
This reverts commit 45a5a0e.
1 parent a2ca6c4 commit 3c20676

24 files changed

+788
-766
lines changed

angular.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@
2727
"options": {
2828
"main": "projects/ngx-matomo-client/test.ts",
2929
"tsConfig": "projects/ngx-matomo-client/tsconfig.spec.json",
30-
"karmaConfig": "projects/ngx-matomo-client/karma.conf.js",
31-
"codeCoverageExclude": ["projects/ngx-matomo-client/core/testing/**"]
30+
"karmaConfig": "projects/ngx-matomo-client/karma.conf.js"
3231
}
3332
},
3433
"lint": {

projects/demo/src/app/app.component.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { NO_ERRORS_SCHEMA } from '@angular/core';
22
import { TestBed } from '@angular/core/testing';
33
import { RouterTestingModule } from '@angular/router/testing';
4-
import { MatomoModule, MatomoRouterModule } from 'ngx-matomo-client';
4+
import { MatomoConfiguration, MatomoRouterModule, MatomoModule } from 'ngx-matomo-client';
55
import { AppComponent } from './app.component';
66

77
describe('AppComponent', () => {
@@ -12,8 +12,8 @@ describe('AppComponent', () => {
1212
MatomoModule.forRoot({
1313
trackerUrl: '',
1414
siteId: '',
15-
}),
16-
MatomoRouterModule.forRoot(),
15+
} as MatomoConfiguration),
16+
MatomoRouterModule,
1717
],
1818
declarations: [AppComponent],
1919
schemas: [NO_ERRORS_SCHEMA],

projects/ngx-matomo-client/core/directives/matomo-opt-out-form.component.spec.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import { Component, LOCALE_ID } from '@angular/core';
22
import { fakeAsync, flush, TestBed } from '@angular/core/testing';
33
import { By, DomSanitizer, SafeResourceUrl } from '@angular/platform-browser';
4-
import { provideMatomo } from '../providers';
5-
import { provideTestingTracker } from '../testing/testing-tracker';
64
import {
75
ASYNC_INTERNAL_MATOMO_CONFIGURATION,
86
INTERNAL_MATOMO_CONFIGURATION,
97
InternalMatomoConfiguration,
8+
MATOMO_CONFIGURATION,
9+
MatomoConfiguration,
1010
} from '../tracker/configuration';
11+
import { MatomoInitializerService } from '../tracker/matomo-initializer.service';
1112
import { MatomoOptOutFormComponent } from './matomo-opt-out-form.component';
1213

1314
@Component({
@@ -101,14 +102,18 @@ describe('MatomoOptOutFormComponent', () => {
101102
HostWithoutLocaleComponent,
102103
],
103104
providers: [
104-
provideMatomo({ siteId: 1, trackerUrl: 'http://localhost' }),
105-
provideTestingTracker(),
105+
{
106+
provide: MATOMO_CONFIGURATION,
107+
useValue: { siteId: 1, trackerUrl: 'http://localhost' } as MatomoConfiguration,
108+
},
106109
{
107110
provide: LOCALE_ID,
108111
useValue: 'en',
109112
},
110113
],
111114
}).compileComponents();
115+
116+
TestBed.inject(MatomoInitializerService).initialize();
112117
});
113118

114119
it('should create', async () => {

projects/ngx-matomo-client/core/private-api.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,4 @@ export {
1212
isAutoConfigurationMode as ɵisAutoConfigurationMode,
1313
} from './tracker/configuration';
1414
export { InternalMatomoTracker as ɵInternalMatomoTracker } from './tracker/internal-matomo-tracker.service';
15-
export {
16-
provideTestingTracker as ɵprovideTestingTracker,
17-
MatomoTestingTracker as ɵMatomoTestingTracker,
18-
} from './testing/testing-tracker';
1915
export { createMatomoFeature as ɵcreateMatomoFeature } from './providers';

projects/ngx-matomo-client/core/providers.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ describe('providers', () => {
2121
const config: MatomoConfiguration = { trackerUrl: 'my-tracker', siteId: 42 };
2222

2323
await setUp([
24-
provideMatomo(config),
2524
{
2625
provide: MatomoInitializerService,
2726
useValue: fakeInitializer,
2827
},
28+
provideMatomo(config),
2929
]);
3030

3131
expect(TestBed.inject(MatomoTracker)).toEqual(jasmine.any(MatomoTracker));
@@ -40,7 +40,6 @@ describe('providers', () => {
4040
const trackerUrlToken = new InjectionToken<string>('trackerUrl');
4141

4242
await setUp([
43-
provideMatomo(() => ({ trackerUrl: TestBed.inject(trackerUrlToken), siteId: 42 })),
4443
{
4544
provide: MatomoInitializerService,
4645
useValue: fakeInitializer,
@@ -49,6 +48,7 @@ describe('providers', () => {
4948
provide: trackerUrlToken,
5049
useValue: trackerUrl,
5150
},
51+
provideMatomo(() => ({ trackerUrl: TestBed.inject(trackerUrlToken), siteId: 42 })),
5252
]);
5353

5454
expect(TestBed.inject(MatomoTracker)).toEqual(jasmine.any(MatomoTracker));

projects/ngx-matomo-client/core/providers.ts

Lines changed: 2 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,9 @@ import {
55
makeEnvironmentProviders,
66
Provider,
77
} from '@angular/core';
8-
import {
9-
ASYNC_INTERNAL_MATOMO_CONFIGURATION,
10-
createDeferredInternalMatomoConfiguration,
11-
createInternalMatomoConfiguration,
12-
DEFERRED_INTERNAL_MATOMO_CONFIGURATION,
13-
INTERNAL_MATOMO_CONFIGURATION,
14-
MATOMO_CONFIGURATION,
15-
MatomoConfiguration,
16-
} from './tracker/configuration';
17-
import {
18-
createInternalMatomoTracker,
19-
InternalMatomoTracker,
20-
} from './tracker/internal-matomo-tracker.service';
21-
import {
22-
createMatomoInitializer,
23-
MatomoInitializerService,
24-
} from './tracker/matomo-initializer.service';
25-
import { MatomoTracker } from './tracker/matomo-tracker.service';
8+
import { MATOMO_CONFIGURATION, MatomoConfiguration } from './tracker/configuration';
9+
import { MatomoInitializerService } from './tracker/matomo-initializer.service';
2610
import { MATOMO_SCRIPT_FACTORY, MatomoScriptFactory } from './tracker/script-factory';
27-
import { ScriptInjector } from './utils/script-injector';
2811

2912
const PRIVATE_MATOMO_PROVIDERS = Symbol('MATOMO_PROVIDERS');
3013
const PRIVATE_MATOMO_CHECKS = Symbol('MATOMO_CHECKS');
@@ -91,28 +74,6 @@ export function provideMatomo(
9174
...features: MatomoFeature[]
9275
): EnvironmentProviders {
9376
const providers: Provider[] = [
94-
MatomoTracker,
95-
ScriptInjector,
96-
{
97-
provide: InternalMatomoTracker,
98-
useFactory: createInternalMatomoTracker,
99-
},
100-
{
101-
provide: MatomoInitializerService,
102-
useFactory: createMatomoInitializer,
103-
},
104-
{
105-
provide: INTERNAL_MATOMO_CONFIGURATION,
106-
useFactory: createInternalMatomoConfiguration,
107-
},
108-
{
109-
provide: DEFERRED_INTERNAL_MATOMO_CONFIGURATION,
110-
useFactory: createDeferredInternalMatomoConfiguration,
111-
},
112-
{
113-
provide: ASYNC_INTERNAL_MATOMO_CONFIGURATION,
114-
useFactory: () => inject(DEFERRED_INTERNAL_MATOMO_CONFIGURATION).configuration,
115-
},
11677
{
11778
provide: ENVIRONMENT_INITIALIZER,
11879
multi: true,

projects/ngx-matomo-client/core/testing/testing-tracker.ts

Lines changed: 0 additions & 59 deletions
This file was deleted.

projects/ngx-matomo-client/core/tracker/configuration.ts

Lines changed: 49 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -20,61 +20,69 @@ export const MATOMO_CONFIGURATION = new InjectionToken<MatomoConfiguration>('MAT
2020
*/
2121
export const INTERNAL_MATOMO_CONFIGURATION = new InjectionToken<InternalMatomoConfiguration>(
2222
'INTERNAL_MATOMO_CONFIGURATION',
23+
{
24+
factory(): InternalMatomoConfiguration {
25+
const { mode, requireConsent, ...restConfig } = requireNonNull(
26+
inject(MATOMO_CONFIGURATION, { optional: true }),
27+
CONFIG_NOT_FOUND,
28+
);
29+
30+
return {
31+
mode: mode ? coerceInitializationMode(mode) : undefined,
32+
disabled: false,
33+
enableLinkTracking: true,
34+
trackAppInitialLoad: !inject(MATOMO_ROUTER_ENABLED),
35+
requireConsent: requireConsent ? coerceConsentRequirement(requireConsent) : 'none',
36+
enableJSErrorTracking: false,
37+
runOutsideAngularZone: false,
38+
disableCampaignParameters: false,
39+
acceptDoNotTrack: false,
40+
...restConfig,
41+
};
42+
},
43+
},
2344
);
2445

25-
export function createInternalMatomoConfiguration(): InternalMatomoConfiguration {
26-
const { mode, requireConsent, ...restConfig } = requireNonNull(
27-
inject(MATOMO_CONFIGURATION, { optional: true }),
28-
CONFIG_NOT_FOUND,
29-
);
30-
31-
return {
32-
mode: mode ? coerceInitializationMode(mode) : undefined,
33-
disabled: false,
34-
enableLinkTracking: true,
35-
trackAppInitialLoad: !inject(MATOMO_ROUTER_ENABLED),
36-
requireConsent: requireConsent ? coerceConsentRequirement(requireConsent) : 'none',
37-
enableJSErrorTracking: false,
38-
runOutsideAngularZone: false,
39-
disableCampaignParameters: false,
40-
acceptDoNotTrack: false,
41-
...restConfig,
42-
};
43-
}
44-
4546
/**
4647
* For internal use only. Injection token for deferred {@link InternalMatomoConfiguration}.
4748
*
4849
*/
4950
export const DEFERRED_INTERNAL_MATOMO_CONFIGURATION =
50-
new InjectionToken<DeferredInternalMatomoConfiguration>('DEFERRED_INTERNAL_MATOMO_CONFIGURATION');
51-
52-
export function createDeferredInternalMatomoConfiguration(): DeferredInternalMatomoConfiguration {
53-
const base = inject(INTERNAL_MATOMO_CONFIGURATION);
54-
let resolveFn: ((configuration: InternalMatomoConfiguration) => void) | undefined;
55-
const configuration = new Promise<InternalMatomoConfiguration>(resolve => (resolveFn = resolve));
56-
57-
return {
58-
configuration,
59-
markReady(configuration) {
60-
requireNonNull(
61-
resolveFn,
62-
'resolveFn',
63-
)({
64-
...base,
65-
...configuration,
66-
});
51+
new InjectionToken<DeferredInternalMatomoConfiguration>(
52+
'DEFERRED_INTERNAL_MATOMO_CONFIGURATION',
53+
{
54+
factory: () => {
55+
const base = inject(INTERNAL_MATOMO_CONFIGURATION);
56+
let resolveFn: ((configuration: InternalMatomoConfiguration) => void) | undefined;
57+
const configuration = new Promise<InternalMatomoConfiguration>(
58+
resolve => (resolveFn = resolve),
59+
);
60+
61+
return {
62+
configuration,
63+
markReady(configuration) {
64+
requireNonNull(
65+
resolveFn,
66+
'resolveFn',
67+
)({
68+
...base,
69+
...configuration,
70+
} as InternalMatomoConfiguration);
71+
},
72+
};
73+
},
6774
},
68-
};
69-
}
75+
);
7076

7177
/**
7278
* For internal use only. Injection token for fully loaded async {@link InternalMatomoConfiguration}.
7379
*
7480
*/
7581
export const ASYNC_INTERNAL_MATOMO_CONFIGURATION = new InjectionToken<
7682
Promise<InternalMatomoConfiguration>
77-
>('ASYNC_INTERNAL_MATOMO_CONFIGURATION');
83+
>('ASYNC_INTERNAL_MATOMO_CONFIGURATION', {
84+
factory: () => inject(DEFERRED_INTERNAL_MATOMO_CONFIGURATION).configuration,
85+
});
7886

7987
/**
8088
* For internal use only. Module configuration merged with default values.
@@ -89,7 +97,7 @@ export type InternalMatomoConfiguration = Omit<MatomoConfiguration, 'mode' | 're
8997
export interface DeferredInternalMatomoConfiguration {
9098
readonly configuration: Promise<InternalMatomoConfiguration>;
9199

92-
markReady(configuration: InternalMatomoConfiguration): void;
100+
markReady(configuration: AutoMatomoConfiguration<'auto' | 'deferred'>): void;
93101
}
94102

95103
/**

projects/ngx-matomo-client/core/tracker/internal-matomo-tracker.service.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ function trimTrailingUndefinedElements<T>(array: T[]): T[] {
1818
return trimmed;
1919
}
2020

21-
export type InternalMatomoTrackerType = Pick<
21+
type InternalMatomoTrackerType = Pick<
2222
InternalMatomoTracker<unknown, string>,
2323
'get' | 'push' | 'pushFn'
2424
>;
@@ -30,7 +30,10 @@ export function createInternalMatomoTracker(): InternalMatomoTrackerType {
3030
return disabled || !isBrowser ? new NoopMatomoTracker() : new InternalMatomoTracker();
3131
}
3232

33-
@Injectable()
33+
@Injectable({
34+
providedIn: 'root',
35+
useFactory: createInternalMatomoTracker,
36+
})
3437
export class InternalMatomoTracker<MATOMO, PREFIX extends string = ''> {
3538
private readonly ngZone = inject(NgZone);
3639
private readonly config = inject(INTERNAL_MATOMO_CONFIGURATION);
@@ -67,7 +70,6 @@ export class InternalMatomoTracker<MATOMO, PREFIX extends string = ''> {
6770
}
6871
}
6972

70-
@Injectable()
7173
export class NoopMatomoTracker<MATOMO = unknown, PREFIX extends string = ''>
7274
implements InternalMatomoTrackerType
7375
{

0 commit comments

Comments
 (0)