Merge pull request #5 from chengpeiquan/develop

Develop
This commit is contained in:
chengpeiquan 2021-01-08 23:45:48 +08:00 committed by GitHub
commit 377337d53b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 32 additions and 6 deletions

View File

@ -1,7 +1,7 @@
vue-baidu-analytics 使用说明 vue-baidu-analytics 使用说明
=== ===
基于Vue开发的百度统计插件可以在 `Vue-CLI脚手架项目` 或者 `引入了Vue相关CDN的普通页面` 上使用,使用本插件的项目需要引入 `Vue Router` 基于Vue开发的百度统计插件可以在 `Vue-CLI脚手架项目` 或者 `引入了Vue相关CDN的普通页面`,以及 `VuePress` 项目上使用,使用本插件的项目需要引入 `Vue Router`
> @v2.0版本更新:<br>最新版支持 Vue 3.x同时兼容 Vue 2.x 使用具体使用方法请看下方说明及demo。<br>对Vue 3.0感兴趣,但还在观望的同学,欢迎阅读我踩坑总结的:[Vue 3.0 学习教程](https://vue3.chengpeiquan.com/) 持续更新ing > @v2.0版本更新:<br>最新版支持 Vue 3.x同时兼容 Vue 2.x 使用具体使用方法请看下方说明及demo。<br>对Vue 3.0感兴趣,但还在观望的同学,欢迎阅读我踩坑总结的:[Vue 3.0 学习教程](https://vue3.chengpeiquan.com/) 持续更新ing
@ -104,6 +104,30 @@ createApp(app)
.mount('#app'); .mount('#app');
``` ```
### 在 VuePress 里使用
插件也支持在Vue的静态文档 [VuePress](https://vuepress.vuejs.org/zh/) 项目里使用。
在项目下的 `/docs/.vuepress` 文件夹下,创建一个 `enhanceApp.js`,按照下面的方式引入即可启动数据上报功能。
官方文档传送门:[应用级别的配置 - VuePress](https://vuepress.vuejs.org/zh/guide/basic-config.html#%E5%BA%94%E7%94%A8%E7%BA%A7%E5%88%AB%E7%9A%84%E9%85%8D%E7%BD%AE)
```js
import baiduAnalytics from 'vue-baidu-analytics'
export default ({ Vue, router }) => {
Vue.use(baiduAnalytics, {
router: router,
siteIdList: [
'aaaaaaaaaaaaaaaaaaa',
'bbbbbbbbbbbbbbbbbbb',
'ccccccccccccccccccc'
],
isDebug: false
});
};
```
可在开发环境打开debug模式了解相关的上报情况上线前记得关闭debug 可在开发环境打开debug模式了解相关的上报情况上线前记得关闭debug
## 方法 ## 方法

View File

@ -1,6 +1,6 @@
/** /**
* name: vue-baidu-analytics * name: vue-baidu-analytics
* version: v2.0.0 * version: v2.0.1
* author: chengpeiquan * author: chengpeiquan
*/ */
(function (global, factory) { (function (global, factory) {

View File

@ -1,6 +1,6 @@
/** /**
* name: vue-baidu-analytics * name: vue-baidu-analytics
* version: v2.0.0 * version: v2.0.1
* author: chengpeiquan * author: chengpeiquan
*/ */
!function(t,i){"object"==typeof exports&&"undefined"!=typeof module?module.exports=i():"function"==typeof define&&define.amd?define(i):(t="undefined"!=typeof globalThis?globalThis:t||self).baiduAnalytics=i()}(this,(function(){"use strict";var t=function(){function t(t,i){void 0===t&&(t=""),void 0===i&&(i=!1),this.siteId=t,this.isDebug=i}return t.prototype.init=function(){window._hmt=window._hmt?window._hmt:[];var t=document.createElement("script");t.async=!0,t.src="https://hm.baidu.com/hm.js?"+this.siteId,document.querySelector("head").appendChild(t),this.isDebug&&console.log("[vue-baidu-analytics] siteId load done.\nsiteId: "+this.siteId)},t.prototype.setAccount=function(){window._hmt.push(["_setAccount",this.siteId])},t.prototype.trackPageview=function(t){if(t&&"string"==typeof t||(t="/"),t.includes("http")){var i=t.split("/"),e=i[0]+"//"+i[2];t=t.replace(e,"")}this.setAccount(),window._hmt.push(["_trackPageview",t]),this.isDebug&&console.log("[vue-baidu-analytics] track pv done.\nsiteId: "+this.siteId+"\npageUrl: "+t)},t.prototype.trackEvent=function(t,i,e,n){if("string"!=typeof t||"string"!=typeof i||!t||!i)throw new Error("[vue-baidu-analytics] Missing necessary category and operation information, and must be of type string.");e&&"string"==typeof e||(e=""),Number(n)||(n=1),this.setAccount(),window._hmt.push(["_trackEvent",t,i,e,n]),this.isDebug&&console.log("[vue-baidu-analytics] track event done.\nsiteId: "+this.siteId+"\ncategory: "+t+"\naction: "+i+"\nlabel: "+e+"\nvalue: "+n)},t}(),i=function(){function i(t,i){this.siteIdList=t,this.isDebug=i}return i.prototype.init=function(){var i=this;this.siteIdList.forEach((function(e){new t(e,i.isDebug).init()}))},i.prototype.pv=function(i){var e=this;this.siteIdList.forEach((function(n){new t(n,e.isDebug).trackPageview(i)}))},i.prototype.event=function(i,e,n,o){var s=this;this.siteIdList.forEach((function(a){new t(a,s.isDebug).trackEvent(i,e,n,o)}))},i}();return function(t,e){var n=e.router,o=e.siteIdList,s=e.isDebug,a=void 0!==s&&s;if("undefined"==typeof document||"undefined"==typeof window)return!1;if(!n)throw new Error("[vue-baidu-analytics] Must pass a Vue-Router instance to vue-baidu-analytics.");if(!o)throw new Error("[vue-baidu-analytics] Missing tracking domain ID, add at least one of baidu analytics.");var r=new i(o,a),u=function(t){var i=2,e=String(t.version);return"2."===e.slice(0,2)&&(i=2),"3."===e.slice(0,2)&&(i=3),i}(t)||2;2===u&&(t.prototype.$pushBAIDU=r),3===u&&(t.config.globalProperties.$pushBAIDU=r),o&&r.init(),n.afterEach((function(t){var i=window.location.pathname.split("/").length,e=window.location.pathname.split("/").slice(0,i-1).join("/"),o="hash"===n.mode?e+"/#"+t.fullPath:""+e+t.fullPath;r.pv(o)}))}})); !function(t,i){"object"==typeof exports&&"undefined"!=typeof module?module.exports=i():"function"==typeof define&&define.amd?define(i):(t="undefined"!=typeof globalThis?globalThis:t||self).baiduAnalytics=i()}(this,(function(){"use strict";var t=function(){function t(t,i){void 0===t&&(t=""),void 0===i&&(i=!1),this.siteId=t,this.isDebug=i}return t.prototype.init=function(){window._hmt=window._hmt?window._hmt:[];var t=document.createElement("script");t.async=!0,t.src="https://hm.baidu.com/hm.js?"+this.siteId,document.querySelector("head").appendChild(t),this.isDebug&&console.log("[vue-baidu-analytics] siteId load done.\nsiteId: "+this.siteId)},t.prototype.setAccount=function(){window._hmt.push(["_setAccount",this.siteId])},t.prototype.trackPageview=function(t){if(t&&"string"==typeof t||(t="/"),t.includes("http")){var i=t.split("/"),e=i[0]+"//"+i[2];t=t.replace(e,"")}this.setAccount(),window._hmt.push(["_trackPageview",t]),this.isDebug&&console.log("[vue-baidu-analytics] track pv done.\nsiteId: "+this.siteId+"\npageUrl: "+t)},t.prototype.trackEvent=function(t,i,e,n){if("string"!=typeof t||"string"!=typeof i||!t||!i)throw new Error("[vue-baidu-analytics] Missing necessary category and operation information, and must be of type string.");e&&"string"==typeof e||(e=""),Number(n)||(n=1),this.setAccount(),window._hmt.push(["_trackEvent",t,i,e,n]),this.isDebug&&console.log("[vue-baidu-analytics] track event done.\nsiteId: "+this.siteId+"\ncategory: "+t+"\naction: "+i+"\nlabel: "+e+"\nvalue: "+n)},t}(),i=function(){function i(t,i){this.siteIdList=t,this.isDebug=i}return i.prototype.init=function(){var i=this;this.siteIdList.forEach((function(e){new t(e,i.isDebug).init()}))},i.prototype.pv=function(i){var e=this;this.siteIdList.forEach((function(n){new t(n,e.isDebug).trackPageview(i)}))},i.prototype.event=function(i,e,n,o){var s=this;this.siteIdList.forEach((function(a){new t(a,s.isDebug).trackEvent(i,e,n,o)}))},i}();return function(t,e){var n=e.router,o=e.siteIdList,s=e.isDebug,a=void 0!==s&&s;if("undefined"==typeof document||"undefined"==typeof window)return!1;if(!n)throw new Error("[vue-baidu-analytics] Must pass a Vue-Router instance to vue-baidu-analytics.");if(!o)throw new Error("[vue-baidu-analytics] Missing tracking domain ID, add at least one of baidu analytics.");var r=new i(o,a),u=function(t){var i=2,e=String(t.version);return"2."===e.slice(0,2)&&(i=2),"3."===e.slice(0,2)&&(i=3),i}(t)||2;2===u&&(t.prototype.$pushBAIDU=r),3===u&&(t.config.globalProperties.$pushBAIDU=r),o&&r.init(),n.afterEach((function(t){var i=window.location.pathname.split("/").length,e=window.location.pathname.split("/").slice(0,i-1).join("/"),o="hash"===n.mode?e+"/#"+t.fullPath:""+e+t.fullPath;r.pv(o)}))}}));

View File

@ -1,7 +1,7 @@
{ {
"name": "vue-baidu-analytics", "name": "vue-baidu-analytics",
"version": "2.0.0", "version": "2.0.1",
"description": "A data collection tool that supports reporting of single-page application data built by Vue 3.0 & 2.0, based on baidu statistics.", "description": "A data collection tool that supports reporting of single-page application data built by Vue 3.0 & 2.0 & VuePress, based on baidu statistics.",
"main": "dist/vue-baidu-analytics.min.js", "main": "dist/vue-baidu-analytics.min.js",
"types": "vue-baidu-analytics.d.ts", "types": "vue-baidu-analytics.d.ts",
"scripts": { "scripts": {
@ -16,11 +16,13 @@
"vue baidu", "vue baidu",
"vue 百度统计", "vue 百度统计",
"vue analytics", "vue analytics",
"vuepress analytics",
"vue 3.0 baidu", "vue 3.0 baidu",
"vue 3.0 百度统计", "vue 3.0 百度统计",
"vue 3.0 analytics", "vue 3.0 analytics",
"spa 3.0 analytics", "spa analytics",
"baidu统计", "baidu统计",
"vuepress统计",
"百度统计" "百度统计"
], ],
"author": "chengpeiquan", "author": "chengpeiquan",