Skip to content

Commit

Permalink
feat: google analytics
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Apr 13, 2018
1 parent c4d3c36 commit 764ccd5
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 2 deletions.
2 changes: 2 additions & 0 deletions docs/config/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ sidebar: auto

### dest

### ga

## Theming

### theme
Expand Down
3 changes: 1 addition & 2 deletions docs/guide/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,9 @@ Each markdown file is compiled into HTML with [markdown-it](https://github.com/m
VuePress is still a work in progress. There are a few things that it currently does not support but are planned:

- Dropdown Items in Navbar
- Google Analytics Integration
- Algolia DocSearch Integration
- Multi-Language Support
- PWA Support
- Algolia DocSearch Integration
- Blogging support

Contributions are welcome!
Expand Down
26 changes: 26 additions & 0 deletions lib/app/clientEntry.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,34 @@
/* global GA_ID, ga */

import './.temp/polyfill'
import { createApp } from './app'

const { app, router } = createApp()

// Google analytics integration
if (process.env.NODE_ENV === 'production' && GA_ID) {
(function (i, s, o, g, r, a, m) {
i['GoogleAnalyticsObject'] = r
i[r] = i[r] || function () {
(i[r].q = i[r].q || []).push(arguments)
}
i[r].l = 1 * new Date()
a = s.createElement(o)
m = s.getElementsByTagName(o)[0]
a.async = 1
a.src = g
m.parentNode.insertBefore(a, m)
})(window, document, 'script', 'https://www.google-analytics.com/analytics.js', 'ga')

ga('create', GA_ID, 'auto')
ga('send', 'pageview')

router.afterEach(function (to) {
ga('set', 'page', to.fullPath)
ga('send', 'pageview')
})
}

router.onReady(() => {
app.$mount('#app')
})
7 changes: 7 additions & 0 deletions lib/webpack/createBaseConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,5 +210,12 @@ module.exports = function createBaseConfig ({
})
}

// inject Google analytics ID
config
.plugin('ga')
.use(require('webpack/lib/DefinePlugin'), [{
GA_ID: siteConfig.ga ? JSON.stringify(siteConfig.ga) : `false`
}])

return config
}

0 comments on commit 764ccd5

Please sign in to comment.