-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #317 from BaseAdresseNationale/feat/update-tracking
Feat/update tracking
- Loading branch information
Showing
6 changed files
with
101 additions
and
67 deletions.
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
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
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 |
---|---|---|
@@ -1,51 +1,66 @@ | ||
/* eslint-disable camelcase */ | ||
|
||
import os from 'node:os' | ||
import MatomoTracker from 'matomo-tracker' | ||
|
||
const {MATOMO_URL, MATOMO_SITE_ID, NODE_ENV} = process.env | ||
|
||
let MATOMO_ERROR = false | ||
// CF. Tracking HTTP API documentation : | ||
// https://developer.matomo.org/api-reference/tracking-api | ||
|
||
let matomo | ||
import os from 'node:os' | ||
import fetch from '../util/fetch.cjs' | ||
|
||
const isDevMode = () => NODE_ENV !== 'production' | ||
const { | ||
MATOMO_URL, | ||
MATOMO_SITE_ID, | ||
MATOMO_TOKEN_AUTH, | ||
NODE_ENV | ||
} = process.env | ||
const isDevMode = NODE_ENV !== 'production' | ||
const matomoUrl = `${MATOMO_URL}/matomo.php` | ||
|
||
const logMatomoError = err => { | ||
MATOMO_ERROR = true | ||
console.warn('[WARNING] tracking request:', err) | ||
console.log('error tracking request:', err) | ||
} | ||
|
||
const matomoUrl = `${MATOMO_URL}/matomo.php` | ||
const encodeParams = params => Object.fromEntries( | ||
Object | ||
.entries(params || {}) | ||
.map(([key, value]) => [key, typeof value === 'string' ? encodeURIComponent(value) : value]), | ||
) | ||
|
||
try { | ||
const matomo = new MatomoTracker(MATOMO_SITE_ID, matomoUrl) | ||
matomo.on('error', logMatomoError) | ||
} catch (error) { | ||
logMatomoError(error) | ||
} | ||
export const getTrackEvent = ({category, action, name, value = 1}) => ({ | ||
e_c: encodeURIComponent(`${isDevMode ? 'DEVMODE - ' : ''}${category}`), // Category name | ||
e_a: encodeURIComponent(action), // Action name | ||
e_n: encodeURIComponent(name), // Name | ||
e_v: value, // Value | ||
}) | ||
|
||
export const getTrackEvent = ({category, action, name, value}) => { | ||
const DEVMODE = isDevMode() | ||
export const sendToTracker = async (params = {}) => { | ||
const {url, ua, download, trackEvent, ...otherParams} = params | ||
const safeOtherParams = encodeParams(otherParams) | ||
const requiredParams = { | ||
idsite: MATOMO_SITE_ID, | ||
rec: 1, | ||
ua: ua || encodeURIComponent(`${os.hostname()} / Node.JS ${process.version} / ${os.version()}`), | ||
...(MATOMO_TOKEN_AUTH ? {token_auth: MATOMO_TOKEN_AUTH} : {}), | ||
} | ||
const urlSearchParams = new URLSearchParams({ | ||
...requiredParams, | ||
...safeOtherParams, | ||
...(url ? {url: encodeURIComponent(url)} : {}), | ||
...(download ? {download: encodeURIComponent(download)} : {}), | ||
...(trackEvent ? getTrackEvent(trackEvent) : {}), | ||
}) | ||
|
||
// CF. Tracking HTTP API documentation : | ||
// https://developer.matomo.org/api-reference/tracking-api | ||
try { | ||
const sentToMatomoWithHTTP = await fetch(matomoUrl, { | ||
method: 'POST', | ||
body: urlSearchParams, | ||
}) | ||
|
||
return { | ||
e_c: `${DEVMODE ? 'DEVMODE - ' : ''}${category}`, // Category name | ||
e_a: action, // Action name | ||
e_n: name, // Name | ||
...(value ? {e_v: value} : {}), // Value | ||
if (sentToMatomoWithHTTP.status !== 200) { | ||
throw new Error(`Matomo HTTP API returned ${sentToMatomoWithHTTP.status}`) | ||
} | ||
} catch (error) { | ||
logMatomoError(error) | ||
} | ||
} | ||
|
||
export const sendToTracker = ({url, download, trackEvent}) => | ||
!MATOMO_ERROR | ||
&& matomo?.track({ | ||
ua: `${os.hostname()} / Node.JS ${process.version} / ${os.version()}`, | ||
...(url ? {url} : {}), | ||
...(download ? {download} : {}), | ||
...(trackEvent ? getTrackEvent(trackEvent) : {}) | ||
}) | ||
|
||
export default sendToTracker |
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 |
---|---|---|
|
@@ -6303,11 +6303,6 @@ map-obj@^4.1.0: | |
resolved "https://registry.yarnpkg.com/map-obj/-/map-obj-4.3.0.tgz#9304f906e93faae70880da102a9f1df0ea8bb05a" | ||
integrity sha512-hdN1wVrZbb29eBGiGjJbeP8JbKjq1urkHJ/LIP/NY48MZ1QVXUsQBV1G1zvYFHn1XE06cwjBsOI2K3Ulnj1YXQ== | ||
|
||
matomo-tracker@^2.2.4: | ||
version "2.2.4" | ||
resolved "https://registry.yarnpkg.com/matomo-tracker/-/matomo-tracker-2.2.4.tgz#ee397d915d7b2e7964996ca28a0a03f4f0692453" | ||
integrity sha512-7fDy4wRhDQ1dnSxVqmnVqmmos9ACKag0fCBtBD3/Qeoqks7MFqXcO35nfS6S8xU/IXNf7534q/4Gx8fuWKYW6A== | ||
|
||
[email protected]: | ||
version "0.3.0" | ||
resolved "https://registry.yarnpkg.com/media-typer/-/media-typer-0.3.0.tgz#8710d7af0aa626f8fffa1ce00168545263255748" | ||
|