前言

Google Analytics 是一个强大的数字分析工具,用于跟踪和分析网站和应用程序的流量和用户行为。

注册

upload successful

upload successful

upload successful

upload successful

upload successful

upload successful

upload successful

upload successful

upload successful

upload successful

upload successful

upload successful

upload successful

如何增加自定义事件来追踪统计

  • 以下面这个为例

upload successful
我们通过事件委托来给所有的a标签绑定点击事件
在点击事件中我们通过以下代码来记录事件,并传递参数

1
2
3
4
gtag('event', 'click_news', {
'news_id': 'news_id',
'category': 'category'
});

gtag 是 Google Analytics (GA) 提供的全局网站标签,用于跟踪和报告用户的互动行为。通过调用 gtag 函数,可以发送各种类型的事件和数据到 Google Analytics。

1
gtag('command', 'event_name', { parameter_key: 'parameter_value', ... });

参数解释:

upload successful

upload successful

upload successful

upload successful

示例代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
handleLinkClick(event) {
let targetElement = event.target;
while (targetElement && (targetElement !== document.getElementById('app'))) {
if (targetElement.tagName === 'A') {
gtag('event', 'click_news', {
'news_id': 'news_id',
'category': 'category'
});
break
}
targetElement = targetElement.parentElement;
}
},