Skip to content

Commit 764ccd5

Browse files
committed
feat: google analytics
1 parent c4d3c36 commit 764ccd5

File tree

4 files changed

+36
-2
lines changed

4 files changed

+36
-2
lines changed

docs/config/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ sidebar: auto
1818

1919
### dest
2020

21+
### ga
22+
2123
## Theming
2224

2325
### theme

docs/guide/README.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,9 @@ Each markdown file is compiled into HTML with [markdown-it](https://github.com/m
2929
VuePress is still a work in progress. There are a few things that it currently does not support but are planned:
3030

3131
- Dropdown Items in Navbar
32-
- Google Analytics Integration
33-
- Algolia DocSearch Integration
3432
- Multi-Language Support
3533
- PWA Support
34+
- Algolia DocSearch Integration
3635
- Blogging support
3736

3837
Contributions are welcome!

lib/app/clientEntry.js

+26
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,34 @@
1+
/* global GA_ID, ga */
2+
13
import './.temp/polyfill'
24
import { createApp } from './app'
35

46
const { app, router } = createApp()
57

8+
// Google analytics integration
9+
if (process.env.NODE_ENV === 'production' && GA_ID) {
10+
(function (i, s, o, g, r, a, m) {
11+
i['GoogleAnalyticsObject'] = r
12+
i[r] = i[r] || function () {
13+
(i[r].q = i[r].q || []).push(arguments)
14+
}
15+
i[r].l = 1 * new Date()
16+
a = s.createElement(o)
17+
m = s.getElementsByTagName(o)[0]
18+
a.async = 1
19+
a.src = g
20+
m.parentNode.insertBefore(a, m)
21+
})(window, document, 'script', 'https://www.google-analytics.com/analytics.js', 'ga')
22+
23+
ga('create', GA_ID, 'auto')
24+
ga('send', 'pageview')
25+
26+
router.afterEach(function (to) {
27+
ga('set', 'page', to.fullPath)
28+
ga('send', 'pageview')
29+
})
30+
}
31+
632
router.onReady(() => {
733
app.$mount('#app')
834
})

lib/webpack/createBaseConfig.js

+7
Original file line numberDiff line numberDiff line change
@@ -210,5 +210,12 @@ module.exports = function createBaseConfig ({
210210
})
211211
}
212212

213+
// inject Google analytics ID
214+
config
215+
.plugin('ga')
216+
.use(require('webpack/lib/DefinePlugin'), [{
217+
GA_ID: siteConfig.ga ? JSON.stringify(siteConfig.ga) : `false`
218+
}])
219+
213220
return config
214221
}

0 commit comments

Comments
 (0)