mirror of
https://github.com/analyticsjs/vue-baidu-analytics.git
synced 2025-04-05 07:52:44 +08:00
1 line
10 KiB
Plaintext
1 line
10 KiB
Plaintext
{"version":3,"file":"vue-baidu-analytics.js","sources":["../src/modules/baidu.ts","../src/modules/pushBAIDU.ts","../src/modules/getVueVersion.ts","../src/main.ts"],"sourcesContent":["/** \r\n * 定义基础配置\r\n * 官方文档 https://tongji.baidu.com/open/api/more?p=guide_overview\r\n */\r\nclass BAIDU {\r\n siteId: string;\r\n isDebug: boolean;\r\n\r\n constructor (siteId: string = '', isDebug: boolean = false) {\r\n this.siteId = siteId;\r\n this.isDebug = isDebug;\r\n }\r\n\r\n /** \r\n * 初始化\r\n */\r\n init () {\r\n window._hmt = window._hmt ? window._hmt : [];\r\n const SCRIPT = document.createElement('script');\r\n SCRIPT['async'] = true;\r\n SCRIPT['src'] = `https://hm.baidu.com/hm.js?${this.siteId}`;\r\n document.querySelector('head').appendChild(SCRIPT);\r\n\r\n if ( this.isDebug ) {\r\n console.log(`[vue-baidu-analytics] siteId load done.\\nsiteId: ${this.siteId}`);\r\n }\r\n }\r\n\r\n /** \r\n * 设置要响应的站点\r\n */\r\n setAccount () {\r\n window._hmt.push(['_setAccount', this.siteId]);\r\n }\r\n\r\n /** \r\n * 提交PV、UV\r\n */\r\n trackPageview (pageUrl: string) {\r\n // 如果页面链接没传或者无效链接,则默认为根域名\r\n if ( !pageUrl || typeof pageUrl !== 'string' ) {\r\n pageUrl = '/';\r\n }\r\n\r\n // 如果页面链接带上了域名,则需要过滤掉\r\n if ( pageUrl.includes('http') ) {\r\n const PAGE_CUT = pageUrl.split('/');\r\n const HOST_NAME = `${PAGE_CUT[0]}//${PAGE_CUT[2]}`;\r\n pageUrl = pageUrl.replace(HOST_NAME, '');\r\n }\r\n\r\n // 设置响应id并提交数据\r\n this.setAccount();\r\n window._hmt.push(['_trackPageview', pageUrl]);\r\n\r\n if ( this.isDebug ) {\r\n console.log(`[vue-baidu-analytics] track pv done.\\nsiteId: ${this.siteId}\\npageUrl: ${pageUrl}`);\r\n }\r\n }\r\n\r\n /** \r\n * 提交点击事件\r\n */\r\n trackEvent (category: string, action: string, label: string, value: number) {\r\n // 前两个是必填项\r\n if ( typeof category !== 'string' || typeof action !== 'string' || !category || !action ) {\r\n throw new Error('[vue-baidu-analytics] Missing necessary category and operation information, and must be of type string.');\r\n return false;\r\n }\r\n\r\n // 重置一些无效的默认值\r\n if ( !label || typeof label !== 'string' ) {\r\n label = '';\r\n }\r\n\r\n if ( !Number(value) ) {\r\n value = 1;\r\n }\r\n\r\n // 设置响应id并提交数据\r\n this.setAccount();\r\n window._hmt.push(['_trackEvent', category, action, label, value]);\r\n\r\n if ( this.isDebug ) {\r\n console.log(`[vue-baidu-analytics] track event done.\\nsiteId: ${this.siteId}\\ncategory: ${category}\\naction: ${action}\\nlabel: ${label}\\nvalue: ${value}`);\r\n }\r\n }\r\n}\r\n\r\nexport default BAIDU;","import BAIDU from '@m/baidu'\r\n\r\n/** \r\n * 定义推送操作\r\n */\r\nclass PushBAIDU {\r\n siteIdList: string[];\r\n isDebug: boolean;\r\n\r\n constructor (siteIdList: string[], isDebug: boolean) {\r\n this.siteIdList = siteIdList;\r\n this.isDebug = isDebug;\r\n }\r\n\r\n /** \r\n * 批量部署站点\r\n */\r\n init () {\r\n this.siteIdList.forEach( (siteId: string) => {\r\n const SITE = new BAIDU(siteId, this.isDebug);\r\n SITE.init();\r\n });\r\n }\r\n\r\n /** \r\n * 批量提交pv上报\r\n */\r\n pv (pageUrl: string) {\r\n this.siteIdList.forEach( (siteId: string) => {\r\n const SITE = new BAIDU(siteId, this.isDebug);\r\n SITE.trackPageview(pageUrl);\r\n });\r\n }\r\n\r\n /** \r\n * 批量提交事件上报\r\n */\r\n event (category: string, action: string, label: string, value: number) {\r\n this.siteIdList.forEach( (siteId: string) => {\r\n const SITE = new BAIDU(siteId, this.isDebug);\r\n SITE.trackEvent(category, action, label, value);\r\n });\r\n }\r\n\r\n}\r\n\r\nexport default PushBAIDU;","/** \r\n * 获取Vue的版本\r\n * @return 2=Vue2.x, 3=Vue3.x\r\n */\r\nconst getVueVersion = (Vue: Vue): number => {\r\n let version: number = 2;\r\n\r\n // 获取Vue的版本号\r\n const VUE_VERSION: string = String(Vue.version);\r\n\r\n // Vue 2.x\r\n if ( VUE_VERSION.slice(0, 2) === '2.' ) {\r\n version = 2; \r\n }\r\n \r\n // Vue 3.x\r\n if ( VUE_VERSION.slice(0, 2) === '3.' ) {\r\n version = 3; \r\n }\r\n \r\n return version;\r\n}\r\n\r\nexport default getVueVersion;","import PushBAIDU from '@m/pushBAIDU'\r\nimport getVueVersion from '@m/getVueVersion'\r\n\r\n/** \r\n * 定义插件\r\n */\r\nexport default function install (Vue: Vue, { router, siteIdList, isDebug = false }: Partial<Options>) {\r\n\r\n /** \r\n * 一些环境和参数的检查\r\n */\r\n if ( typeof document === 'undefined' || typeof window === 'undefined' ) {\r\n return false;\r\n }\r\n\r\n if ( !router ) {\r\n throw new Error('[vue-baidu-analytics] Must pass a Vue-Router instance to vue-baidu-analytics.');\r\n }\r\n\r\n if ( !siteIdList ) {\r\n throw new Error('[vue-baidu-analytics] Missing tracking domain ID, add at least one of baidu analytics.');\r\n }\r\n\r\n /** \r\n * 挂载推送的方法\r\n */\r\n const pushBAIDU: any = new PushBAIDU(siteIdList, isDebug);\r\n \r\n // 获取Vue版本(获取失败则默认为2)\r\n const VUE_VERSION: number = getVueVersion(Vue) || 2;\r\n\r\n // 2.x可以直接挂载到原型上\r\n if ( VUE_VERSION === 2 ) {\r\n Vue.prototype.$pushBAIDU = pushBAIDU;\r\n }\r\n\r\n // 3.x必须使用这个方式来挂载\r\n if ( VUE_VERSION === 3 ) {\r\n Vue.config.globalProperties.$pushBAIDU = pushBAIDU;\r\n }\r\n\r\n /** \r\n * 部署站点并初始化\r\n */\r\n if ( siteIdList ) {\r\n pushBAIDU.init();\r\n }\r\n\r\n /** \r\n * 路由切换时执行PV上报\r\n */\r\n router.afterEach( (to: To) => {\r\n const PAGE_PATH_DIR_COUNT = window.location.pathname.split('/').length;\r\n const PAGE_PATH = window.location.pathname.split('/').slice(0, PAGE_PATH_DIR_COUNT - 1).join('/');\r\n const PAGE_URL = router.mode === 'hash' ? `${PAGE_PATH}/#${to.fullPath}` : `${PAGE_PATH}${to.fullPath}`;\r\n\r\n pushBAIDU.pv(PAGE_URL);\r\n });\r\n}"],"names":[],"mappings":";;;;;;;;;;;EAIA;MAIE,eAAa,MAAmB,EAAE,OAAwB;UAA7C,uBAAA,EAAA,WAAmB;UAAE,wBAAA,EAAA,eAAwB;UACxD,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;UACrB,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;OACxB;MAKD,oBAAI,GAAJ;UACE,MAAM,CAAC,IAAI,GAAG,MAAM,CAAC,IAAI,GAAG,MAAM,CAAC,IAAI,GAAG,EAAE,CAAC;UAC7C,IAAM,MAAM,GAAG,QAAQ,CAAC,aAAa,CAAC,QAAQ,CAAC,CAAC;UAChD,MAAM,CAAC,OAAO,CAAC,GAAG,IAAI,CAAC;UACvB,MAAM,CAAC,KAAK,CAAC,GAAG,gCAA8B,IAAI,CAAC,MAAQ,CAAC;UAC5D,QAAQ,CAAC,aAAa,CAAC,MAAM,CAAC,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC;UAEnD,IAAK,IAAI,CAAC,OAAO,EAAG;cAClB,OAAO,CAAC,GAAG,CAAC,yDAAuD,IAAI,CAAC,MAAQ,CAAC,CAAC;WACnF;OACF;MAKD,0BAAU,GAAV;UACE,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,aAAa,EAAE,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC;OAChD;MAKD,6BAAa,GAAb,UAAe,OAAe;UAE5B,IAAK,CAAC,OAAO,IAAI,OAAO,OAAO,KAAK,QAAQ,EAAG;cAC7C,OAAO,GAAG,GAAG,CAAC;WACf;UAGD,IAAK,OAAO,CAAC,QAAQ,CAA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