Created
December 5, 2010 14:27
-
-
Save rummelonp/729123 to your computer and use it in GitHub Desktop.
GoogleAnalyticsの外部リンクをトラックするコードを、全てのリンクに追加するJavaScript(要prototype.js)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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