-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Beatrice Guerra <[email protected]> Co-authored-by: Rodley Orosa <[email protected]> Refs: SHELL-198 (#388)
- Loading branch information
1 parent
9fc0b13
commit 8d0594b
Showing
7 changed files
with
200 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2024 Zextras <https://www.zextras.com> | ||
* | ||
* SPDX-License-Identifier: AGPL-3.0-only | ||
*/ | ||
import { MatomoTracker } from '../matomo'; | ||
|
||
export function initMatomo(): void { | ||
// do nothing | ||
} | ||
|
||
export function getMatomoTracker(): MatomoTracker { | ||
return { | ||
trackEvent: () => undefined, | ||
trackPageView: () => undefined, | ||
setCustomUrl: () => undefined | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2024 Zextras <https://www.zextras.com> | ||
* | ||
* SPDX-License-Identifier: AGPL-3.0-only | ||
*/ | ||
export interface MatomoTracker { | ||
trackPageView(customTitle?: string): void; | ||
trackEvent(category: string, action: string, name?: string, value?: number): void; | ||
setCustomUrl(url: string): void; | ||
} | ||
|
||
declare global { | ||
const Matomo: { | ||
getTracker(trackerUrl: string, siteId: number): MatomoTracker; | ||
}; | ||
} | ||
export function initMatomo(url: string): void { | ||
const matomoScriptId = 'matomo-script'; | ||
|
||
if (document.getElementById(matomoScriptId)) { | ||
return; | ||
} | ||
|
||
const matomoScript = document.createElement('script'); | ||
const scriptElement = document.getElementsByTagName('script')[0]; | ||
matomoScript.type = 'text/javascript'; | ||
matomoScript.async = true; | ||
matomoScript.src = `${url}matomo.js`; | ||
matomoScript.id = matomoScriptId; | ||
scriptElement.parentNode?.insertBefore(matomoScript, scriptElement); | ||
} | ||
|
||
export function getMatomoTracker(url: string, siteId: number): MatomoTracker { | ||
return Matomo.getTracker(`${url}matomo.php`, siteId); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2024 Zextras <https://www.zextras.com> | ||
* | ||
* SPDX-License-Identifier: AGPL-3.0-only | ||
*/ | ||
import * as matomo from './matomo'; | ||
import { Tracker } from './tracker'; | ||
|
||
beforeEach(() => { | ||
Tracker.enableTracker(false); | ||
}); | ||
describe('Tracker', () => { | ||
it('should not call trackPageView when Tracker.isEnabled is false(default)', () => { | ||
const trackPageView = jest.fn(); | ||
jest.spyOn(matomo, 'getMatomoTracker').mockReturnValue({ | ||
trackEvent: () => undefined, | ||
trackPageView, | ||
setCustomUrl: () => undefined | ||
}); | ||
const tracker = new Tracker(1); | ||
tracker.trackPageView('title'); | ||
expect(trackPageView).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('should call trackPageView when Tracker.isEnabled is true', () => { | ||
const trackPageView = jest.fn(); | ||
const setCustomUrl = jest.fn(); | ||
jest.spyOn(matomo, 'getMatomoTracker').mockReturnValue({ | ||
trackEvent: () => undefined, | ||
trackPageView, | ||
setCustomUrl | ||
}); | ||
Tracker.enableTracker(true); | ||
const tracker = new Tracker(1); | ||
tracker.trackPageView('title'); | ||
expect(trackPageView).toHaveBeenCalled(); | ||
expect(setCustomUrl).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should not call trackEvent when Tracker.isEnabled is false(default)', () => { | ||
const trackEvent = jest.fn(); | ||
jest.spyOn(matomo, 'getMatomoTracker').mockReturnValue({ | ||
trackEvent, | ||
trackPageView: () => undefined, | ||
setCustomUrl: () => undefined | ||
}); | ||
const tracker = new Tracker(1); | ||
tracker.trackEvent('category', 'action'); | ||
expect(trackEvent).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('should call trackEvent when Tracker.isEnabled is true', () => { | ||
const trackEvent = jest.fn(); | ||
const setCustomUrl = jest.fn(); | ||
jest.spyOn(matomo, 'getMatomoTracker').mockReturnValue({ | ||
trackEvent, | ||
trackPageView: () => undefined, | ||
setCustomUrl | ||
}); | ||
Tracker.enableTracker(true); | ||
const tracker = new Tracker(1); | ||
tracker.trackEvent('category', 'action'); | ||
expect(trackEvent).toHaveBeenCalled(); | ||
expect(setCustomUrl).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should call init only when tracker is enabled', () => { | ||
const initMatomoSpy = jest.spyOn(matomo, 'initMatomo'); | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars,unused-imports/no-unused-vars | ||
const tracker = new Tracker(1); | ||
expect(initMatomoSpy).not.toHaveBeenCalled(); | ||
Tracker.enableTracker(true); | ||
expect(initMatomoSpy).toHaveBeenCalled(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2024 Zextras <https://www.zextras.com> | ||
* | ||
* SPDX-License-Identifier: AGPL-3.0-only | ||
*/ | ||
|
||
import { getMatomoTracker, initMatomo, MatomoTracker } from './matomo'; | ||
|
||
export class Tracker { | ||
private static readonly URL = 'https://analytics.zextras.tools/'; | ||
|
||
private matomoTracker: MatomoTracker | null; | ||
|
||
private static isEnabled = false; | ||
|
||
private readonly siteId: number; | ||
|
||
constructor(siteId: number) { | ||
this.siteId = siteId; | ||
if (Tracker.isEnabled) { | ||
this.matomoTracker = getMatomoTracker(Tracker.URL, siteId); | ||
} else { | ||
this.matomoTracker = null; | ||
} | ||
} | ||
|
||
public static enableTracker(isEnabled: boolean): void { | ||
if (isEnabled) { | ||
initMatomo(Tracker.URL); | ||
} | ||
Tracker.isEnabled = isEnabled; | ||
} | ||
|
||
public trackPageView(customTitle?: string): void { | ||
const tracker = this.getMatomoTrackerInstance(); | ||
tracker?.setCustomUrl(window.location.href); | ||
tracker?.trackPageView(customTitle); | ||
} | ||
|
||
public trackEvent(category: string, action: string, name?: string, value?: number): void { | ||
const tracker = this.getMatomoTrackerInstance(); | ||
tracker?.setCustomUrl(window.location.href); | ||
tracker?.trackEvent(category, action, name, value); | ||
} | ||
|
||
private getMatomoTrackerInstance(): MatomoTracker | null { | ||
if (!Tracker.isEnabled) { | ||
return null; | ||
} | ||
if (this.matomoTracker === null) { | ||
try { | ||
this.matomoTracker = getMatomoTracker(Tracker.URL, this.siteId); | ||
} catch (e) { | ||
console.warn('Matomo is not initialized yet: ', e); | ||
} | ||
} | ||
return this.matomoTracker; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters