Vue uses Baidu statistics to bury points

Statistics code installation

//main.js
声明_hmt对象
var _hmt = _hmt || [];
window._hmt = _hmt; // 必须把_hmt挂载到window下,否则找不到

//统计代码获取
(function() {
      var hm = document.createElement("script");
      hm.src ="https://hm.baidu.com/hm.js?百度统计所申请的appKey";   
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
 })();

Page burying point

trackPageView (used to send PV statistics of a specified URL)
Syntax: _hmt.push(['_trackPageview', pageURL]);
Parameters: pageURL: Specify the page URL to count PV (must start with "/" (slash) Relative path at the beginning)
Example: _hmt.push(['_trackPageview', '/virtual/login']);

//监听路由下所有页面
router.beforeEach((to, from, next) => {
  // next()
  if (_hmt) {
    if (to.path) {
        _hmt.push(['_trackPageview', '/#' + to.path]);
    }
  }

})

incident point

trackEvent (used to trigger an event, such as the click of a button, the play/stop of the player, the start/pause of the game, etc.)
Syntax: _hmt.push(['_trackEvent', category, action, opt_label, opt_value ]);
Parameters:
category: Required String => Type name of the target to be monitored => Events that are left blank or filled with "-" will be discarded
action: Required String => Action name for users to interact with the web page => Events that are not filled in or filled with "-" will be discarded
opt_label: Optional String => Some additional information about the event => If not filled in or filled with "-", it means that this item is empty
opt_value: Optional Number => Value related to the event

example:

//template
<input placeholder="请输入搜索内容" v-model="searchValue" class="input-with-select" style="width:612px;">
<button @click="searchFn"> 搜索 <button/>
js

data(){
	return {
		searchValue:''
	}
}

methods:{
	searchFn(){
		_hmt.push(['_trackEvent', 'search', 'click',  this.searchValue]);
	}
}


The data after the point burying is completed needs to be logged in to Baidu Statistics to view Baidu Statistics in Basic Report => Access Analysis

Attachment: Baidu statistical document
Insert image description here

Guess you like

Origin blog.csdn.net/weixin_42215897/article/details/110933693