Skip to content

Commit

Permalink
fix(tracker): prevent initializing Matomo more than once
Browse files Browse the repository at this point in the history
Cherry-picked from 784b1de
  • Loading branch information
EmmanuelRoux committed Jul 22, 2022
1 parent 30c415d commit 8b1e773
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
1 change: 1 addition & 0 deletions projects/tracker/src/lib/errors.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export const ALREADY_INJECTED_ERROR = 'Matomo trackers have already been initialized';
export const ALREADY_INITIALIZED_ERROR = 'Matomo has already been initialized';
26 changes: 20 additions & 6 deletions projects/tracker/src/lib/matomo-initializer.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
MatomoConsentMode,
MatomoInitializationMode,
} from './configuration';
import { ALREADY_INJECTED_ERROR } from './errors';
import { ALREADY_INITIALIZED_ERROR, ALREADY_INJECTED_ERROR } from './errors';
import { MatomoHolder } from './holder';
import { MatomoInitializerService } from './matomo-initializer.service';
import { MatomoTracker, NoopMatomoTracker } from './matomo-tracker.service';
Expand Down Expand Up @@ -487,6 +487,22 @@ describe('MatomoInitializerService', () => {
expect(tracker.setSiteId).toHaveBeenCalledOnceWith('fakeSiteId');
});

it('should map deprecated init() method to initialize()', () => {
// Given
const service = instantiate({
trackerUrl: '',
siteId: '',
});

spyOn(service, 'initialize');

// When
service.init();

// Then
expect(service.initialize).toHaveBeenCalledOnceWith();
});

it('should throw an error when initialized trackers more than once', () => {
// Given
const service = instantiate({
Expand All @@ -502,19 +518,17 @@ describe('MatomoInitializerService', () => {
);
});

it('should map deprecated init() method to initialize()', () => {
it('should throw an error when initialized more than once', () => {
// Given
const service = instantiate({
trackerUrl: '',
siteId: '',
});

spyOn(service, 'initialize');

// When
service.init();
service.initialize();

// Then
expect(service.initialize).toHaveBeenCalledOnceWith();
expect(() => service.initialize()).toThrowError(ALREADY_INITIALIZED_ERROR);
});
});
9 changes: 8 additions & 1 deletion projects/tracker/src/lib/matomo-initializer.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
MatomoInitializationMode,
MatomoTrackerConfiguration,
} from './configuration';
import { ALREADY_INJECTED_ERROR } from './errors';
import { ALREADY_INITIALIZED_ERROR, ALREADY_INJECTED_ERROR } from './errors';
import { initializeMatomoHolder } from './holder';
import { MatomoTracker } from './matomo-tracker.service';
import { MATOMO_SCRIPT_FACTORY, MatomoScriptFactory } from './script-factory';
Expand Down Expand Up @@ -76,6 +76,7 @@ export class NoopMatomoInitializer
],
})
export class MatomoInitializerService {
private initialized = false;
private injected = false;

constructor(
Expand All @@ -95,9 +96,15 @@ export class MatomoInitializerService {
initialize(): void {
this.runPreInitTasks();

if (this.initialized) {
throw new Error(ALREADY_INITIALIZED_ERROR);
}

if (isAutoConfigurationMode(this.config)) {
this.injectMatomoScript(this.config);
}

this.initialized = true;
}

initializeTracker(config: AutoMatomoConfiguration<MatomoInitializationMode.AUTO_DEFERRED>): void {
Expand Down

0 comments on commit 8b1e773

Please sign in to comment.