On the Internet can not find a variety of VUE project to join baidu statistics code and implementation:

I explored a set of methods to add Baidu statistics, first of all to make clear, because Vue is a single page application, so directly add Baidu statistics code to the header of index.html is not useful, the page is only loaded once.

Let’s take a look at the official API of Baidu statistics, _trackPageview

Used to send PV statistics requests for a specified URL, typically used for PV statistics for AJAX pages.

grammar

_hmt.push([‘_trackPageview’, pageURL]);

For example,

_hmt.push([‘_trackPageview’, ‘/virtual/login’]);

parameter

The name of the Mandatory/Optional type function note
pageURL Will choose String Specify the URL of the page for which PV statistics are to be collected The relative path must begin with a slash (/)

At this time we can through the vue-router vue every change, to the virtual URL directly to baidu statistics public API interface, let them to deal with their own

Inside the main function, do something like this:

[javascript]
view plain
copy
print
?

  1. router.beforeEach((to, from, next) => {  
  2. // Statistics code
  3.   if (to.path) {  
  4.     _hmt.push([‘_trackPageview’, ‘/#’ + to.fullPath]);  
  5.   }  
  6.   next();  
  7. });  
Route.beforeeach ((to, from, Next) => {// Statistics code if (to.path) {_hmt. Push (['_trackPageview', '/#' + to.fullPath]); } next(); });Copy the code