{"version":3,"file":"vue-baidu-analytics.min.js","sources":["../src/modules/baidu.ts","../src/modules/pushBAIDU.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;","import PushBAIDU from '@m/pushBAIDU'\r\n\r\nexport default function install (Vue: Vue, { router, siteIdList, isDebug = false }: Partial) {\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 = new PushBAIDU(siteIdList, isDebug);\r\n Vue.prototype.$pushBAIDU = pushBAIDU;\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}\r\n"],"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","prototype","$pushBAIDU","afterEach","to","PAGE_PATH_DIR_COUNT","location","pathname","length","PAGE_PATH","slice","join","PAGE_URL","mode","fullPath","pv"],"mappings":";;;;;gPAIA,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,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