Skip to content

Commit e883487

Browse files
laradevittwardpeet
authored andcommitted
fix(gatsby-plugin-google-analytics): Don't create tracking cod… (#19592)
1 parent 7b500f4 commit e883487

File tree

4 files changed

+20
-5
lines changed

4 files changed

+20
-5
lines changed

packages/gatsby-plugin-google-analytics/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ module.exports = {
1515
{
1616
resolve: `gatsby-plugin-google-analytics`,
1717
options: {
18+
// The property ID; the tracking code won't be generated without it
1819
trackingId: "YOUR_GOOGLE_ANALYTICS_TRACKING_ID",
1920
// Defines where to place the tracking script - `true` in the head and `false` in the body
2021
head: false,

packages/gatsby-plugin-google-analytics/src/__tests__/gatsby-ssr.js

+11-4
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ describe(`gatsby-plugin-google-analytics`, () => {
2929
const setHeadComponents = jest.fn()
3030
const setPostBodyComponents = jest.fn()
3131

32-
options = Object.assign({}, options)
32+
options = Object.assign({ trackingId: `TEST_TRACKING_ID` }, options)
3333

3434
onRenderBody({ setHeadComponents, setPostBodyComponents }, options)
3535

@@ -39,6 +39,15 @@ describe(`gatsby-plugin-google-analytics`, () => {
3939
}
4040
}
4141

42+
it(`does not set tracking script when trackingId not given`, () => {
43+
const { setHeadComponents, setPostBodyComponents } = setup({
44+
trackingId: ``,
45+
})
46+
47+
expect(setHeadComponents).not.toHaveBeenCalled()
48+
expect(setPostBodyComponents).not.toHaveBeenCalled()
49+
})
50+
4251
it(`sets tracking script`, () => {
4352
const { setHeadComponents, setPostBodyComponents } = setup()
4453

@@ -56,9 +65,7 @@ describe(`gatsby-plugin-google-analytics`, () => {
5665
})
5766

5867
it(`sets trackingId`, () => {
59-
const { setPostBodyComponents } = setup({
60-
trackingId: `TEST_TRACKING_ID`,
61-
})
68+
const { setPostBodyComponents } = setup()
6269

6370
const result = JSON.stringify(setPostBodyComponents.mock.calls[0][0])
6471

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
exports.onPreInit = ({ reporter }, options) => {
2+
if (!options.trackingId) {
3+
reporter.warn(
4+
`The Google Analytics plugin requires a tracking ID. Did you mean to add it?`
5+
)
6+
}
7+
}

packages/gatsby-plugin-google-analytics/src/gatsby-ssr.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export const onRenderBody = (
2727
{ setHeadComponents, setPostBodyComponents },
2828
pluginOptions
2929
) => {
30-
if (process.env.NODE_ENV !== `production`) {
30+
if (process.env.NODE_ENV !== `production` || !pluginOptions.trackingId) {
3131
return null
3232
}
3333

0 commit comments

Comments
 (0)