Skip to content

Instantly share code, notes, and snippets.

@rummelonp
Created December 5, 2010 14:27
Show Gist options
  • Save rummelonp/729123 to your computer and use it in GitHub Desktop.
Save rummelonp/729123 to your computer and use it in GitHub Desktop.
GoogleAnalyticsの外部リンクをトラックするコードを、全てのリンクに追加するJavaScript(要prototype.js)
var addTrackToLink = function() {
var anchor = new RegExp(location.href + "#.*?");
var js = /^javascript\:/;
return function(link) {
var handler = link.readAttribute('onclick');
var href = link.href;
if (handler === null && !anchor.test(href) && !js.test(href)) {
var action = href.match(/http?:\/\/([^\/]+)\//)[1];
link.writeAttribute('onclick', "javascript:_gaq.push(['_trackEvent', 'outbound-article', '" + action + "'])");
}
};
}();
document.observe('dom:loaded', function(event) {
$$('a').each(addTrackToLink);
}, false);
document.observe('DOMNodeInserted', function(event) {
$(event.target).select('a').each(addTrackToLink);
}, false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment