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

[gatsby-plugin-google-gtag] Disable default pageview tracking #9842

Merged
merged 3 commits into from
Nov 15, 2018
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
15 changes: 10 additions & 5 deletions packages/gatsby-plugin-google-gtag/src/gatsby-ssr.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ exports.onRenderBody = (
) => {
if (process.env.NODE_ENV !== `production`) return null

const gtagConfig = pluginOptions.gtagConfig || {}

// Prevent duplicate or excluded pageview events being emitted on initial load of page by the `config` command
// https://developers.google.com/analytics/devguides/collection/gtagjs/#disable_pageview_tracking

gtagConfig.send_page_view = false

const firstTrackingId =
pluginOptions.trackingIds && pluginOptions.trackingIds.length
? pluginOptions.trackingIds[0]
Expand All @@ -31,8 +38,8 @@ exports.onRenderBody = (
: ``
}
${
typeof pluginOptions.gtagConfig.anonymize_ip !== `undefined` &&
pluginOptions.gtagConfig.anonymize_ip === true
typeof gtagConfig.anonymize_ip !== `undefined` &&
gtagConfig.anonymize_ip === true
? `function gaOptout(){document.cookie=disableStr+'=true; expires=Thu, 31 Dec 2099 23:59:59 UTC;path=/',window[disableStr]=!0}var gaProperty='${firstTrackingId}',disableStr='ga-disable-'+gaProperty;document.cookie.indexOf(disableStr+'=true')>-1&&(window[disableStr]=!0);`
: ``
}
Expand All @@ -49,9 +56,7 @@ exports.onRenderBody = (
${pluginOptions.trackingIds
.map(
trackingId =>
`gtag('config', '${trackingId}', ${JSON.stringify(
pluginOptions.gtagConfig || {}
)});`
`gtag('config', '${trackingId}', ${JSON.stringify(gtagConfig)});`
)
.join(``)}
}
Expand Down