如何在nuxt js中添加全局站点标签以及如何发送事件?

Jon*_*han 1 google-analytics nuxt.js

我像这样发送。如何在 nuxt js 中配置全局站点标签

methods: {
    track(){
        this.$gtag('event', 'aaa', {
            'event_category' : 'bbb',
            'event_label' : 'ccc'
        });
    }
},
Run Code Online (Sandbox Code Playgroud)

全局网站标记链接 https://developers.google.com/analytics/devguides/collection/gtagjs

Nic*_*wes 7

在您的插件文件夹中,添加以下内容google-analytics.client.js

将 ###TAG ID### 替换为您的标签。

export default () => {
  const script = document.createElement('script')
  script.src = 'https://www.googletagmanager.com/gtag/js?id=###TAG ID###'
  script.async = true
  document.getElementsByTagName('head')[0].appendChild(script)
  window.dataLayer = window.dataLayer || [];
  function gtag() {
    dataLayer.push(arguments);
  }
  gtag('js', new Date());
  gtag('config', '###TAG ID###');
  gtag('event', 'aaa');
  gtag('event_category', 'bbb');
  gtag('event_label', 'ccc');
});
Run Code Online (Sandbox Code Playgroud)

然后将其添加到您的nuxt.config.js. 将以下内容添加到您的插件中:

plugins: [
  { src: '~/plugins/google-analytics.client.js'},
],
Run Code Online (Sandbox Code Playgroud)