Google Analytics - Record Outbound Links - open ne

2019-02-15 20:35发布

I have used the following link to record outbound links through google analytics. Is it possible to open the link in a new window?

<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;">

5条回答
Luminary・发光体
2楼-- · 2019-02-15 21:26

In the general case

In the more general case of target being anything, like

  • empty string - default for <a> elements. same meaning as _self.
  • _blank - URL is loaded into a new window/tab
  • _self - URL replaces the current page.
  • _parent - URL is loaded into the parent frame
  • _top - URL replaces any framesets that may be loaded
  • name - The name of the window

you can modify the line

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

to this:

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

The odd thing is that providing "" to window.open is the same as providing _blank, therefore the extra ?: operator is necessary.

By the way: Your provided code (and the code provided by official google examples) shall be modified slightly to work correct, see How do I set up Google Analytics to track outbound links? The official example doesn't work

查看更多
三岁会撩人
3楼-- · 2019-02-15 21:28

edit, I forgot to add target="_blank":

I would do it this way tracking outbound links:

<a href="http://outgoinglink.com" target="_blank" onclick="_gaq.push(['_trackEvent','outgoing_links','outgoinglink.com'])">Link Text</a>
查看更多
Animai°情兽
4楼-- · 2019-02-15 21:28

Replace the document.location = url; with window.open(url), that opens in a new window and tracks at the same time. You don't need to modify the link, just call the JS function on the onclick.

查看更多
\"骚年 ilove
5楼-- · 2019-02-15 21:39

Here's a modified snippet from the link you provided that should work both for links that open in the current window and links that open in a new window without any modification:

<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>

Here are the two link types. Note that the onclick handler is the same in both:

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

Limitations:

  • If the browser ignores the 'target="blank"' and instead opens the URL in the same window (e.g. a UIWebView inside the Facebook iOS app), then the link may not get tracked. This is also true of ErikdR's accepted answer. You can add code to detect cases like this (e.g. detect ipad/iphone webview via javascript).
  • If the user tries to ctrl-/shift-/cmd-click (i.e. open in a new tab or window) a link that doesn't have 'target="blank"', the URL will open in the existing window rather than opening in a new window. The same is true of Roslyn's code snippet (and Google's official code sample). Again, you can add code to handle these special cases.
查看更多
家丑人穷心不美
6楼-- · 2019-02-15 21:40

Jeff's answer was very close to solving this problem for me. The recommended code from Google breaks target = _blank, and window.open strategies get caught by popup blockers. I modified the test for target like so:

if (!link.target || link.target.match(/^_(self|parent|top)$/i)){
    return false;
    setTimeout(function() { document.location = link.href }, 100);
} else {
    return true;
}
查看更多
登录 后发表回答