From a498aa1ba01d8e93815e3d2ea87262e1b2e21ebf Mon Sep 17 00:00:00 2001 From: David Marby Date: Mon, 19 Aug 2019 13:32:00 +0200 Subject: [PATCH] feat: Add anonymizeIp option to saber-plugin-google-analytics (#363) * Add anonymizeIp to saber-plugin-google-analytics * refactor we already called `send` in `afterEach` hook * add readme * typo --- .../saber-plugin-google-analytics/README.md | 40 +++++++++++++++++++ .../saber-plugin-google-analytics/index.js | 5 ++- .../saber-browser.js | 5 ++- 3 files changed, 47 insertions(+), 3 deletions(-) create mode 100644 packages/saber-plugin-google-analytics/README.md diff --git a/packages/saber-plugin-google-analytics/README.md b/packages/saber-plugin-google-analytics/README.md new file mode 100644 index 000000000..b5e96b1c9 --- /dev/null +++ b/packages/saber-plugin-google-analytics/README.md @@ -0,0 +1,40 @@ +# saber-plugin-google-analytics + +Add Google Analytics to your Saber sites. + +## Install + +```bash +yarn add saber-plugin-google-analytics +``` + +## Usage + +In your `saber-config.yml`: + +```yml +plugins: + - resolve: saber-plugin-google-analytics + options: + trackId: UA-XXX-XX # Google Analytics Track ID +``` + +## Plugin Options + +### trackId + +- Type: `string` +- Required: `true` + +Google Analytics Track ID. + +### anonymizeIp + +- Type: `boolean` +- Default: `false` + +To anonymize the IP address for all hits sent from a single tracker, set the `anonymizeIp` option to `true`. + +## License + +MIT. diff --git a/packages/saber-plugin-google-analytics/index.js b/packages/saber-plugin-google-analytics/index.js index 3c7dd5934..aa1b0f2fb 100644 --- a/packages/saber-plugin-google-analytics/index.js +++ b/packages/saber-plugin-google-analytics/index.js @@ -4,11 +4,12 @@ const ID = 'google-analytics' exports.name = ID -exports.apply = (api, { trackId = false } = {}) => { +exports.apply = (api, { trackId = false, anonymizeIp = false } = {}) => { api.hooks.chainWebpack.tap(ID, config => { config.plugin('constants').tap(([options]) => [ Object.assign(options, { - __GA_TRACK_ID__: JSON.stringify(trackId) + __GA_TRACK_ID__: JSON.stringify(trackId), + __GA_ANONYMIZE_IP: JSON.stringify(anonymizeIp) }) ]) }) diff --git a/packages/saber-plugin-google-analytics/saber-browser.js b/packages/saber-plugin-google-analytics/saber-browser.js index 92d37d28a..6dfa3dfc8 100644 --- a/packages/saber-plugin-google-analytics/saber-browser.js +++ b/packages/saber-plugin-google-analytics/saber-browser.js @@ -37,7 +37,10 @@ export default function({ router }) { })(window, document, 'script', 'https://www.google-analytics.com/analytics.js', 'ga') ga('create', __GA_TRACK_ID__, 'auto') - ga('send', 'pageview') + + if (__GA_ANONYMIZE_IP) { + ga('set', 'anonymizeIp', true) + } router.afterEach(to => { ga('set', 'page', to.fullPath)