Skip to content

Commit

Permalink
feat(gatsby-plugin-google-analytics): Add preconnect and dns-prefetch (
Browse files Browse the repository at this point in the history
…#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 <misiek.piechowiak@gmail.com>
Co-authored-by: Ward Peeters <ward@coding-tech.com>
  • Loading branch information
3 people committed Mar 26, 2019
1 parent ba36f8d commit 0640104
Showing 1 changed file with 45 additions and 36 deletions.
81 changes: 45 additions & 36 deletions packages/gatsby-plugin-google-analytics/src/gatsby-ssr.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,43 @@ exports.onRenderBody = (
{ setHeadComponents, setPostBodyComponents },
pluginOptions
) => {
if (process.env.NODE_ENV === `production`) {
let excludeGAPaths = []
if (typeof pluginOptions.exclude !== `undefined`) {
const Minimatch = require(`minimatch`).Minimatch
pluginOptions.exclude.map(exclude => {
const mm = new Minimatch(exclude)
excludeGAPaths.push(mm.makeRe())
})
}
if (process.env.NODE_ENV !== `production`) {
return null
}

const gaCreateOptions = {}
for (const option in knownOptions) {
if (typeof pluginOptions[option] === knownOptions[option]) {
gaCreateOptions[option] = pluginOptions[option]
}
// Lighthouse recommends pre-connecting to google analytics
setHeadComponents([
<link
rel="preconnect dns-prefetch"
key="preconnect-google-analytics"
href="https://www.google-analytics.com"
/>,
])

let excludeGAPaths = []
if (typeof pluginOptions.exclude !== `undefined`) {
const Minimatch = require(`minimatch`).Minimatch
pluginOptions.exclude.map(exclude => {
const mm = new Minimatch(exclude)
excludeGAPaths.push(mm.makeRe())
})
}

const gaCreateOptions = {}
for (const option in knownOptions) {
if (typeof pluginOptions[option] === knownOptions[option]) {
gaCreateOptions[option] = pluginOptions[option]
}
}

const setComponents = pluginOptions.head
? setHeadComponents
: setPostBodyComponents
return setComponents([
<script
key={`gatsby-plugin-google-analytics`}
dangerouslySetInnerHTML={{
__html: `
const setComponents = pluginOptions.head
? setHeadComponents
: setPostBodyComponents
return setComponents([
<script
key={`gatsby-plugin-google-analytics`}
dangerouslySetInnerHTML={{
__html: `
${
excludeGAPaths.length
? `window.excludeGAPaths=[${excludeGAPaths.join(`,`)}];`
Expand All @@ -69,14 +81,14 @@ exports.onRenderBody = (
}
if (typeof ga === "function") {
ga('create', '${pluginOptions.trackingId}', '${
typeof pluginOptions.cookieDomain === `string`
? pluginOptions.cookieDomain
: `auto`
}', ${
typeof pluginOptions.name === `string`
? `'${pluginOptions.name}', `
: ``
}${JSON.stringify(gaCreateOptions)});
typeof pluginOptions.cookieDomain === `string`
? pluginOptions.cookieDomain
: `auto`
}', ${
typeof pluginOptions.name === `string`
? `'${pluginOptions.name}', `
: ``
}${JSON.stringify(gaCreateOptions)});
${
typeof pluginOptions.anonymize !== `undefined` &&
pluginOptions.anonymize === true
Expand All @@ -99,10 +111,7 @@ exports.onRenderBody = (
: ``
}}
`,
}}
/>,
])
}

return null
}}
/>,
])
}

0 comments on commit 0640104

Please sign in to comment.