Skip to content

Commit

Permalink
Add useMatomo to prevent matomo stats in dev
Browse files Browse the repository at this point in the history
  • Loading branch information
louptheron committed Oct 18, 2024
1 parent ea0324b commit f477339
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 16 deletions.
6 changes: 6 additions & 0 deletions frontend/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ FRONTEND_MONITORFISH_VERSION=
FRONTEND_SENTRY_TRACING_ORIGINS=
FRONTEND_SENTRY_ENV=

################################################################################
# UseMatomo

FRONTEND_MATOMO_URL=
FRONTEND_MATOMO_ID=

################################################################################
# Shom

Expand Down
6 changes: 6 additions & 0 deletions frontend/.env.local.defaults
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ FRONTEND_MONITORFISH_VERSION=
FRONTEND_SENTRY_TRACING_ORIGINS=
FRONTEND_SENTRY_ENV=

################################################################################
# UseMatomo

FRONTEND_MATOMO_URL=
FRONTEND_MATOMO_ID=

################################################################################
# Shom

Expand Down
15 changes: 0 additions & 15 deletions frontend/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,6 @@
<meta name="theme-color" content="#000000" />
<meta name="description" content="MonitorFish" />
<title>MonitorFish</title>
<!-- Matomo -->
<script>
var _paq = window._paq = window._paq || [];
/* tracker methods like "setCustomDimension" should be called before "trackPageView" */
_paq.push(['trackPageView']);
_paq.push(['enableLinkTracking']);
(function() {
var u="https://stats.beta.gouv.fr/";
_paq.push(['setTrackerUrl', u+'matomo.php']);
_paq.push(['setSiteId', '146']);
var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
g.async=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s);
})();
</script>
<!-- End Matomo Code -->
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
Expand Down
3 changes: 3 additions & 0 deletions frontend/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { CustomGlobalStyle } from '@components/CustomGlobalStyle'
import { FrontendErrorBoundary } from '@components/FrontendErrorBoundary'
import { useMatomo } from '@hooks/useMatomo'
import { GlobalStyle, THEME, ThemeProvider } from '@mtes-mct/monitor-ui'
import { UnsupportedBrowserPage } from '@pages/UnsupportedBrowserPage'
import { isBrowserSupported } from '@utils/isBrowserSupported'
Expand All @@ -14,6 +15,8 @@ import { router } from './router'
countries.registerLocale(COUNTRIES_FR)

export function App() {
useMatomo()

if (!isBrowserSupported()) {
return <UnsupportedBrowserPage />
}
Expand Down
2 changes: 2 additions & 0 deletions frontend/src/env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ interface ImportMetaEnv {
readonly FRONTEND_GEOSERVER_LOCAL_URL: string
readonly FRONTEND_GEOSERVER_REMOTE_URL: string
readonly FRONTEND_MAPBOX_KEY: string
readonly FRONTEND_MATOMO_ID: string
readonly FRONTEND_MATOMO_URL: string
readonly FRONTEND_MISSION_FORM_AUTO_SAVE_ENABLED: string
readonly FRONTEND_MISSION_FORM_AUTO_UPDATE_ENABLED: string
readonly FRONTEND_MONITORENV_URL: string
Expand Down
43 changes: 43 additions & 0 deletions frontend/src/hooks/useMatomo.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { useEffect } from 'react'

/* eslint-disable no-underscore-dangle */
export function useMatomo() {
const matomoUrl = import.meta.env.FRONTEND_MATOMO_URL
const matomoSiteId = import.meta.env.FRONTEND_MATOMO_ID
const isMatomoActivated = matomoUrl && matomoSiteId

useEffect(() => {
if (!isMatomoActivated) {
return
}

const normalizedMatomoUrl = matomoUrl[matomoUrl.length - 1] !== '/' ? `${matomoUrl}/` : matomoUrl

if (typeof window === 'undefined') {
return
}

window._paq = window._paq || []

window._paq.push(['trackPageView'])
window._paq.push(['enableLinkTracking'])
window._paq.push(['setTrackerUrl', `${normalizedMatomoUrl}matomo.php`])
window._paq.push(['setSiteId', matomoSiteId])

const doc = document
const scriptElement = doc.createElement('script')
const scripts = doc.getElementsByTagName('script')[0]

scriptElement.type = 'text/javascript'
scriptElement.async = true
scriptElement.defer = true
scriptElement.src = `${normalizedMatomoUrl}matomo.js`

if (scripts && scripts.parentNode) {
scripts.parentNode.insertBefore(scriptElement, scripts)
}
}, [matomoSiteId, matomoUrl, isMatomoActivated])

return null
}
/* eslint-enable no-underscore-dangle */
2 changes: 1 addition & 1 deletion frontend/src/hooks/useTracking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type Tracking = {
}

/**
* Wrapper of Matomo script injected in `index.html`.
* Wrapper of UseMatomo script injected in `index.html`.
*
*
* @see https://developer.matomo.org/guides/tracking-javascript-guide
Expand Down
2 changes: 2 additions & 0 deletions infra/docker/docker-compose.cypress.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ services:
- FRONTEND_MISSION_FORM_AUTO_SAVE_ENABLED=true # Even if we inject this env var, the value is not used (see cypress.config.ts)
- FRONTEND_MISSION_FORM_AUTO_UPDATE_ENABLED=true
- FRONTEND_PRIOR_NOTIFICATION_LIST_ENABLED=true
- FRONTEND_MATOMO_URL=
- FRONTEND_MATOMO_ID=
ports:
- 8880:8880
- 8000:8000
Expand Down
2 changes: 2 additions & 0 deletions infra/docker/docker-compose.puppeteer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ services:
- FRONTEND_MISSION_FORM_AUTO_SAVE_ENABLED=true
- FRONTEND_MISSION_FORM_AUTO_UPDATE_ENABLED=true
- FRONTEND_PRIOR_NOTIFICATION_LIST_ENABLED=true
- FRONTEND_MATOMO_URL=
- FRONTEND_MATOMO_ID=
ports:
- 8880:8880
- 8000:8000
Expand Down
2 changes: 2 additions & 0 deletions infra/remote/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ services:
- FRONTEND_MISSION_FORM_AUTO_SAVE_ENABLED=$MONITORFISH_MISSION_FORM_AUTO_SAVE_ENABLED
- FRONTEND_MISSION_FORM_AUTO_UPDATE_ENABLED=$MONITORFISH_MISSION_FORM_AUTO_UPDATE_ENABLED
- FRONTEND_PRIOR_NOTIFICATION_LIST_ENABLED=$MONITORFISH_PRIOR_NOTIFICATION_LIST_ENABLED
- FRONTEND_MATOMO_URL=$MONITORFISH_MATOMO_URL
- FRONTEND_MATOMO_ID=$MONITORFISH_MATOMO_ID
ports:
- 8880:8880
- 8000:8000
Expand Down

0 comments on commit f477339

Please sign in to comment.