Skip to content

Commit f40b0e3

Browse files
committed
feat: add google analytics anonymizeIp in siteConfig
Easier privacy following vuejs#515
1 parent 6857aac commit f40b0e3

File tree

4 files changed

+26
-6
lines changed

4 files changed

+26
-6
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ https://vuepress.vuejs.org/
5151

5252
* [Multi-Language Support](https://vuepress.vuejs.org/guide/i18n.html)
5353
* [Service Worker](https://vuepress.vuejs.org/config/#serviceworker)
54-
* [Google Analytics](https://vuepress.vuejs.org/config/#ga)
54+
* [Google Analytics, GDPR-compliant](https://vuepress.vuejs.org/config/#ga)
5555

5656
## Showcase
5757

docs/config/README.md

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,25 @@ Specify the output directory for `vuepress build`.
7474

7575
### ga
7676

77-
- Type: `string`
77+
- Type: `string | { id: string, anonymizeIp: boolean }`
7878
- Default: `undefined`
7979

80-
Provide the Google Analytics ID to enable integration.
80+
Provide the Google Analytics ID as a string to enable integration.
81+
82+
You can also use an object to enable IP anonymization:
83+
84+
``` js
85+
module.exports = {
86+
//
87+
ga: {
88+
id: 'UA-XXXX-X',
89+
anonymizeIp: true // false by default for backwards compatibility
90+
}
91+
}
92+
```
8193

8294
::: tip
83-
Please be aware of [GDPR (2018 reform of EU data protection rules)](https://ec.europa.eu/commission/priorities/justice-and-fundamental-rights/data-protection/2018-reform-eu-data-protection-rules_en) and consider setting Google Analytics to [anonymize IPs](https://support.google.com/analytics/answer/2763052?hl=en) where appropriate and/or needed.
95+
Consider setting Google Analytics to [anonymize IPs](https://support.google.com/analytics/answer/2763052?hl=en) using `anonymizeIp: true` setting, to protect user privacy and comply with [GDPR (2018 reform of EU data protection rules)](https://ec.europa.eu/commission/priorities/justice-and-fundamental-rights/data-protection/2018-reform-eu-data-protection-rules_en).
8496
:::
8597

8698
### serviceWorker

lib/app/clientEntry.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* global BASE_URL, GA_ID, ga, SW_ENABLED, VUEPRESS_VERSION, LAST_COMMIT_HASH*/
1+
/* global BASE_URL, GA_ID, GA_ANONYMIZE_IP, ga, SW_ENABLED, VUEPRESS_VERSION, LAST_COMMIT_HASH*/
22

33
import { createApp } from './app'
44
import SWUpdateEvent from './SWUpdateEvent'
@@ -27,6 +27,10 @@ if (process.env.NODE_ENV === 'production' && GA_ID) {
2727
})(window, document, 'script', 'https://www.google-analytics.com/analytics.js', 'ga')
2828

2929
ga('create', GA_ID, 'auto')
30+
31+
// Improved privacy: https://support.google.com/analytics/answer/2763052
32+
if (GA_ANONYMIZE_IP) ga('set', 'anonymizeIp', true)
33+
3034
ga('send', 'pageview')
3135

3236
router.afterEach(function (to) {

lib/webpack/createBaseConfig.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,11 +289,15 @@ module.exports = function createBaseConfig ({
289289
}
290290

291291
// inject constants
292+
const hasGaObjectConfig = typeof siteConfig.ga === 'object' && typeof siteConfig.ga.id === 'string'
292293
config
293294
.plugin('injections')
294295
.use(require('webpack/lib/DefinePlugin'), [{
295296
BASE_URL: JSON.stringify(siteConfig.base || '/'),
296-
GA_ID: siteConfig.ga ? JSON.stringify(siteConfig.ga) : false,
297+
GA_ID: hasGaObjectConfig
298+
? JSON.stringify(siteConfig.ga.id)
299+
: (siteConfig.ga ? JSON.stringify(siteConfig.ga.id) : false),
300+
GA_ANONYMIZE_IP: siteConfig.ga && siteConfig.ga.anonymizeIp === true,
297301
SW_ENABLED: !!siteConfig.serviceWorker,
298302
VUEPRESS_VERSION: JSON.stringify(require('../../package.json').version),
299303
LAST_COMMIT_HASH: JSON.stringify(getLastCommitHash())

0 commit comments

Comments
 (0)