Skip to content
This repository has been archived by the owner on Dec 20, 2024. It is now read-only.

Commit

Permalink
add additionalTrackers to set multiple trackers
Browse files Browse the repository at this point in the history
  • Loading branch information
bfabio committed Mar 13, 2023
1 parent 197a17e commit de4c999
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
{
Expand All @@ -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'
}
]
}
}
]
Expand Down
16 changes: 15 additions & 1 deletion src/__tests__/gatsby-ssr.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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/)
})
})
})
})
Expand Down
7 changes: 6 additions & 1 deletion src/gatsby-ssr.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ function buildTrackingCode(pluginOptions) {
disableCookies,
cookieDomain,
enableJSErrorTracking,
respectDnt = true
respectDnt = true,
additionalTrackers = []
} = pluginOptions

const script = localScript ? localScript : `${matomoUrl}/${matomoJsScript}`
Expand Down Expand Up @@ -47,6 +48,10 @@ 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}])`
)}
window.start = new Date();
(function() {
Expand Down

0 comments on commit de4c999

Please sign in to comment.