简体   繁体   English

使用谷歌分析跟踪链接点击次数?

[英]Tracking link clicks with google analytics?

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']);"

You probably want to use event tracking - this is a simple Javascript function to can fire from the click event on your links.您可能想要使用事件跟踪 - 这是一个简单的 Javascript 函数,可以从链接上的点击事件中触发。 You will need to make sure you have the standard google tracking script on your page too.您还需要确保您的页面上也有标准的谷歌跟踪脚本

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.事件跟踪是 ga.js 跟踪代码中提供的一种方法,可用于记录用户与网站元素(例如 Flash 驱动的菜单系统)的交互。 This is accomplished by attaching the method call to the particular UI element you want to track.这是通过将方法调用附加到您要跟踪的特定 UI 元素来实现的。 When used this way, all user activity on such elements is calculated and displayed as Events in the Analytics reporting interface.以这种方式使用时,此类元素上的所有用户活动都会计算并显示为 Analytics 报告界面中的事件。 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.最后,事件跟踪采用面向对象的模型,您可以使用它来收集和分类与网页对象的不同类型的交互。

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.以上是针对旧版本的 API - ga.js。 If you are using the newer Universal tracking please refer to the docs .如果您使用较新的Universal 跟踪,请参阅文档 Effectively the data passed is the same as before, however the call is different.有效地传递的数据与以前相同,但调用不同。

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>

Please note that _gaq.push(..) refers to tracking with the legacy Classic Analytics Web Tracking (ga.js) .请注意, _gaq.push(..)指的是使用旧版经典分析网络跟踪(ga.js) 进行跟踪 The new standard Universal Analytics Web Tracking (analytics.js) uses a different methodology like:新标准Universal Analytics Web Tracking (analytics.js)使用不同的方法,例如:

ga('send', 'event', 'button', 'click', 'nav buttons', 4);

The first two options cannot be changed, they pass the send option with the hit type event to the ga function .前两个选项不能更改,它们将带有命中类型eventsend选项传递send ga函数。

The next two options are required as well, the last two are optional.接下来的两个选项也是必需的,最后两个是可选的。 They represent:他们代表:

  • button (string | required) : Category button (字符串 | 必需) :类别
  • click (string | required) : Action click (字符串 | 必需) :操作
  • nav buttons (string | not required) : Label nav buttons (字符串 | 不需要) :标签
  • 4 (Positive Integer | not required) : Value 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

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.跟踪出站链接和表单可能很棘手,因为一旦新页面开始加载,大多数浏览器将停止在当前页面上执行 JavaScript。 One solution to this problem is to set the transport field to beacon此问题的一种解决方案是将transport字段设置为beacon

beacon transport allows data to be sent asynchronously to a server, even after a page was closed. beacon传输允许将数据异步发送到服务器,即使在页面关闭后也是如此。

Add transport: 'beacon' , like this:添加transport: 'beacon' ,像这样:

 <a
    href="http://example.com"
    onclick="ga('send', 'event', {transport: 'beacon', eventAction: 'click', eventCategory: 'Example'})"
 >example</a>

Unfortunately, some browsers don't support beacon, including IE 11 (caniuse) .不幸的是,一些浏览器不支持信标,包括 IE 11 (caniuse) 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.要解决此问题,您可以取消页面导航,将请求发送到 Google Analytics,等待其完成,然后启动页面导航。 Fortunately, all the modern major browsers do support it.幸运的是,所有现代主流浏览器都支持它。

I use this in the footer of every page setup as an event in Google Goals.我在每个页面设置的页脚中使用它作为 Google 目标中的一个事件。

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然后在管理>目标中

  1. Custom风俗
  2. Goal Description目标描述

在此处输入图片说明

  1. 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.转到“行为”>“事件”>“概述”以报告数据。

I see that the other answers are referring to the old function: ga() .我看到其他答案指的是旧函数: ga() Which is not gonna work if you are using the newest version of Google Analytics tracking...如果您使用最新版本的 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>

The example above has the following options:上面的例子有以下选项:

  • Hit Type ( Event | Required): Cannot be changed命中类型Event | 必需):无法更改
  • Event Action (Text | Required): The type of interaction (eg ' click ')事件操作(文本 | 必需):交互类型(例如“ click ”)
  • event_Category (Text | Required): Typically the object that was interacted with (eg ' Navbar button ') event_Category (文本 | 必需):通常是与之交互的对象(例如“ Navbar button ”)
  • event_label (Text | Optional): Useful for categorizing events (eg ' Navbar blue Download button ') event_label (Text | Optional):用于分类事件(例如“导航栏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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM