I'm a complete beginner with Google Analytics, and I need to know how to set it up so that it can track the number of clicks a link on my website gets?
我是Google Analytics的初学者,我需要知道如何设置它以便它可以跟踪我网站上的链接获得的点击次数?
For example I have a link:
例如,我有一个链接:
<a href="https://google.com">link</a>
I know I'm supposed to put an onClick
event on there somewhere but I don't have any idea how it links to Google Analytics?
我知道我应该在那里放一个onClick事件,但我不知道它是如何链接到Google Analytics的?
Is this the correct Onclick code:
这是正确的Onclick代码:
onClick="_gaq.push(['_trackEvent', 'Link', 'Click', 'Banner Advert1']);"
5 个解决方案
#1
37
You probably want to use event tracking - this is a simple Javascript function to can fire from the click event on your links. You will need to make sure you have the standard google tracking script on your page too.
您可能希望使用事件跟踪 - 这是一个简单的Javascript函数,可以从链接上的click事件触发。您还需要确保页面上还有标准的Google跟踪脚本。
From Google Event Tracking Guide
来自Google事件跟踪指南
Event Tracking is a method available in the ga.js tracking code that you can use to record user interaction with website elements, such as a Flash-driven menu system. This is accomplished by attaching the method call to the particular UI element you want to track. When used this way, all user activity on such elements is calculated and displayed as Events in the Analytics reporting interface. Additionally, pageview calculations are unaffected by user activity tracked using the Event Tracking method. Finally, Event Tracking employs an object-oriented model that you can use to collect and classify different types of interaction with your web page objects.
事件跟踪是ga.js跟踪代码中可用的方法,您可以使用该方法记录用户与网站元素的交互,例如Flash驱动的菜单系统。这是通过将方法调用附加到要跟踪的特定UI元素来完成的。以这种方式使用时,将计算此类元素上的所有用户活动,并在Analytics报告界面中显示为事件。此外,网页浏览计算不受使用事件跟踪方法跟踪的用户活动的影响。最后,事件跟踪采用面向对象的模型,您可以使用它来收集和分类与您的网页对象的不同类型的交互。
Example:
<a href="www.google.com" onclick="_gaq.push(['_trackEvent', 'Google Link', 'Action label', 'Additional info']);">link</a>
UPDATE
The above is for the older version of the API - ga.js. If you are using the newer Universal tracking please refer to the docs. Effectively the data passed is the same as before, however the call is different.
以上是针对旧版本的API - ga.js.如果您使用的是较新的通用跟踪,请参阅文档。有效地传递的数据与之前相同,但是呼叫是不同的。
Example for event tracking using the newer API:
使用较新的API进行事件跟踪的示例:
<a href="www.google.com" onclick="ga('send', 'event', 'Google Link', 'Action label', 'Action Value');">link</a>
#2
28
Please note that _gaq.push(..)
refers to tracking with the legacy Classic Analytics Web Tracking (ga.js). The new standard Universal Analytics Web Tracking (analytics.js) uses a different methodology like:
请注意,_gaq.push(..)是指使用旧版经典分析网络跟踪(ga.js)进行跟踪。新标准Universal Analytics Web Tracking(analytics.js)使用不同的方法,例如:
ga('send', 'event', 'button', 'click', 'nav buttons', 4);
ga('发送','事件','按钮','点击','导航按钮',4);
The first two options cannot be changed, they pass the send
option with the hit type event
to the ga
function .
前两个选项无法更改,它们将带有匹配类型事件的发送选项传递给ga函数。
The next two options are required as well, the last two are optional. They represent:
接下来的两个选项也是必需的,后两个选项是可选的。他们代表:
-
button
(string | required) : Category -
click
(string | required) : Action -
nav buttons
(string | not required) : Label -
4
(Positive Integer | not required) : Value
按钮(字符串|必填):类别
click(string | required):动作
导航按钮(字符串|不是必需的):标签
4(正整数|不需要):值
More information may be found at : https://developers.google.com/analytics/devguides/collection/analyticsjs/events
有关详细信息,请访问:https://developers.google.com/analytics/devguides/collection/analyticsjs/events
#3
7
I use this in the footer of every page setup as an event in Google Goals.
我在每个页面设置的页脚中使用它作为Google Goals中的事件。
Swap out register with the slug for the page path before the success page.
在成功页面之前使用slug交换寄存器以获取页面路径。
<script>
window.addEventListener('load',function(){
if(window.location.pathname =="/register/" )
{
ga('send','event','register page','referrer',document.referrer)
}
});
</script>
Then this in Admin > Goals
然后在管理员>目标中
- Custom
- Goal Description
- Goal details
This enables you to track which page URL the linked was clicked on if it resulted in a successful goal completion.
这使您可以跟踪链接被点击的页面URL,如果它导致目标完成成功。
Go to Behavior > Events > Overviews for reporting data.
转到行为>事件>概述以获取报告数据。
#4
5
The other answers don't take into consideration that the request may not get completed before the page changes, causing the event not to be recorded.
其他答案没有考虑到在页面更改之前请求可能无法完成,导致事件不被记录。
That's the problem with this solution found in other answers:
这是其他答案中找到的解决方案的问题:
<a
href="http://example.com"
onclick="ga('send', 'event', {eventAction: 'click', eventCategory: 'Example'})"
>example</a>
Google Analytics documentation does provide a solution to this:
Google Analytics文档确实为此提供了解决方案:
Tracking outbound links and forms can be tricky because most browsers will stop executing JavaScript on the current page once a new page starts to load. One solution to this problem is to set the
transport
field tobeacon
.跟踪出站链接和表单可能很棘手,因为一旦新页面开始加载,大多数浏览器将停止在当前页面上执行JavaScript。该问题的一个解决方案是将传输字段设置为信标。
Add transport: 'beacon'
, like this:
添加运输:'beacon',如下所示:
<a
href="http://example.com"
onclick="ga('send', 'event', {transport: 'beacon', eventAction: 'click', eventCategory: 'Example'})"
>example</a>
Unfortunately, some older browsers don't support beacon. To work around this, you could cancel the page navigation, send the request to Google Analytics, wait for its completion, and then launch the page navigation. Fortunately, all the modern major browsers do support it.
不幸的是,一些旧的浏览器不支持信标。要解决此问题,您可以取消页面导航,将请求发送到Google Analytics,等待其完成,然后启动页面导航。幸运的是,所有现代主流浏览器都支持它。
#5
2
I see that the other answers are referring to the old syntax: ga()
. Which is not gonna work if you are using the newest version of Google Analytics tracking...
我看到其他答案都是指旧语法:ga()。如果您使用的是最新版本的Google Analytics跟踪功能,那将无法运行...
Here is an example for event tracking using the newest GA version gtag()
:
以下是使用最新GA版本gtag()进行事件跟踪的示例:
<a href="http://example.com" onclick="gtag('event', 'click', {'event_category': 'Navbar button', 'event_label': 'Navbar blue Download button'});">Download</a>
So this is our onclick attribute content:
所以这是我们的onclick属性内容:
gtag('event', 'click', {'event_category': 'Navbar button', 'event_label': 'Navbar blue Download button'});
The above script has the following options:
上面的脚本有以下选项:
- Hit Type (
Event
| Required): Cannot be changed - Event Action (Text | Required): The type of interaction (e.g. '
click
') - event_Category (Text | Required): Typically the object that was interacted with (e.g. '
Navbar button
') - event_label (Text | Optional): Useful for categorizing events (e.g. '
Navbar blue Download button
')
命中类型(事件|必需):无法更改
事件操作(文本|必填):交互类型(例如“点击”)
event_Category(Text |必需):通常与之交互的对象(例如“导航栏按钮”)
event_label(文本|可选):用于对事件进行分类(例如“Navbar blue Download button”)
More details: https://developers.google.com/analytics/devguides/collection/gtagjs/migration#track_events
更多详细信息:https://developers.google.com/analytics/devguides/collection/gtagjs/migration#track_events
#1
37
You probably want to use event tracking - this is a simple Javascript function to can fire from the click event on your links. You will need to make sure you have the standard google tracking script on your page too.
您可能希望使用事件跟踪 - 这是一个简单的Javascript函数,可以从链接上的click事件触发。您还需要确保页面上还有标准的Google跟踪脚本。
From Google Event Tracking Guide
来自Google事件跟踪指南
Event Tracking is a method available in the ga.js tracking code that you can use to record user interaction with website elements, such as a Flash-driven menu system. This is accomplished by attaching the method call to the particular UI element you want to track. When used this way, all user activity on such elements is calculated and displayed as Events in the Analytics reporting interface. Additionally, pageview calculations are unaffected by user activity tracked using the Event Tracking method. Finally, Event Tracking employs an object-oriented model that you can use to collect and classify different types of interaction with your web page objects.
事件跟踪是ga.js跟踪代码中可用的方法,您可以使用该方法记录用户与网站元素的交互,例如Flash驱动的菜单系统。这是通过将方法调用附加到要跟踪的特定UI元素来完成的。以这种方式使用时,将计算此类元素上的所有用户活动,并在Analytics报告界面中显示为事件。此外,网页浏览计算不受使用事件跟踪方法跟踪的用户活动的影响。最后,事件跟踪采用面向对象的模型,您可以使用它来收集和分类与您的网页对象的不同类型的交互。
Example:
<a href="www.google.com" onclick="_gaq.push(['_trackEvent', 'Google Link', 'Action label', 'Additional info']);">link</a>
UPDATE
The above is for the older version of the API - ga.js. If you are using the newer Universal tracking please refer to the docs. Effectively the data passed is the same as before, however the call is different.
以上是针对旧版本的API - ga.js.如果您使用的是较新的通用跟踪,请参阅文档。有效地传递的数据与之前相同,但是呼叫是不同的。
Example for event tracking using the newer API:
使用较新的API进行事件跟踪的示例:
<a href="www.google.com" onclick="ga('send', 'event', 'Google Link', 'Action label', 'Action Value');">link</a>
#2
28
Please note that _gaq.push(..)
refers to tracking with the legacy Classic Analytics Web Tracking (ga.js). The new standard Universal Analytics Web Tracking (analytics.js) uses a different methodology like:
请注意,_gaq.push(..)是指使用旧版经典分析网络跟踪(ga.js)进行跟踪。新标准Universal Analytics Web Tracking(analytics.js)使用不同的方法,例如:
ga('send', 'event', 'button', 'click', 'nav buttons', 4);
ga('发送','事件','按钮','点击','导航按钮',4);
The first two options cannot be changed, they pass the send
option with the hit type event
to the ga
function .
前两个选项无法更改,它们将带有匹配类型事件的发送选项传递给ga函数。
The next two options are required as well, the last two are optional. They represent:
接下来的两个选项也是必需的,后两个选项是可选的。他们代表:
-
button
(string | required) : Category -
click
(string | required) : Action -
nav buttons
(string | not required) : Label -
4
(Positive Integer | not required) : Value
按钮(字符串|必填):类别
click(string | required):动作
导航按钮(字符串|不是必需的):标签
4(正整数|不需要):值
More information may be found at : https://developers.google.com/analytics/devguides/collection/analyticsjs/events
有关详细信息,请访问:https://developers.google.com/analytics/devguides/collection/analyticsjs/events
#3
7
I use this in the footer of every page setup as an event in Google Goals.
我在每个页面设置的页脚中使用它作为Google Goals中的事件。
Swap out register with the slug for the page path before the success page.
在成功页面之前使用slug交换寄存器以获取页面路径。
<script>
window.addEventListener('load',function(){
if(window.location.pathname =="/register/" )
{
ga('send','event','register page','referrer',document.referrer)
}
});
</script>
Then this in Admin > Goals
然后在管理员>目标中
- Custom
- Goal Description
- Goal details
This enables you to track which page URL the linked was clicked on if it resulted in a successful goal completion.
这使您可以跟踪链接被点击的页面URL,如果它导致目标完成成功。
Go to Behavior > Events > Overviews for reporting data.
转到行为>事件>概述以获取报告数据。
#4
5
The other answers don't take into consideration that the request may not get completed before the page changes, causing the event not to be recorded.
其他答案没有考虑到在页面更改之前请求可能无法完成,导致事件不被记录。
That's the problem with this solution found in other answers:
这是其他答案中找到的解决方案的问题:
<a
href="http://example.com"
onclick="ga('send', 'event', {eventAction: 'click', eventCategory: 'Example'})"
>example</a>
Google Analytics documentation does provide a solution to this:
Google Analytics文档确实为此提供了解决方案:
Tracking outbound links and forms can be tricky because most browsers will stop executing JavaScript on the current page once a new page starts to load. One solution to this problem is to set the
transport
field tobeacon
.跟踪出站链接和表单可能很棘手,因为一旦新页面开始加载,大多数浏览器将停止在当前页面上执行JavaScript。该问题的一个解决方案是将传输字段设置为信标。
Add transport: 'beacon'
, like this:
添加运输:'beacon',如下所示:
<a
href="http://example.com"
onclick="ga('send', 'event', {transport: 'beacon', eventAction: 'click', eventCategory: 'Example'})"
>example</a>
Unfortunately, some older browsers don't support beacon. To work around this, you could cancel the page navigation, send the request to Google Analytics, wait for its completion, and then launch the page navigation. Fortunately, all the modern major browsers do support it.
不幸的是,一些旧的浏览器不支持信标。要解决此问题,您可以取消页面导航,将请求发送到Google Analytics,等待其完成,然后启动页面导航。幸运的是,所有现代主流浏览器都支持它。
#5
2
I see that the other answers are referring to the old syntax: ga()
. Which is not gonna work if you are using the newest version of Google Analytics tracking...
我看到其他答案都是指旧语法:ga()。如果您使用的是最新版本的Google Analytics跟踪功能,那将无法运行...
Here is an example for event tracking using the newest GA version gtag()
:
以下是使用最新GA版本gtag()进行事件跟踪的示例:
<a href="http://example.com" onclick="gtag('event', 'click', {'event_category': 'Navbar button', 'event_label': 'Navbar blue Download button'});">Download</a>
So this is our onclick attribute content:
所以这是我们的onclick属性内容:
gtag('event', 'click', {'event_category': 'Navbar button', 'event_label': 'Navbar blue Download button'});
The above script has the following options:
上面的脚本有以下选项:
- Hit Type (
Event
| Required): Cannot be changed - Event Action (Text | Required): The type of interaction (e.g. '
click
') - event_Category (Text | Required): Typically the object that was interacted with (e.g. '
Navbar button
') - event_label (Text | Optional): Useful for categorizing events (e.g. '
Navbar blue Download button
')
命中类型(事件|必需):无法更改
事件操作(文本|必填):交互类型(例如“点击”)
event_Category(Text |必需):通常与之交互的对象(例如“导航栏按钮”)
event_label(文本|可选):用于对事件进行分类(例如“Navbar blue Download button”)
More details: https://developers.google.com/analytics/devguides/collection/gtagjs/migration#track_events
更多详细信息:https://developers.google.com/analytics/devguides/collection/gtagjs/migration#track_events