我正在从https://github.com/nuxt-community/google-gtag将gtag用于nuxt。我已经正确地将它包含在nuxt.config.js的modules
中,并且文档说:
此模块在您的NuxtJs项目中包含Google gtag,默认情况下启用每个页面跟踪。您可以在组件/函数/方法内部使用gtag,如下所示:
this.$gtag('event', 'your_event', { /* track something awesome */})
我正在跟踪我的下载按钮:
<button class="download_button" @click="
this.$gtag('event', 'DownloadButton', {
event_category: 'Article',
event_label: 'Product name',
})
"
>
Download Now
</button>
单击按钮时收到此错误:
TypeError: Cannot read property '$gtag' of null at click
和Property '$gtag' does not exist on type 'ArticleContent'
,当我在脚本标签中使用它时。
导入该模块时我丢失了什么东西吗?我真的很感谢您的帮助。
如果在模板中使用this
,请尝试将其删除:
<button
class="download_button" @click="$gtag('event', 'DownloadButton', {
event_category: 'Article',
event_label: 'Product name',
})
"
>
Download Now
</button>