diff --git a/CHANGELOG.md b/CHANGELOG.md index a44a1396c..a2545db55 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +## 2.2.0 + +### Features +- Add `Google Analytics` plugin. + ## 2.1.0 ### Features - Add search plugin diff --git a/README.md b/README.md index b5d7e09a8..a1f126183 100644 --- a/README.md +++ b/README.md @@ -76,13 +76,12 @@ These open-source projects are using docsify to generate their sites. Pull reque ## Development -### prepare ```bash npm i && npm run dev open http://localhost:3000 ``` -### More Language Highlight +## More Language Highlight ```html diff --git a/build/build.js b/build/build.js index 62314e585..cbeefc929 100644 --- a/build/build.js +++ b/build/build.js @@ -47,3 +47,14 @@ isProd && build({ moduleName: 'D.Search', plugins: [uglify()] }) +build({ + entry: 'plugins/ga.js', + output: 'plugins/ga.js', + moduleName: 'D.GA' +}) +isProd && build({ + entry: 'plugins/ga.js', + output: 'plugins/ga.min.js', + moduleName: 'D.GA', + plugins: [uglify()] +}) diff --git a/docs/README.md b/docs/README.md index 8a1eb9ec6..5be78842d 100644 --- a/docs/README.md +++ b/docs/README.md @@ -468,7 +468,7 @@ If a document can have a search, can enhance some user experience. Installing th ```html - + ``` @@ -495,3 +495,20 @@ window.$docsify = { } } ``` + +### Google Analytics + +Install the plugin and configure the track id. + +```html + + +``` + +or + +```js +window.$docsify = { + ga: 'UA-XXXXX-Y' +} +``` diff --git a/docs/zh-cn.md b/docs/zh-cn.md index 0b193953f..0a1405859 100644 --- a/docs/zh-cn.md +++ b/docs/zh-cn.md @@ -501,3 +501,20 @@ window.$docsify = { } } ``` + +### Google Analytics + +安装插件并且配置 track id。 + +```html + + +``` + +或者 + +```js +window.$docsify = { + ga: 'UA-XXXXX-Y' +} +``` diff --git a/src/index.js b/src/index.js index 19a365178..3604f3f6a 100644 --- a/src/index.js +++ b/src/index.js @@ -15,7 +15,8 @@ const OPTIONS = utils.merge({ auto2top: false, name: '', themeColor: '', - nameLink: window.location.pathname + nameLink: window.location.pathname, + ga: '' }, window.$docsify) const script = document.currentScript || [].slice.call(document.getElementsByTagName('script')).pop() diff --git a/src/plugins/ga.js b/src/plugins/ga.js new file mode 100644 index 000000000..5200240c0 --- /dev/null +++ b/src/plugins/ga.js @@ -0,0 +1,42 @@ +// From https://github.com/egoist/vue-ga/blob/master/src/index.js + +function appendScript () { + const script = document.createElement('script') + script.async = true + script.src = 'https://www.google-analytics.com/analytics.js' + document.body.appendChild(script) +} + +function init (id) { + if (!window.ga) { + appendScript() + window.ga = window.ga || function () { + (window.ga.q = window.ga.q || []).push(arguments) + } + window.ga.l = Number(new Date()) + window.ga('create', id, 'auto') + } +} + +function collect () { + init(window.$docsify.ga) + window.ga('set', 'page', location.href) + window.ga('send', 'pageview') +} + +const install = function () { + if (!window.Docsify || !window.Docsify.installed) { + console.error('[Docsify] Please load docsify.js first.') + return + } + + if (!window.$docsify.ga) { + console.error('[Docsify] ga is required.') + return + } + + collect() + window.$docsify.plugins = [].concat(window.$docsify.plugins, collect) +} + +export default install()