Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(gatsby-plugin-google-analytics): Add preconnect and dns-prefetch #12826

Merged
merged 5 commits into from
Mar 26, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}}
/>,
])
}