add additionalTrackers to set multiple trackers (#403)

See https://developer.matomo.org/guides/tracking-javascript-guide#multiple-piwik-trackers
This commit is contained in:
Fabio Bonelli 2023-03-23 10:39:43 +01:00 committed by GitHub
parent 197a17ec1e
commit 7d921386fa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 4 deletions

View File

@ -81,7 +81,7 @@ _NOTE: By default, this plugin only generates output when run in production mode
| `respectDnt` | (optional) If false, will load all scripts without respecting user preference to `Do Not Track` on browsers. Defaults to `true`. |
| `dev` | (optional) Activate dev mode by setting to `true`. Will load all scripts despite not running in `production` environment. Ignores your local browser's DNT header too. Outputs some information in console about what it is doing. Useful for local testing but careful: all hits will be send like in production. |
| `enableJSErrorTracking` | (optional) Enable basic JavaScript error tracking and reporting in Matomo by setting to `true`. |
| `additionalTrackers` | (optional) An array of additional trackers to track on different Matomo servers. Additional trackers are objects with the keys `siteId` and `trackerUrl` containing the full URL to the Matomo PHP script. Defaults to `[]`. |
```js
plugins: [
{
@ -100,7 +100,13 @@ plugins: [
cookieDomain: '*.example.org',
localScript: '/piwik.js',
dev: false,
enableJSErrorTracking: true
enableJSErrorTracking: true,
additionalTrackers: [
{
siteId: 'ADDITIONAL_SITE_ID',
trackerUrl: 'https://ADDITIONAL_MATOMO_URL.COM/matomo.php'
}
]
}
}
]

View File

@ -4,7 +4,7 @@
import { onRenderBody } from '../gatsby-ssr'
describe('gatsby-plugin-google-analytics', () => {
describe('gatsby-plugin-matomo', () => {
describe('gatsby-ssr', () => {
describe('onRenderBody', () => {
describe('in non-production env', () => {
@ -120,6 +120,20 @@ describe('gatsby-plugin-google-analytics', () => {
const result = JSON.stringify(setPostBodyComponents.mock.calls[0][0])
expect(result).not.toMatch(/navigator.doNotTrack/)
})
it('sets additionalTrackers', () => {
const { setPostBodyComponents } = setup({
additionalTrackers: [
{
siteId: 'TEST_ADDITIONAL_SITE_ID',
trackerUrl: 'TEST_ADDITIONAL_TRACKER_URL'
}
]
})
const result = JSON.stringify(setPostBodyComponents.mock.calls[0][0])
expect(result).toMatch(/TEST_ADDITIONAL_SITE_ID/)
expect(result).toMatch(/TEST_ADDITIONAL_TRACKER_URL/)
})
})
})
})

View File

@ -14,7 +14,8 @@ function buildTrackingCode(pluginOptions) {
disableCookies,
cookieDomain,
enableJSErrorTracking,
respectDnt = true
respectDnt = true,
additionalTrackers = []
} = pluginOptions
const script = localScript ? localScript : `${matomoUrl}/${matomoJsScript}`
@ -47,6 +48,13 @@ function buildTrackingCode(pluginOptions) {
window._paq.push(['setTrackerUrl', '${matomoUrl}/${matomoPhpScript}']);
window._paq.push(['setSiteId', '${siteId}']);
window._paq.push(['enableHeartBeatTimer']);
${additionalTrackers
.map(
(t) =>
`window._paq.push(['addTracker', '${t.trackerUrl}', '${t.siteId}']);`
)
.join('\n')}
window.start = new Date();
(function() {