mirror of
https://github.com/kremalicious/gatsby-plugin-matomo.git
synced 2025-01-03 02:15:01 +01:00
add additionalTrackers to set multiple trackers (#403)
See https://developer.matomo.org/guides/tracking-javascript-guide#multiple-piwik-trackers
This commit is contained in:
parent
197a17ec1e
commit
7d921386fa
10
README.md
10
README.md
@ -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`. |
|
| `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. |
|
| `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`. |
|
| `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
|
```js
|
||||||
plugins: [
|
plugins: [
|
||||||
{
|
{
|
||||||
@ -100,7 +100,13 @@ plugins: [
|
|||||||
cookieDomain: '*.example.org',
|
cookieDomain: '*.example.org',
|
||||||
localScript: '/piwik.js',
|
localScript: '/piwik.js',
|
||||||
dev: false,
|
dev: false,
|
||||||
enableJSErrorTracking: true
|
enableJSErrorTracking: true,
|
||||||
|
additionalTrackers: [
|
||||||
|
{
|
||||||
|
siteId: 'ADDITIONAL_SITE_ID',
|
||||||
|
trackerUrl: 'https://ADDITIONAL_MATOMO_URL.COM/matomo.php'
|
||||||
|
}
|
||||||
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
@ -4,7 +4,7 @@
|
|||||||
|
|
||||||
import { onRenderBody } from '../gatsby-ssr'
|
import { onRenderBody } from '../gatsby-ssr'
|
||||||
|
|
||||||
describe('gatsby-plugin-google-analytics', () => {
|
describe('gatsby-plugin-matomo', () => {
|
||||||
describe('gatsby-ssr', () => {
|
describe('gatsby-ssr', () => {
|
||||||
describe('onRenderBody', () => {
|
describe('onRenderBody', () => {
|
||||||
describe('in non-production env', () => {
|
describe('in non-production env', () => {
|
||||||
@ -120,6 +120,20 @@ describe('gatsby-plugin-google-analytics', () => {
|
|||||||
const result = JSON.stringify(setPostBodyComponents.mock.calls[0][0])
|
const result = JSON.stringify(setPostBodyComponents.mock.calls[0][0])
|
||||||
expect(result).not.toMatch(/navigator.doNotTrack/)
|
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/)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -14,7 +14,8 @@ function buildTrackingCode(pluginOptions) {
|
|||||||
disableCookies,
|
disableCookies,
|
||||||
cookieDomain,
|
cookieDomain,
|
||||||
enableJSErrorTracking,
|
enableJSErrorTracking,
|
||||||
respectDnt = true
|
respectDnt = true,
|
||||||
|
additionalTrackers = []
|
||||||
} = pluginOptions
|
} = pluginOptions
|
||||||
|
|
||||||
const script = localScript ? localScript : `${matomoUrl}/${matomoJsScript}`
|
const script = localScript ? localScript : `${matomoUrl}/${matomoJsScript}`
|
||||||
@ -47,6 +48,13 @@ function buildTrackingCode(pluginOptions) {
|
|||||||
window._paq.push(['setTrackerUrl', '${matomoUrl}/${matomoPhpScript}']);
|
window._paq.push(['setTrackerUrl', '${matomoUrl}/${matomoPhpScript}']);
|
||||||
window._paq.push(['setSiteId', '${siteId}']);
|
window._paq.push(['setSiteId', '${siteId}']);
|
||||||
window._paq.push(['enableHeartBeatTimer']);
|
window._paq.push(['enableHeartBeatTimer']);
|
||||||
|
${additionalTrackers
|
||||||
|
.map(
|
||||||
|
(t) =>
|
||||||
|
`window._paq.push(['addTracker', '${t.trackerUrl}', '${t.siteId}']);`
|
||||||
|
)
|
||||||
|
.join('\n')}
|
||||||
|
|
||||||
window.start = new Date();
|
window.start = new Date();
|
||||||
|
|
||||||
(function() {
|
(function() {
|
||||||
|
Loading…
Reference in New Issue
Block a user