React,react-router single-page website, cnzz/ Youmai statistics how to do?

current practice:

put the statistical code directly in index.html;
problem: cannot count the url; during route hopping

<script type="text/javascript">var cnzz_protocol = (("https:" == document.location.protocol) ? " https://" : " http://");document.write(unescape("%3Cspan style="display:none;" id="cnzz_stat_icon_1258575107"%3E%3C/span%3E%3Cscript src="" + cnzz_protocol + "s5.cnzz.com/stat.php%3Fid%3D1258575107" type="text/javascript"%3E%3C/script%3E"));</script>

vue

find a code writing method for cnzz statistics of vue on the Internet. How to write the react project?

<template>  
  <router-view></router-view>  
</template>  
  
<script>  
export default {  
  name: "app",  
  mounted () {  
    const script = document.createElement("script")  
    script.src = "https://s95.cnzz.com/z_stat.php?id=1111111111&web_id=1111111111"  
    script.language = "JavaScript"  
    document.body.appendChild(script)  
  },  
  watch: {  
    "$route" () {  
      if (window._czc) {  
        let location = window.location  
        let contentUrl = location.pathname + location.hash  
        let refererUrl = "/"  
        window._czc.push(["_trackPageview", contentUrl, refererUrl])  
      }  
    }  
  }  
}  
</script>  
Mar.02,2021

I don't know if there are hook functions, ha, there are several stupid ways

one is to write separately in the componentDidMount of each page container, which is also possible

.

the other is that the components that encapsulate the routing, such as link, router.push redirect, and other methods or components, can all be encapsulated by themselves, insert them in the form of a parameter called jump link, and then use the friendly alliance method

.

another way is that you can package it with high-level components and execute the methods in high-level components every time to url push the current one. This is similar to the first one


is it really necessary to consider the jumps under each url? This is not caused by duplicate statistics, a user opens a website to produce a statistical calculation is not normal? On the contrary, when a non-single page application tries to make a page jump, it is calculated only once


import {getMaidianTrendsBi} from "./utils/maidianBi";
const _hmt = window._hmt || []

  componentDidMount() {
  getMaidianTrendsBi();
   history.listen((location, action) => {
        _hmt.push(['_trackPageview', location.pathname]);
        getMaidianTrendsBi();
      })
  }
.
Menu