From 8769009d74f9ffb46ffd46057e27cd99c1f072f3 Mon Sep 17 00:00:00 2001 From: Xavier Desoindre Date: Thu, 28 Nov 2024 10:09:04 +0100 Subject: [PATCH] chore: proper event tracking --- detection/start.ts | 4 ++-- src/scripts/getStats.ts | 10 +++++++--- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/detection/start.ts b/detection/start.ts index 4060b3f34..f6ae49ae0 100644 --- a/detection/start.ts +++ b/detection/start.ts @@ -83,10 +83,10 @@ export const initMatomo = () => { window.please.track = function (ary) { //@ts-expect-error: Matomo redefinition const matomo = window.Matomo - if (matomo) { + if (matomo && ary[0] === 'trackEvent') { //@ts-expect-error: injected MATOMO_SITE_URL, MATOMO_SITE_ID constant from env var, see webpack.config.js const matomoTracker = matomo.getTracker(MATOMO_SITE_URL + '/matomo.php', MATOMO_SITE_ID) - matomoTracker.trackEvent(ary[0], ary[1], ary[2], ary[3]) + matomoTracker.trackEvent(ary[1], ary[2], ary[3]) } } } diff --git a/src/scripts/getStats.ts b/src/scripts/getStats.ts index 1fdede033..8d5ab0691 100644 --- a/src/scripts/getStats.ts +++ b/src/scripts/getStats.ts @@ -32,7 +32,7 @@ export const getMatomoStats = async (date: string) => { const [allVisits, allEventsByCategory, allEventsByAction, lastWeekEventsByCategory] = await Promise.all([ await axios .post< - { label: string; nb_visits: number }[] + { label: string; nb_visits: number; url: string }[] >(`${process.env.NEXT_PUBLIC_MATOMO_SITE_URL}?idSite=${process.env.NEXT_PUBLIC_MATOMO_SITE_ID}&method=Actions.getPageUrls&format=JSON&module=API&period=week&date=${date}&showColumns=nb_visits&filter_limit=-1&flat=1`) .then((response) => response.data), await axios @@ -52,11 +52,15 @@ export const getMatomoStats = async (date: string) => { .then((response) => response.data), ]) + const impactco2Visits = allVisits.filter( + (page) => + page.url && (page.url.startsWith('https://impactco2.fr/') || page.url.startsWith('https://www.impactco2.fr/')) + ) const iframes = allEventsByCategory.filter((event) => event.label.startsWith('IFrame_')) const lastWeekIframes = lastWeekEventsByCategory.filter((event) => event.label.startsWith('IFrame_')) const internalVisits: Record = {} - allVisits.forEach((page) => { + impactco2Visits.forEach((page) => { const segments = page.label.split('?') const key = (segments[0].startsWith('/') ? segments[0].slice(1) : segments[0]).replace('outils/', '') if (internalPages.includes(key)) { @@ -82,7 +86,7 @@ export const getMatomoStats = async (date: string) => { }) const results = { - visits: allVisits.reduce((acc, visit) => acc + visit.nb_visits, 0), + visits: impactco2Visits.reduce((acc, visit) => acc + visit.nb_visits, 0), iframes: iframes.reduce((acc, visit) => acc + visit.nb_visits, 0), api: allEventsByCategory .filter(