Add support for error tracking [Fixes #168] (#169)

* Add support for error tracking [Fixes #168]

* fix missing cookieDomain

Co-authored-by: Matthias Kretschmann <m@kretschmann.io>
This commit is contained in:
Sam Richards 2021-05-23 02:37:14 -07:00 committed by GitHub
parent 4858debe66
commit ae2e8e7a61
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 17 deletions

View File

@ -65,22 +65,23 @@ _NOTE: By default, this plugin only generates output when run in production mode
### Options
| Option | Explanation |
| ----------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `siteId` | Your Matomo site ID configured in your Matomo installation. |
| `matomoUrl` | The url of your Matomo installation. |
| `siteUrl` | The url of your site, usually the same as `siteMetadata.siteUrl`. Only used for generating the url for `noscript` image tracking fallback. |
| `matomoPhpScript` | (optional) The name of your Matomo PHP script. Defaults to `piwik.php` |
| `matomoJsScript` | (optional) The name of your Matomo JS script. Defaults to `piwik.js` |
| `exclude` | (optional) Specify an array of pathnames where tracking code will be excluded. The pathname `/offline-plugin-app-shell-fallback/` is excluded by default. |
| `requireConsent` | (optional) If true, tracking will be disabled until you call `window._paq.push(['setConsentGiven']);`. |
| `requireCookieConsent` | (optional) If true, no cookies will be stored or used until you call `window._paq.push(['setCookieConsentGiven']);`. |
| `disableCookies` | (optional) If true, no cookie will be used by Matomo. |
| `cookieDomain` | (optional) Specify cookie domain. |
| `localScript` | (optional) If set, load local `piwik.js` script from the given path, instead of loading it from your `matomoUrl`. |
| `trackLoad` | (optional) If true, it will track the loading of the matomo library. 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. |
| Option | Explanation |
| ----------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `siteId` | Your Matomo site ID configured in your Matomo installation. |
| `matomoUrl` | The url of your Matomo installation. |
| `siteUrl` | The url of your site, usually the same as `siteMetadata.siteUrl`. Only used for generating the url for `noscript` image tracking fallback. |
| `matomoPhpScript` | (optional) The name of your Matomo PHP script. Defaults to `piwik.php` |
| `matomoJsScript` | (optional) The name of your Matomo JS script. Defaults to `piwik.js` |
| `exclude` | (optional) Specify an array of pathnames where tracking code will be excluded. The pathname `/offline-plugin-app-shell-fallback/` is excluded by default. |
| `requireConsent` | (optional) If true, tracking will be disabled until you call `window._paq.push(['setConsentGiven']);`. |
| `requireCookieConsent` | (optional) If true, no cookies will be stored or used until you call `window._paq.push(['setCookieConsentGiven']);`. |
| `disableCookies` | (optional) If true, no cookie will be used by Matomo. |
| `cookieDomain` | (optional) Specify cookie domain. |
| `localScript` | (optional) If set, load local `piwik.js` script from the given path, instead of loading it from your `matomoUrl`. |
| `trackLoad` | (optional) If true, it will track the loading of the matomo library. 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. |
| `enableJSErrorTracking` | (optional) Enable basic JavaScript error tracking and reporting in Matomo by setting to `true`. |
```js
plugins: [
@ -99,7 +100,8 @@ plugins: [
disableCookies: false,
cookieDomain: '*.example.org',
localScript: '/piwik.js',
dev: false
dev: false,
enableJSErrorTracking: true,
}
}
]

View File

@ -93,6 +93,14 @@ describe('gatsby-plugin-google-analytics', () => {
expect(result).toMatch(/disableCookies/)
})
it('sets enableJSErrorTracking', () => {
const { setPostBodyComponents } = setup({
enableJSErrorTracking: true
})
const result = JSON.stringify(setPostBodyComponents.mock.calls[0][0])
expect(result).toMatch(/enableJSErrorTracking/)
})
it('sets localScript', () => {
const { setPostBodyComponents } = setup({
localScript: 'TEST_LOCAL_SCRIPT'

View File

@ -13,6 +13,7 @@ function buildTrackingCode(pluginOptions) {
requireCookieConsent,
disableCookies,
cookieDomain,
enableJSErrorTracking,
respectDnt = true
} = pluginOptions
@ -33,6 +34,11 @@ function buildTrackingCode(pluginOptions) {
: ''
}
${disableCookies ? "window._paq.push(['disableCookies']);" : ''}
${
enableJSErrorTracking
? "window._paq.push(['enableJSErrorTracking']);"
: ''
}
${
cookieDomain
? `window._paq.push(['setCookieDomain', '${cookieDomain}']);`