feat(v2): support IP anonymization for Google Analytics plugin (#2148)

* Support IP anonymization for google analytics

* Fixes

* Add docs

* typo fix

* Fix syntax

* document in suggested place

* attempted to add functionality to gtag plugin

* Remove from api-site-config

* document in appropriate places

* fix anonymize IP

Co-authored-by: Yangshun Tay <tay.yang.shun@gmail.com>
This commit is contained in:
Reece Dunham 2020-01-12 08:27:08 -05:00 committed by Yangshun Tay
parent 5e821fd4e0
commit ad031a15a4
3 changed files with 10 additions and 3 deletions

View file

@ -18,7 +18,7 @@ module.exports = function(context) {
); );
} }
const {trackingID} = googleAnalytics; const {trackingID, anonymizeIP} = googleAnalytics;
if (!trackingID) { if (!trackingID) {
throw new Error( throw new Error(
@ -53,6 +53,7 @@ module.exports = function(context) {
innerHTML: ` innerHTML: `
window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date; window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date;
ga('create', '${trackingID}', 'auto'); ga('create', '${trackingID}', 'auto');
ga('set', 'anonymizeIp', ${anonymizeIP});
ga('send', 'pageview'); ga('send', 'pageview');
`, `,
}, },

View file

@ -18,7 +18,7 @@ module.exports = function(context) {
); );
} }
const {trackingID} = gtag; const {anonymizeIP, trackingID} = gtag;
if (!trackingID) { if (!trackingID) {
throw new Error( throw new Error(
@ -70,7 +70,9 @@ module.exports = function(context) {
window.dataLayer = window.dataLayer || []; window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);} function gtag(){dataLayer.push(arguments);}
gtag('js', new Date()); gtag('js', new Date());
gtag('config', '${trackingID}');`, gtag('config', '${trackingID}', { ${
anonymizeIP ? "'anonymize_ip': true" : ''
} });`,
}, },
], ],
}; };

View file

@ -312,6 +312,8 @@ module.exports = {
themeConfig: { themeConfig: {
googleAnalytics: { googleAnalytics: {
trackingID: 'UA-141789564-1', trackingID: 'UA-141789564-1',
// Optional fields.
anonymizeIP: true, // Should IPs be anonymized?
}, },
}, },
}; };
@ -338,6 +340,8 @@ module.exports = {
themeConfig: { themeConfig: {
gtag: { gtag: {
trackingID: 'UA-141789564-1', trackingID: 'UA-141789564-1',
// Optional fields.
anonymizeIP: true, // Should IPs be anonymized?
}, },
}, },
}; };