Skip to content

Commit

Permalink
feat(tracker): add new disableCampaignParameters configuration option
Browse files Browse the repository at this point in the history
  • Loading branch information
EmmanuelRoux committed May 28, 2024
1 parent 4cfda65 commit 88258fa
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 11 deletions.
32 changes: 21 additions & 11 deletions projects/ngx-matomo-client/core/tracker/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ export const MATOMO_CONFIGURATION = new InjectionToken<MatomoConfiguration>('MAT
export const INTERNAL_MATOMO_CONFIGURATION = new InjectionToken<InternalMatomoConfiguration>(
'INTERNAL_MATOMO_CONFIGURATION',
{
factory: () =>
({
disabled: false,
enableLinkTracking: true,
trackAppInitialLoad: !inject(MATOMO_ROUTER_ENABLED),
requireConsent: MatomoConsentMode.NONE,
enableJSErrorTracking: false,
runOutsideAngularZone: false,
acceptDoNotTrack: false,
...requireNonNull(inject(MATOMO_CONFIGURATION, { optional: true }), CONFIG_NOT_FOUND),
}) as InternalMatomoConfiguration,
factory: (): InternalMatomoConfiguration => ({
disabled: false,
enableLinkTracking: true,
trackAppInitialLoad: !inject(MATOMO_ROUTER_ENABLED),
requireConsent: MatomoConsentMode.NONE,
enableJSErrorTracking: false,
runOutsideAngularZone: false,
disableCampaignParameters: false,
acceptDoNotTrack: false,
...requireNonNull(inject(MATOMO_CONFIGURATION, { optional: true }), CONFIG_NOT_FOUND),
}),
},
);

Expand Down Expand Up @@ -179,6 +179,16 @@ export interface BaseMatomoConfiguration {

/** Set to `true` to run matomo calls outside of angular NgZone. This may fix angular freezes. */
runOutsideAngularZone?: boolean;

/**
* Set to `true` to avoid sending campaign parameters
*
* By default, Matomo will send campaign parameters (mtm, utm, etc.) to the tracker and record that information.
* Some privacy regulations may not allow for this information to be collected.
*
* <b>This is available as of Matomo 5.1 only.</b>
*/
disableCampaignParameters?: boolean;
}

export interface BaseAutoMatomoConfiguration<
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,28 @@ describe('MatomoInitializerService', () => {
expect(tracker.setDoNotTrack).toHaveBeenCalledBefore(tracker.trackPageView);
});

it('should disable campaign parameters if enabled', () => {
// Given
const service = instantiate({
mode: MatomoInitializationMode.MANUAL,
disableCampaignParameters: true,
trackAppInitialLoad: true,
enableLinkTracking: false,
});
const tracker = TestBed.inject(MatomoTracker);

spyOn(tracker, 'trackPageView');
spyOn(tracker, 'disableCampaignParameters');

// When
service.initialize();

// Then
expect(tracker.trackPageView).toHaveBeenCalledOnceWith();
expect(tracker.disableCampaignParameters).toHaveBeenCalledTimes(1);
expect(tracker.disableCampaignParameters).toHaveBeenCalledBefore(tracker.trackPageView);
});

it('should require tracking consent if setting if enabled', () => {
// Given
const service = instantiate({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,10 @@ export class MatomoInitializerService {
this.tracker.enableJSErrorTracking();
}

if (this.config.disableCampaignParameters) {
this.tracker.disableCampaignParameters();
}

if (this.config.trackAppInitialLoad) {
this.tracker.trackPageView();
}
Expand Down

0 comments on commit 88258fa

Please sign in to comment.