Skip to content

Commit 0640104

Browse files
pgiladpiehwardpeet
committed
feat(gatsby-plugin-google-analytics): Add preconnect and dns-prefetch (#12826)
* Add preconnect and dns-prefetch to google analytics * chore: format * Join preconnect and dns-prefetch to a single link * Swap places of dns-prefetch and preconnect Co-authored-by: Michal Piechowiak <[email protected]> Co-authored-by: Ward Peeters <[email protected]>
1 parent ba36f8d commit 0640104

File tree

1 file changed

+45
-36
lines changed

1 file changed

+45
-36
lines changed

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

+45-36
Original file line numberDiff line numberDiff line change
@@ -18,31 +18,43 @@ exports.onRenderBody = (
1818
{ setHeadComponents, setPostBodyComponents },
1919
pluginOptions
2020
) => {
21-
if (process.env.NODE_ENV === `production`) {
22-
let excludeGAPaths = []
23-
if (typeof pluginOptions.exclude !== `undefined`) {
24-
const Minimatch = require(`minimatch`).Minimatch
25-
pluginOptions.exclude.map(exclude => {
26-
const mm = new Minimatch(exclude)
27-
excludeGAPaths.push(mm.makeRe())
28-
})
29-
}
21+
if (process.env.NODE_ENV !== `production`) {
22+
return null
23+
}
3024

31-
const gaCreateOptions = {}
32-
for (const option in knownOptions) {
33-
if (typeof pluginOptions[option] === knownOptions[option]) {
34-
gaCreateOptions[option] = pluginOptions[option]
35-
}
25+
// Lighthouse recommends pre-connecting to google analytics
26+
setHeadComponents([
27+
<link
28+
rel="preconnect dns-prefetch"
29+
key="preconnect-google-analytics"
30+
href="https://www.google-analytics.com"
31+
/>,
32+
])
33+
34+
let excludeGAPaths = []
35+
if (typeof pluginOptions.exclude !== `undefined`) {
36+
const Minimatch = require(`minimatch`).Minimatch
37+
pluginOptions.exclude.map(exclude => {
38+
const mm = new Minimatch(exclude)
39+
excludeGAPaths.push(mm.makeRe())
40+
})
41+
}
42+
43+
const gaCreateOptions = {}
44+
for (const option in knownOptions) {
45+
if (typeof pluginOptions[option] === knownOptions[option]) {
46+
gaCreateOptions[option] = pluginOptions[option]
3647
}
48+
}
3749

38-
const setComponents = pluginOptions.head
39-
? setHeadComponents
40-
: setPostBodyComponents
41-
return setComponents([
42-
<script
43-
key={`gatsby-plugin-google-analytics`}
44-
dangerouslySetInnerHTML={{
45-
__html: `
50+
const setComponents = pluginOptions.head
51+
? setHeadComponents
52+
: setPostBodyComponents
53+
return setComponents([
54+
<script
55+
key={`gatsby-plugin-google-analytics`}
56+
dangerouslySetInnerHTML={{
57+
__html: `
4658
${
4759
excludeGAPaths.length
4860
? `window.excludeGAPaths=[${excludeGAPaths.join(`,`)}];`
@@ -69,14 +81,14 @@ exports.onRenderBody = (
6981
}
7082
if (typeof ga === "function") {
7183
ga('create', '${pluginOptions.trackingId}', '${
72-
typeof pluginOptions.cookieDomain === `string`
73-
? pluginOptions.cookieDomain
74-
: `auto`
75-
}', ${
76-
typeof pluginOptions.name === `string`
77-
? `'${pluginOptions.name}', `
78-
: ``
79-
}${JSON.stringify(gaCreateOptions)});
84+
typeof pluginOptions.cookieDomain === `string`
85+
? pluginOptions.cookieDomain
86+
: `auto`
87+
}', ${
88+
typeof pluginOptions.name === `string`
89+
? `'${pluginOptions.name}', `
90+
: ``
91+
}${JSON.stringify(gaCreateOptions)});
8092
${
8193
typeof pluginOptions.anonymize !== `undefined` &&
8294
pluginOptions.anonymize === true
@@ -99,10 +111,7 @@ exports.onRenderBody = (
99111
: ``
100112
}}
101113
`,
102-
}}
103-
/>,
104-
])
105-
}
106-
107-
return null
114+
}}
115+
/>,
116+
])
108117
}

0 commit comments

Comments
 (0)