diff --git a/docs/config/README.md b/docs/config/README.md index 0067ef8959..01460fbfea 100644 --- a/docs/config/README.md +++ b/docs/config/README.md @@ -18,6 +18,8 @@ sidebar: auto ### dest +### ga + ## Theming ### theme diff --git a/docs/guide/README.md b/docs/guide/README.md index 31a3a5084b..97f0c60f1f 100644 --- a/docs/guide/README.md +++ b/docs/guide/README.md @@ -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! diff --git a/lib/app/clientEntry.js b/lib/app/clientEntry.js index b7f2788e50..093afa69af 100644 --- a/lib/app/clientEntry.js +++ b/lib/app/clientEntry.js @@ -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') }) diff --git a/lib/webpack/createBaseConfig.js b/lib/webpack/createBaseConfig.js index d3d8a758c0..4a7dea8e2d 100644 --- a/lib/webpack/createBaseConfig.js +++ b/lib/webpack/createBaseConfig.js @@ -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 }