Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/better matomo #214

Merged
merged 2 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/few-donkeys-admire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@consent-manager/integration-matomo": minor
---

Enable JS error tracking in Matomo by default. Also adds a feature that lets devs enable any Matomo feature via Consent Manager"
32 changes: 31 additions & 1 deletion packages/integration-matomo/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ interface MatomoTrackerConfig extends IntegrationConfigOptions {
siteID?: string // @todo this should be required
enableLinkTracking?: boolean
enableHeartBeatTimer?: boolean
enableJSErrorTracking?: boolean
push?: string[][]
}

let wasInitialized = false
Expand All @@ -38,11 +40,19 @@ interface TrackedPageData {
interface TrackPageViewSPA {
location: Location
prevLocation?: Location
enableLinkTracking?: boolean
enableHeartBeatTimer?: boolean
enableJSErrorTracking?: boolean
push?: string[][]
}

const trackPageViewSPA = ({
location,
prevLocation,
enableLinkTracking = true,
enableHeartBeatTimer = true,
enableJSErrorTracking = true,
push = [],
}: TrackPageViewSPA): TrackedPageData | null => {
const paq = window._paq
if (!paq) {
Expand All @@ -58,9 +68,19 @@ const trackPageViewSPA = ({
paq.push(['setCustomUrl', url])
paq.push(['setDocumentTitle', title])
paq.push(['trackPageView'])
paq.push(['enableLinkTracking'])

enableLinkTracking && paq.push(['enableLinkTracking'])
enableHeartBeatTimer && paq.push(['enableHeartBeatTimer'])
enableJSErrorTracking && paq.push(['enableJSErrorTracking'])

paq.push(['trackAllContentImpressions'])

if (push.length) {
for (const pushValue of push) {
paq.push(pushValue)
}
}

return { url, title }
}

Expand All @@ -85,6 +105,8 @@ export const useMatomoTracker = ({
// @todo these might need to be in config, but outside of react due to route update hooks
enableLinkTracking = true,
enableHeartBeatTimer = true,
enableJSErrorTracking = true,
push = [],
}: MatomoTrackerConfig): Tracker => {
const [isEnabled] = useDecision('matomo')

Expand All @@ -93,9 +115,17 @@ export const useMatomoTracker = ({

enableLinkTracking && _paq.push(['enableLinkTracking'])
enableHeartBeatTimer && _paq.push(['enableHeartBeatTimer'])
enableJSErrorTracking && _paq.push(['enableJSErrorTracking'])

_paq.push(['setTrackerUrl', `${matomoURL}matomo.php`])
_paq.push(['setSiteId', siteID])

if (push.length) {
for (const pushValue of push) {
_paq.push(pushValue)
}
}

const script = document.createElement('script')

script.src = `${matomoURL}matomo.js`
Expand Down
Loading