-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9e61f16
commit cec057c
Showing
4 changed files
with
89 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
const path = require('path'); | ||
|
||
module.exports = function (context) { | ||
const {siteConfig} = context; | ||
const {themeConfig} = siteConfig; | ||
const {matomo} = themeConfig || {}; | ||
|
||
if (!matomo) { | ||
throw new Error(`Please specify 'matomo' object in 'themeConfig' with 'matomoUrl' and 'siteId' fields in it to use docusaurus-plugin-matomo`); | ||
} | ||
|
||
const {matomoUrl, siteId} = matomo; | ||
|
||
if (!matomoUrl) { | ||
throw new Error('Please specify the `matomoUrl` field in the `themeConfig.matomo`'); | ||
} | ||
if (!siteId) { | ||
throw new Error('Please specify the `siteId` field in the `themeConfig.matomo`'); | ||
} | ||
const phpLoader = matomo.phpLoader || 'matomo.php'; | ||
const jsLoader = matomo.jsLoader || 'matomo.js'; | ||
|
||
const isProd = process.env.NODE_ENV === 'production'; | ||
|
||
return { | ||
name: 'docusaurus-plugin-matomo', | ||
getClientModules() { | ||
|
||
return isProd ? [path.resolve(__dirname, './track')] : []; | ||
}, | ||
|
||
injectHtmlTags() { | ||
if (!isProd) { | ||
return {}; | ||
} | ||
return { | ||
headTags: [ | ||
{ | ||
tagName: 'link', | ||
attributes: { | ||
rel: 'preconnect', | ||
href: `${matomoUrl}`, | ||
}, | ||
}, | ||
{ | ||
tagName: 'script', | ||
innerHTML: ` | ||
var _paq = window._paq = window._paq || []; | ||
_paq.push(['setRequestMethod', 'POST']); | ||
_paq.push(['trackPageView']); | ||
_paq.push(['enableLinkTracking']); | ||
_paq.push(['enableHeartBeatTimer']); | ||
(function() { | ||
var u="${matomoUrl}"; | ||
_paq.push(['setRequestMethod', 'POST']); | ||
_paq.push(['setTrackerUrl', u+'${phpLoader}']); | ||
_paq.push(['setSiteId', '${siteId}']); | ||
var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0]; | ||
g.type='text/javascript'; g.async=true; g.src=u+'${jsLoader}'; s.parentNode.insertBefore(g,s); | ||
})(); | ||
`, | ||
}, | ||
], | ||
}; | ||
}, | ||
}; | ||
}; |
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,14 @@ | ||
import ExecutionEnvironment from '@docusaurus/ExecutionEnvironment'; | ||
|
||
export default (function () { | ||
if (!ExecutionEnvironment.canUseDOM) { | ||
return null; | ||
} | ||
return { | ||
onRouteUpdate({location}) { | ||
_paq.push(['setCustomUrl', location.pathname]); | ||
_paq.push(['setDocumentTitle', document.title]); | ||
_paq.push(['trackPageView']); | ||
}, | ||
}; | ||
})(); |
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