From ae2e8e7a6101e01a74595e02fd68f517e7185584 Mon Sep 17 00:00:00 2001
From: Sam Richards <sbrichards@gmail.com>
Date: Sun, 23 May 2021 02:37:14 -0700
Subject: [PATCH] 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>
---
 README.md                   | 36 +++++++++++++++++++-----------------
 src/__tests__/gatsby-ssr.js |  8 ++++++++
 src/gatsby-ssr.js           |  6 ++++++
 3 files changed, 33 insertions(+), 17 deletions(-)

diff --git a/README.md b/README.md
index eebf49d..c991bcd 100644
--- a/README.md
+++ b/README.md
@@ -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,
     }
   }
 ]
diff --git a/src/__tests__/gatsby-ssr.js b/src/__tests__/gatsby-ssr.js
index efb016e..78fa880 100644
--- a/src/__tests__/gatsby-ssr.js
+++ b/src/__tests__/gatsby-ssr.js
@@ -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'
diff --git a/src/gatsby-ssr.js b/src/gatsby-ssr.js
index 3ec3541..ff4ea58 100644
--- a/src/gatsby-ssr.js
+++ b/src/gatsby-ssr.js
@@ -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}']);`