admin管理员组文章数量:1335616
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="" onClick="recordOutboundLink(this, 'Outbound Links', 'example');return false;">
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." onClick="recordOutboundLink(this, 'Outbound Links', 'example.');return false;">
Share
Improve this question
edited Nov 12, 2012 at 8:18
Jarl
2,8714 gold badges25 silver badges31 bronze badges
asked Apr 21, 2012 at 16:24
RosRos
6343 gold badges18 silver badges35 bronze badges
5 Answers
Reset to default 5edit, I forgot to add target="_blank":
I would do it this way tracking outbound links:
<a href="http://outgoinglink." target="_blank" onclick="_gaq.push(['_trackEvent','outgoing_links','outgoinglink.'])">Link Text</a>
Jeff's answer was very close to solving this problem for me. The remended 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;
}
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.
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
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."
// 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."
onClick="return recordOutboundLink(this, ['Outbound Links', 'example.']);">
<a href="http://www.example."
target="_blank"
onClick="return recordOutboundLink(this, ['Outbound Links', 'example.']);">
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.
本文标签: javascriptGoogle AnalyticsRecord Outbound Linksopen new windowStack Overflow
版权声明:本文标题:javascript - Google Analytics - Record Outbound Links - open new window - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742392236a2466218.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论