mirror of
https://github.com/analyticsjs/vue-baidu-analytics.git
synced 2025-04-06 03:58:00 +08:00
1 line
9.6 KiB
Plaintext
1 line
9.6 KiB
Plaintext
{"version":3,"file":"vue-baidu-analytics.min.js","sources":["../src/modules/baidu.ts","../src/modules/pushBAIDU.ts","../src/main.ts","../src/modules/getVueVersion.ts","../src/modules/getRouterMode.ts"],"sourcesContent":["/** \n * 定义基础配置\n * 官方文档 https://tongji.baidu.com/open/api/more?p=guide_overview\n */\nclass BAIDU {\n siteId: string;\n isDebug: boolean;\n\n constructor (siteId: string = '', isDebug: boolean = false) {\n this.siteId = siteId;\n this.isDebug = isDebug;\n }\n\n /** \n * 初始化\n */\n init () {\n window._hmt = window._hmt ? window._hmt : [];\n const SCRIPT = document.createElement('script');\n SCRIPT['async'] = true;\n SCRIPT['src'] = `https://hm.baidu.com/hm.js?${this.siteId}`;\n document.querySelector('head').appendChild(SCRIPT);\n\n if ( this.isDebug ) {\n console.log(`[vue-baidu-analytics] siteId load done.\\nsiteId: ${this.siteId}`);\n }\n }\n\n /** \n * 设置要响应的站点\n */\n setAccount () {\n window._hmt.push(['_setAccount', this.siteId]);\n }\n\n /** \n * 提交PV、UV\n */\n trackPageview (pageUrl: string) {\n // 如果页面链接没传或者无效链接,则默认为根域名\n if ( !pageUrl || typeof pageUrl !== 'string' ) {\n pageUrl = '/';\n }\n\n // 如果页面链接带上了域名,则需要过滤掉\n if ( pageUrl.includes('http') ) {\n const PAGE_CUT = pageUrl.split('/');\n const HOST_NAME = `${PAGE_CUT[0]}//${PAGE_CUT[2]}`;\n pageUrl = pageUrl.replace(HOST_NAME, '');\n }\n\n // 设置响应id并提交数据\n this.setAccount();\n window._hmt.push(['_trackPageview', pageUrl]);\n\n if ( this.isDebug ) {\n console.log(`[vue-baidu-analytics] track pv done.\\nsiteId: ${this.siteId}\\npageUrl: ${pageUrl}`);\n }\n }\n\n /** \n * 提交点击事件\n */\n trackEvent (category: string, action: string, label: string, value: number) {\n // 前两个是必填项\n if ( typeof category !== 'string' || typeof action !== 'string' || !category || !action ) {\n throw new Error('[vue-baidu-analytics] Missing necessary category and operation information, and must be of type string.');\n return false;\n }\n\n // 重置一些无效的默认值\n if ( !label || typeof label !== 'string' ) {\n label = '';\n }\n\n if ( !Number(value) ) {\n value = 1;\n }\n\n // 设置响应id并提交数据\n this.setAccount();\n window._hmt.push(['_trackEvent', category, action, label, value]);\n\n if ( this.isDebug ) {\n console.log(`[vue-baidu-analytics] track event done.\\nsiteId: ${this.siteId}\\ncategory: ${category}\\naction: ${action}\\nlabel: ${label}\\nvalue: ${value}`);\n }\n }\n}\n\nexport default BAIDU;","import BAIDU from '@m/baidu'\n\n/** \n * 定义推送操作\n */\nclass PushBAIDU {\n siteIdList: string[];\n isDebug: boolean;\n\n constructor (siteIdList: string[], isDebug: boolean) {\n this.siteIdList = siteIdList;\n this.isDebug = isDebug;\n }\n\n /** \n * 批量部署站点\n */\n init () {\n this.siteIdList.forEach( (siteId: string) => {\n const SITE = new BAIDU(siteId, this.isDebug);\n SITE.init();\n });\n }\n\n /** \n * 批量提交pv上报\n */\n pv (pageUrl: string) {\n this.siteIdList.forEach( (siteId: string) => {\n const SITE = new BAIDU(siteId, this.isDebug);\n SITE.trackPageview(pageUrl);\n });\n }\n\n /** \n * 批量提交事件上报\n */\n event (category: string, action: string, label: string, value: number) {\n this.siteIdList.forEach( (siteId: string) => {\n const SITE = new BAIDU(siteId, this.isDebug);\n SITE.trackEvent(category, action, label, value);\n });\n }\n\n}\n\nexport default PushBAIDU;","import PushBAIDU from '@m/pushBAIDU'\nimport getVueVersion from '@m/getVueVersion'\nimport getRouterMode from '@m/getRouterMode'\n\n/** \n * 定义插件\n */\nexport default function install (Vue: Vue, { router, siteIdList, isDebug = false }: Partial<Options>) {\n\n /** \n * 一些环境和参数的检查\n */\n if ( typeof document === 'undefined' || typeof window === 'undefined' ) {\n return false;\n }\n\n if ( !router ) {\n throw new Error('[vue-baidu-analytics] Must pass a Vue-Router instance to vue-baidu-analytics.');\n }\n\n if ( !siteIdList ) {\n throw new Error('[vue-baidu-analytics] Missing tracking domain ID, add at least one of baidu analytics.');\n }\n\n /** \n * 挂载推送的方法\n */\n const pushBAIDU: any = new PushBAIDU(siteIdList, isDebug);\n \n // 获取Vue版本(获取失败则默认为2)\n const VUE_VERSION: number = getVueVersion(Vue) || 2;\n\n // 2.x可以直接挂载到原型上\n if ( VUE_VERSION === 2 ) {\n Vue.prototype.$pushBAIDU = pushBAIDU;\n }\n\n // 3.x必须使用这个方式来挂载\n if ( VUE_VERSION === 3 ) {\n Vue.config.globalProperties.$pushBAIDU = pushBAIDU;\n }\n\n /** \n * 部署站点并初始化\n */\n if ( siteIdList ) {\n pushBAIDU.init();\n }\n\n /** \n * 路由切换时执行PV上报\n */\n router.afterEach( (to: To) => {\n // 根据Vue版本获取路由模式\n const ROUTER_MODE: string = getRouterMode(VUE_VERSION, router);\n \n // 获取页面的url信息\n const PAGE_PATH_DIR_COUNT: number = window.location.pathname.split('/').length;\n const PAGE_PATH: string = window.location.pathname.split('/').slice(0, PAGE_PATH_DIR_COUNT - 1).join('/');\n\n // 根据路由模式生成要上报的链接\n const PAGE_URL: string = ROUTER_MODE === 'hash' ? `${PAGE_PATH}/#${to.fullPath}` : `${PAGE_PATH}${to.fullPath}`;\n \n // 上报数据\n pushBAIDU.pv(PAGE_URL);\n });\n}","/** \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;","/** \r\n * 获取Vue的版本\r\n * @param {number} vueVersion - vue版本号,2=Vue2.x, 3=Vue3.x\r\n * @param {object} router - vue路由\r\n * @return hash=hash模式、history=history模式\r\n */\r\nconst getRouterMode = (vueVersion: number, router: any): string => {\r\n let mode: string = 'history';\r\n\r\n // 2.x直接读取mode即可\r\n if ( vueVersion === 2 ) {\r\n mode = router.mode;\r\n }\r\n\r\n // 3.x需要判断一下\r\n if ( vueVersion === 3 ) {\r\n const BASE: string = router.options.history.base || '';\r\n if ( BASE.includes('#') ) {\r\n mode = 'hash';\r\n }\r\n }\r\n \r\n return mode;\r\n}\r\n\r\nexport default getRouterMode;"],"names":["siteId","isDebug","this","BAIDU","window","_hmt","SCRIPT","document","createElement","querySelector","appendChild","console","log","push","pageUrl","includes","PAGE_CUT","split","HOST_NAME","replace","setAccount","category","action","label","value","Error","Number","siteIdList","PushBAIDU","forEach","_this","init","trackPageview","trackEvent","Vue","_a","router","_b","pushBAIDU","VUE_VERSION","version","String","slice","getVueVersion","prototype","$pushBAIDU","config","globalProperties","afterEach","to","ROUTER_MODE","vueVersion","mode","options","history","base","getRouterMode","PAGE_PATH_DIR_COUNT","location","pathname","length","PAGE_PATH","join","PAGE_URL","fullPath","pv"],"mappings":";;;;;sMAIA,iBAIE,WAAaA,EAAqBC,gBAArBD,mBAAqBC,MAChCC,KAAKF,OAASA,EACdE,KAAKD,QAAUA,EA6EnB,OAvEEE,iBAAA,WACEC,OAAOC,KAAOD,OAAOC,KAAOD,OAAOC,KAAO,GAC1C,IAAMC,EAASC,SAASC,cAAc,UACtCF,EAAc,OAAI,EAClBA,EAAY,IAAI,8BAA8BJ,KAAKF,OACnDO,SAASE,cAAc,QAAQC,YAAYJ,GAEtCJ,KAAKD,SACRU,QAAQC,IAAI,uDAAuDV,KAAKF,SAO5EG,uBAAA,WACEC,OAAOC,KAAKQ,KAAK,CAAC,cAAeX,KAAKF,UAMxCG,0BAAA,SAAeW,GAOb,GALMA,GAA8B,iBAAZA,IACtBA,EAAU,KAIPA,EAAQC,SAAS,QAAU,CAC9B,IAAM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