vue作為單頁面的,引入百度統計,需要注意不少。
一、基本的流量統計
在index.html 入口文件中引入百度統計生成的一連串代碼:
var _hmt = _hmt || []; (function() { var hm = document.createElement('script'); hm.src = 'https://hm.baidu.com/hm.js?f62fe3c5d9343dece386407e99be4e39'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(hm, s); })();
這個會統計頁面總的流量。
二、頁面中監聽事件
需要在相應的vue 文件中加入相應的代碼
methods: { getDownIosUrl() { window._hmt.push(['_trackEvent', 'IosDoctor', 'click', '來康醫生IOS版', 'opt_value']); window.location.href = 'itms-apps://itunes.apple.com/cn/app/id1233649213?mt=8'; }, }
PS:必須要寫window._hmt.push 否則會監聽不到
三、單個頁面流量的檢測
在路由的配置文件中,加入這行代碼。
// 測試百度統計 routeInstance.beforeEach((to, from, next) => { if (to.path) { window._hmt.push(['_trackPageview', '/pro/#' + to.fullPath]); } next(); })
'/pro/#' 寫相對路徑,解決vue 路由 hash 模式下,百度統計無法統計單頁面的問題。
四、廣告途徑的追蹤
可以根據轉化率來確定下載次數。在轉化設置中,設置事件轉化中的trackevent。
然后通過“報告中的入口頁面中的轉化分析”查看數據。

