Skip to content

Commit

Permalink
refactor(tracker): replace init() method with initialized() for namin…
Browse files Browse the repository at this point in the history
…g consistency

Cherry-picked from 8e23baf

DEPRECATION:
Method `MatomoInitializerService.init()` has been deprecated.
Use `MatomoInitializerService.initialize()` instead.
  • Loading branch information
EmmanuelRoux committed Jul 22, 2022
1 parent 77a377f commit 30c415d
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 19 deletions.
48 changes: 32 additions & 16 deletions projects/tracker/src/lib/matomo-initializer.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ describe('MatomoInitializerService', () => {
spyOn(tracker, 'trackPageView');

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

// Then
expect(tracker.trackPageView).toHaveBeenCalledOnceWith();
Expand All @@ -102,7 +102,7 @@ describe('MatomoInitializerService', () => {
spyOn(tracker, 'enableLinkTracking');

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

// Then
expect(tracker.enableLinkTracking).toHaveBeenCalledOnceWith();
Expand All @@ -122,7 +122,7 @@ describe('MatomoInitializerService', () => {
spyOn(tracker, 'setDoNotTrack');

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

// Then
expect(tracker.trackPageView).toHaveBeenCalledOnceWith();
Expand All @@ -144,7 +144,7 @@ describe('MatomoInitializerService', () => {
spyOn(tracker, 'requireConsent');

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

// Then
expect(tracker.trackPageView).toHaveBeenCalledOnceWith();
Expand All @@ -166,7 +166,7 @@ describe('MatomoInitializerService', () => {
spyOn(tracker, 'requireCookieConsent');

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

// Then
expect(tracker.trackPageView).toHaveBeenCalledOnceWith();
Expand All @@ -187,7 +187,7 @@ describe('MatomoInitializerService', () => {
spyOn(tracker, 'enableJSErrorTracking');

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

// Then
expect(tracker.trackPageView).toHaveBeenCalledTimes(1);
Expand Down Expand Up @@ -234,7 +234,7 @@ describe('MatomoInitializerService', () => {
setUpScriptInjection(script => (injectedScript = script));

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

// Then
expectInjectedScript(injectedScript, 'http://fakeTrackerUrl/matomo.js');
Expand All @@ -256,7 +256,7 @@ describe('MatomoInitializerService', () => {
setUpScriptInjection(script => (injectedScript = script));

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

// Then
expectInjectedScript(injectedScript, 'http://fakeTrackerUrl/matomo.js');
Expand All @@ -276,7 +276,7 @@ describe('MatomoInitializerService', () => {
setUpScriptInjection(script => (injectedScript = script));

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

// Then
expectInjectedScript(injectedScript, 'http://myCustomScriptUrl');
Expand All @@ -296,7 +296,7 @@ describe('MatomoInitializerService', () => {
setUpScriptInjection(script => (injectedScript = script));

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

// Then
expectInjectedScript(injectedScript, 'http://myCustomScript.js');
Expand All @@ -322,7 +322,7 @@ describe('MatomoInitializerService', () => {
setUpScriptInjection(script => (injectedScript = script));

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

// Then
expectInjectedScript(injectedScript, 'http://fakeTrackerUrl1/matomo.js');
Expand Down Expand Up @@ -355,7 +355,7 @@ describe('MatomoInitializerService', () => {
setUpScriptInjection(script => (injectedScript = script));

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

// Then
expectInjectedScript(injectedScript, 'http://fakeTrackerUrl1/matomo.js');
Expand All @@ -381,7 +381,7 @@ describe('MatomoInitializerService', () => {
setUpScriptInjection(script => (injectedScript = script));

// When
service.init();
service.initialize();
service.initializeTracker({ trackerUrl: '', siteId: '' });

// Then
Expand All @@ -406,7 +406,7 @@ describe('MatomoInitializerService', () => {
setUpScriptInjection(script => (injectedScript = script));

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

// Then
expect(injectedScript).toBeUndefined();
Expand Down Expand Up @@ -445,7 +445,7 @@ describe('MatomoInitializerService', () => {
setUpScriptInjection(script => (injectedScript = script));

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

// Then
expect(injectedScript?.src).toMatch('^(.+://[^/]+)?/fake/script/url$');
Expand All @@ -467,7 +467,7 @@ describe('MatomoInitializerService', () => {
setUpScriptInjection(script => (injectedScript = script));

// When
service.init();
service.initialize();
// Then
expect(injectedScript).toBeFalsy();
expect(tracker.setTrackerUrl).not.toHaveBeenCalled();
Expand Down Expand Up @@ -501,4 +501,20 @@ describe('MatomoInitializerService', () => {
ALREADY_INJECTED_ERROR
);
});

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();
});
});
9 changes: 7 additions & 2 deletions projects/tracker/src/lib/matomo-initializer.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ export function createMatomoInitializer(
}

export class NoopMatomoInitializer
implements Pick<MatomoInitializerService, 'init' | 'initializeTracker'>
implements Pick<MatomoInitializerService, 'initialize' | 'initializeTracker'>
{
init(): void {
initialize(): void {
// No-op
}

Expand Down Expand Up @@ -87,7 +87,12 @@ export class MatomoInitializerService {
initializeMatomoHolder();
}

/** @deprecated use {@link initialize initialize()} instead */
init(): void {
this.initialize();
}

initialize(): void {
this.runPreInitTasks();

if (isAutoConfigurationMode(this.config)) {
Expand Down
2 changes: 1 addition & 1 deletion projects/tracker/src/lib/ngx-matomo-tracker.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class NgxMatomoTrackerModule {
) {
if (!parent) {
// Do not initialize if it is already (by a parent module)
this.initializer.init();
this.initializer.initialize();
}
}

Expand Down

0 comments on commit 30c415d

Please sign in to comment.