-
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.
Add useMatomo to prevent matomo stats in dev
- Loading branch information
1 parent
ea0324b
commit f477339
Showing
10 changed files
with
67 additions
and
16 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
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,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 */ |
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