mirror of
https://github.com/analyticsjs/vue-baidu-analytics.git
synced 2025-04-06 03:58:00 +08:00
1 line
11 KiB
Plaintext
1 line
11 KiB
Plaintext
{"version":3,"file":"vue-baidu-analytics.js","sources":["../src/modules/baidu.ts","../src/modules/pushBAIDU.ts","../src/modules/getVueVersion.ts","../src/modules/getRouterMode.ts","../src/main.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;","/** \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;","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 = window.location.pathname.split('/').length;\n const PAGE_PATH = window.location.pathname.split('/').slice(0, PAGE_PATH_DIR_COUNT - 1).join('/');\n\n // 根据路由模式生成要上报的链接\n const PAGE_URL = ROUTER_MODE === 'hash' ? `${PAGE_PATH}/#${to.fullPath}` : `${PAGE_PATH}${to.fullPath}`;\n \n // 上报数据\n pushBAIDU.pv(PAGE_URL);\n });\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,OAA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