Skip to content

Commit

Permalink
feat: Add anonymizeIp option to saber-plugin-google-analytics (#363)
Browse files Browse the repository at this point in the history
* Add anonymizeIp to saber-plugin-google-analytics

* refactor

we already called `send` in `afterEach` hook

* add readme

* typo
  • Loading branch information
DMarby authored and egoist committed Aug 19, 2019
1 parent 5091335 commit a498aa1
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 3 deletions.
40 changes: 40 additions & 0 deletions packages/saber-plugin-google-analytics/README.md
Original file line number Diff line number Diff line change
@@ -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.
5 changes: 3 additions & 2 deletions packages/saber-plugin-google-analytics/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
])
})
Expand Down
5 changes: 4 additions & 1 deletion packages/saber-plugin-google-analytics/saber-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit a498aa1

Please sign in to comment.