Skip to content

Commit 73d45a0

Browse files
authored
Add custom matomo implementation enabling cookie settings (#47)
1 parent dc765b2 commit 73d45a0

File tree

5 files changed

+99
-23
lines changed

5 files changed

+99
-23
lines changed

docusaurus.config.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ const config = {
5454
// ... other options
5555
}
5656
],
57-
'docusaurus-plugin-matomo'
57+
'./src/plugins/docusaurus-plugin-matomo-analytics/index.js'
5858
],
5959

6060
themeConfig:
@@ -133,11 +133,13 @@ const config = {
133133
darkTheme: darkCodeTheme,
134134
additionalLanguages: ['powershell', 'ruby']
135135
},
136-
matomo: {
137-
matomoUrl: 'https://matomo.scs.community/',
136+
matomoAnalytics: {
137+
matomoUrl:
138+
'matomo.scs.community',
138139
siteId: '2',
139140
phpLoader: 'matomo.php',
140-
jsLoader: 'matomo.js'
141+
jsLoader: 'matomo.js',
142+
disableCookies: true
141143
}
142144
}),
143145

package-lock.json

-18
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
"@fullcalendar/timegrid": "^6.0.1",
2727
"@mdx-js/react": "^1.6.22",
2828
"clsx": "^1.2.1",
29-
"docusaurus-plugin-matomo": "^0.0.6",
3029
"prism-react-renderer": "^1.3.5",
3130
"react": "^17.0.2",
3231
"react-dom": "^17.0.2"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
const path = require('path')
2+
3+
// based on https://github.com/karser/docusaurus-plugin-matomo with extended parameters
4+
5+
function matomoAnalytics (context, options) {
6+
const { siteConfig } = context
7+
const { themeConfig } = siteConfig
8+
const { matomoAnalytics } = themeConfig || {}
9+
10+
if (!matomoAnalytics) {
11+
throw new Error(
12+
'Please specify \'matomo\' object in \'themeConfig\' with \'matomoUrl\' and \'siteId\' fields in it to use docusaurus-plugin-matomo'
13+
)
14+
}
15+
16+
const { matomoUrl, siteId } = matomoAnalytics
17+
18+
if (!matomoUrl) {
19+
throw new Error(
20+
'Please specify the `matomoUrl` field in the `themeConfig.matomo`'
21+
)
22+
}
23+
if (!siteId) {
24+
throw new Error(
25+
'Please specify the `siteId` field in the `themeConfig.matomo`'
26+
)
27+
}
28+
29+
const isProd = process.env.NODE_ENV === 'production'
30+
return {
31+
name: 'docusaurus-plugin-matomo-analytics',
32+
33+
getClientModules () {
34+
return isProd ? [path.resolve(__dirname, './track')] : []
35+
},
36+
37+
injectHtmlTags () {
38+
return {
39+
headTags: [
40+
{
41+
tagName: 'link',
42+
attributes: {
43+
rel: 'preconnect',
44+
href: `${matomoAnalytics.matomoUrl}`
45+
}
46+
},
47+
{
48+
tagName: 'script',
49+
innerHTML: `
50+
var _paq = window._paq = window._paq || [];
51+
_paq.push(["setDocumentTitle", document.domain + "/" + document.title]);
52+
${matomoAnalytics.cookieDomain ? `_paq.push(["setCookieDomain", "${matomoAnalytics.cookieDomain}"]);` : ''}
53+
${matomoAnalytics.domains ? `_paq.push(["setDomains", ["${matomoAnalytics.domains}"]])` : ''}
54+
${matomoAnalytics.campaignNameKey ? `_paq.push(["setCampaignNameKey", "${matomoAnalytics.campaignNameKey}"]);` : ''}
55+
${matomoAnalytics.campaignKeywordKey ? `_paq.push(["setCampaignKeywordKey", "${matomoAnalytics.campaignKeywordKey}"]);` : ''}
56+
${matomoAnalytics.doNotTrack ? `_paq.push(["setDoNotTrack", ${matomoAnalytics.doNotTrack}]);` : ''}
57+
${matomoAnalytics.disableCookies ? '_paq.push(["disableCookies"]);' : ''}
58+
_paq.push(['trackPageView']);
59+
_paq.push(['enableLinkTracking']);
60+
(function() {
61+
var u="${matomoAnalytics.matomoUrl}";
62+
_paq.push(['setTrackerUrl', u+'${matomoAnalytics.phpLoader}']);
63+
_paq.push(['setSiteId', '${matomoAnalytics.siteId}']);
64+
var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
65+
g.async=true; g.src=u+'${
66+
matomoAnalytics.jsLoader
67+
}'; s.parentNode.insertBefore(g,s);
68+
})();
69+
70+
`
71+
}
72+
]
73+
}
74+
}
75+
}
76+
}
77+
78+
module.exports = matomoAnalytics
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/* eslint-disable no-undef */
2+
import ExecutionEnvironment from '@docusaurus/ExecutionEnvironment'
3+
4+
export default (function () {
5+
if (!ExecutionEnvironment.canUseDOM) {
6+
return null
7+
}
8+
return {
9+
onRouteUpdate ({ location }) {
10+
_paq.push(['setCustomUrl', location.pathname])
11+
_paq.push(['setDocumentTitle', document.title])
12+
_paq.push(['trackPageView'])
13+
}
14+
}
15+
})()

0 commit comments

Comments
 (0)