Add Baidu statistics code to a single-page vue application (for test purposes) and vue code

Source: Internet
Author: User

Add Baidu statistics code to a single-page vue application (for test purposes) and vue code

In this way, the ingress file is inserted globally, and each page is available. This involves a problem, Vue. the js Project is a single-page application. Every time a user browses a website, the page will not be refreshed when accessing the inner page, which means that Baidu Statistics Code will not be triggered; therefore, the result displayed in the Baidu statistics background is that only the traffic at the webpage portal is counted, but the access traffic on the inner page is not counted.

Solution

In main. call the afterEach method of vue-router in the js file and add the statistical code to this method. In this way, the statistical code is executed every time the router changes, the Code is as follows:

Router. afterEach (to, from, next) =>{ setTimeout () =>{ var _ hmt = _ hmt | []; (function () {// remove the last inserted code document before each execution. getElementById ('Baidu _ tj ') & document. getElementById ('Baidu _ tj '). remove (); var hm = document. createElement ("script"); hm. src = "https://hm.baidu.com/hm.js? Xxxx "; hm. id = "baidu_tj" var s = document. getElementsByTagName ("script") [0]; s. parentNode. insertBefore (hm, s) ;}) () ;}, 0 );});

Summary

The above is a small version of the vue single-page application plus Baidu statistics code, I hope to help you, if you have any questions, please leave a message, the small series will reply to you in a timely manner. Thank you very much for your support for the help House website!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.