谷歌分析 - 记录出站链接 - 打开新窗口

Google Analytics - Record Outbound Links - open new window

本文关键字:新窗口 窗口 链接 记录 谷歌      更新时间:2023-09-26

我使用以下链接通过谷歌分析记录出站链接。是否可以在新窗口中打开链接?

<script type="text/javascript">
function recordOutboundLink(link, category, action) 
{  
try {    
    var myTracker=_gat._getTrackerByName();    
    _gaq.push(['myTracker._trackEvent', category ,  action ]);    
    setTimeout('document.location = "' + link.href + '"', 100)  
 }
catch(err)
{
}
}
</script>
 <a href="http://www.example.com" onClick="recordOutboundLink(this, 'Outbound Links', 'example.com');return false;">

编辑,我忘了添加target="_blank":

我会这样做跟踪出站链接:

<a href="http://outgoinglink.com" target="_blank" onclick="_gaq.push(['_trackEvent','outgoing_links','outgoinglink.com'])">Link Text</a>

杰夫的回答非常接近为我解决这个问题。 谷歌推荐的代码打破了目标= _blank,window.open策略被弹出窗口阻止程序捕获。 我修改了目标的测试,如下所示:

if (!link.target || link.target.match(/^_(self|parent|top)$/i)){
    return false;
    setTimeout(function() { document.location = link.href }, 100);
} else {
    return true;
}

document.location = url;替换为 window.open(url) ,这将在新窗口中打开并同时进行跟踪。你不需要修改链接,只需在onclick上调用JS函数即可。

在一般情况下

在更一般的情况下,target是任何东西,比如

  • 空字符串 - <a>元素的默认值。 与_self 含义相同。
  • _blank - URL 加载到新窗口/选项卡中
  • _self - URL 替换当前页面。
  • _parent - URL 加载到父框架中
  • _top - URL 替换可能加载的任何框架集
  • 名称 - 窗口的名称

您可以修改行

setTimeout('document.location = "' + link.href + '"', 100)

对此:

setTimeout('window.open("' + link.href + '", link.target == "" ? "_self" : link.target)', 100)

奇怪的是,向window.open提供""与提供_blank相同,因此需要额外的?:运算符。

顺便说一下:您提供的代码(以及官方谷歌示例提供的代码)应稍作修改以正常工作,请参阅如何设置谷歌分析来跟踪出站链接?官方示例不起作用

这是您提供的链接中的修改代码段,它既适用于在当前窗口中打开的链接,也适用于在新窗口中打开的链接,无需任何修改:

<script type="text/javascript">
// Records an event for an outbound link.  Supports links even if they open in a
// different window (target="_blank").
// Don't forget to return the return value of this function from the onclick handler.
// link - A reference to the <a> anchor object to which this call is attached
// parameters - Parameters to pass to the event, in an array:
//              category/action/label/value/noninteraction or some subset; see GA docs.
//              Pass "undefined" (the value, not the string) for unspecified optional params if necessary.
// Exmaple:
// <a href="http://www.google.com"
//    onclick="return recordOutboundLink(this, ['category', 'action', 'label', 6.6, false);">link</a>
function recordOutboundLink(link, parameters) {
    try {
        _gaq.push(['_trackEvent'].concat(parameters));
        if (link.target == '_blank') {
            return true;
        } else {
            setTimeout(function() { document.location = link.href }, 100);
            return false;
        }
    } catch (err) {
        return true;
    }
}
</script>

以下是两种链接类型。 请注意,onclick 处理程序在两者中是相同的:

<a href="http://www.example.com"
   onClick="return recordOutboundLink(this, ['Outbound Links', 'example.com']);">
<a href="http://www.example.com"
   target="_blank"
   onClick="return recordOutboundLink(this, ['Outbound Links', 'example.com']);">

限制

  • 如果浏览器忽略"target="blank",而是在同一窗口中打开URL(例如Facebook iOS应用程序中的UIWebView),则链接可能无法被跟踪。 埃里克德公认的答案也是如此。 您可以添加代码来检测此类情况(例如,通过javascript检测ipad/iphone webview)。
  • 如果用户尝试按 ctrl-/shift-/cmd-单击(即在新选项卡或窗口中打开)没有"target="blank"的链接,则 URL 将在现有窗口中打开,而不是在新窗口中打开。 Roslyn的代码片段(以及Google的官方代码示例)也是如此。 同样,您可以添加代码来处理这些特殊情况。