Skip to content

Commit

Permalink
fix(gatsby-plugin-google-analytics): Don't create tracking cod… (#19592)
Browse files Browse the repository at this point in the history
  • Loading branch information
laradevitt authored and wardpeet committed Nov 18, 2019
1 parent 7b500f4 commit e883487
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 5 deletions.
1 change: 1 addition & 0 deletions packages/gatsby-plugin-google-analytics/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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()

Expand All @@ -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])

Expand Down
7 changes: 7 additions & 0 deletions packages/gatsby-plugin-google-analytics/src/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -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?`
)
}
}
2 changes: 1 addition & 1 deletion packages/gatsby-plugin-google-analytics/src/gatsby-ssr.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit e883487

Please sign in to comment.