鍍金池/ 問(wèn)答/HTML/ react,react-router單頁(yè)面網(wǎng)站,cnzz/友盟統(tǒng)計(jì)怎么做?

react,react-router單頁(yè)面網(wǎng)站,cnzz/友盟統(tǒng)計(jì)怎么做?

目前的做法:

直接把統(tǒng)計(jì)代碼放在index.html里面;
問(wèn)題:不能統(tǒng)計(jì)到路由跳轉(zhuǎn)過(guò)程中的url;

<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.#/stat.php%3Fid%3D1258575107' type='text/javascript'%3E%3C/script%3E"));</script>

vue的寫(xiě)法

網(wǎng)上找到一個(gè)關(guān)于vue的cnzz統(tǒng)計(jì)的代碼寫(xiě)法,求react項(xiàng)目怎么寫(xiě)比較好?

<template>  
  <router-view></router-view>  
</template>  
  
<script>  
export default {  
  name: 'app',  
  mounted () {  
    const script = document.createElement('script')  
    script.src = 'https://s95.#/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>  
回答
編輯回答
選擇

我不知道有沒(méi)有鉤子函數(shù)哈,有幾種笨辦法

一種是,在每個(gè)頁(yè)面容器的componentDidMount里面單獨(dú)寫(xiě)一下,這也可以吧

另一種是,再封裝一下路由的那部分組件,比如 link啊 router.push redirect啊這類方法或組件,都可以自己封裝下,吧跳轉(zhuǎn)鏈接一參數(shù)形式穿進(jìn)去,再用友盟的方法

還有一種就是可以用高階組件包一下,每次都執(zhí)行下高階組件里面的方法,用來(lái)把當(dāng)前的url push一下,這個(gè)和第一種差不多吧

2017年6月29日 13:36