diff --git a/README.md b/README.md index d7faae3..b2f68a8 100644 --- a/README.md +++ b/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`. | | `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' + } + ] } } ] diff --git a/src/__tests__/gatsby-ssr.js b/src/__tests__/gatsby-ssr.js index 26c0f4a..0d185f2 100644 --- a/src/__tests__/gatsby-ssr.js +++ b/src/__tests__/gatsby-ssr.js @@ -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/) + }) }) }) }) diff --git a/src/gatsby-ssr.js b/src/gatsby-ssr.js index ff4ea58..7fe0011 100644 --- a/src/gatsby-ssr.js +++ b/src/gatsby-ssr.js @@ -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,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() {