diff --git a/packages/gatsby-plugin-google-analytics/README.md b/packages/gatsby-plugin-google-analytics/README.md index 29a09d24d54ab..04d59f98c01ac 100644 --- a/packages/gatsby-plugin-google-analytics/README.md +++ b/packages/gatsby-plugin-google-analytics/README.md @@ -15,6 +15,7 @@ module.exports = { { resolve: `gatsby-plugin-google-analytics`, options: { + // The property ID; the tracking code won't be generated without it trackingId: "YOUR_GOOGLE_ANALYTICS_TRACKING_ID", // Defines where to place the tracking script - `true` in the head and `false` in the body head: false, diff --git a/packages/gatsby-plugin-google-analytics/src/__tests__/gatsby-ssr.js b/packages/gatsby-plugin-google-analytics/src/__tests__/gatsby-ssr.js index 7ebc21e137ab2..2f772b897f532 100644 --- a/packages/gatsby-plugin-google-analytics/src/__tests__/gatsby-ssr.js +++ b/packages/gatsby-plugin-google-analytics/src/__tests__/gatsby-ssr.js @@ -29,7 +29,7 @@ describe(`gatsby-plugin-google-analytics`, () => { const setHeadComponents = jest.fn() const setPostBodyComponents = jest.fn() - options = Object.assign({}, options) + options = Object.assign({ trackingId: `TEST_TRACKING_ID` }, options) onRenderBody({ setHeadComponents, setPostBodyComponents }, options) @@ -39,6 +39,15 @@ describe(`gatsby-plugin-google-analytics`, () => { } } + it(`does not set tracking script when trackingId not given`, () => { + const { setHeadComponents, setPostBodyComponents } = setup({ + trackingId: ``, + }) + + expect(setHeadComponents).not.toHaveBeenCalled() + expect(setPostBodyComponents).not.toHaveBeenCalled() + }) + it(`sets tracking script`, () => { const { setHeadComponents, setPostBodyComponents } = setup() @@ -56,9 +65,7 @@ describe(`gatsby-plugin-google-analytics`, () => { }) it(`sets trackingId`, () => { - const { setPostBodyComponents } = setup({ - trackingId: `TEST_TRACKING_ID`, - }) + const { setPostBodyComponents } = setup() const result = JSON.stringify(setPostBodyComponents.mock.calls[0][0]) diff --git a/packages/gatsby-plugin-google-analytics/src/gatsby-node.js b/packages/gatsby-plugin-google-analytics/src/gatsby-node.js new file mode 100644 index 0000000000000..4bd3461dea050 --- /dev/null +++ b/packages/gatsby-plugin-google-analytics/src/gatsby-node.js @@ -0,0 +1,7 @@ +exports.onPreInit = ({ reporter }, options) => { + if (!options.trackingId) { + reporter.warn( + `The Google Analytics plugin requires a tracking ID. Did you mean to add it?` + ) + } +} diff --git a/packages/gatsby-plugin-google-analytics/src/gatsby-ssr.js b/packages/gatsby-plugin-google-analytics/src/gatsby-ssr.js index 857061c351be4..2ef6737009e88 100644 --- a/packages/gatsby-plugin-google-analytics/src/gatsby-ssr.js +++ b/packages/gatsby-plugin-google-analytics/src/gatsby-ssr.js @@ -27,7 +27,7 @@ export const onRenderBody = ( { setHeadComponents, setPostBodyComponents }, pluginOptions ) => { - if (process.env.NODE_ENV !== `production`) { + if (process.env.NODE_ENV !== `production` || !pluginOptions.trackingId) { return null }