对于单页面数据统计上报,将代码放到index.html 文件中只有首页才会加载一次,也就是只能上报首页的。
解决方案:放到路由afterEach

var _paq = window._paq = window._paq || [];
//   当前页面path
  _paq.push(['setCustomUrl', window.location.origin + to.fullPath]);
  if (from && from.fullPath) {
//    form  path   
    _paq.push(['setReferrerUrl', window.location.origin + (from && from.fullPath)]);
  }
  _paq.push(['trackPageView']);
  // _paq.push(['enableLinkTracking']);
  // _paq.push(['alwaysUseSendBeacon']);
  _paq.push(['setTrackerUrl', "\/\/xxx\/wp-content\/plugins\/matomo\/app\/matomo.php"]);
  _paq.push(['setSiteId', '1']);
  if (!createMatomoScript) {
    var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
    g.type='text/javascript';
    g.async=true;
    g.src="\/\/xxx\/wp-content\/uploads\/matomo\/matomo.js";
    s.parentNode.insertBefore(g,s);
    createMatomoScript = g
  }

google的可以使用vue-gtag插件
https://matteo-gabriele.gitbook.io/vue-gtag/master/auto-tracking
main.js

import VueGtag from "vue-gtag";
Vue.use(VueGtag, {
    config: { id: "G-xx" },
    includes: [
      { id: 'G-xx' },
    ]
  }, router);

自动检测路由变化后会统计上报

如果是非单页面直接在HTML文件中写入script相关代码

<script async src="https://www.googletagmanager.com/gtag/js?id=G-xxx"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'G-xxx');
</script>

菜鸟小N
32 声望1 粉丝