Is there a simple, universal outbound click tracking method for JQuery / Javascript?

让人想犯罪 __ 提交于 2019-12-11 23:51:53

问题


I'm looking for a way of tracking all outbound clicks from a web page using Javascript/ JQuery without modifying any of the existing page code. The solution must work with frames, iframes, content from different domains, AJAX etc.

Perhaps, Javascript is the wrong technology for a universal solution. If so, please let me know what would be better.


回答1:


It's easy enough to capture all clicks on external links.

$(document).ready(function() {
    $("a[@href^=http]").each(function(){
        if(this.href.indexOf(location.hostname) == -1) {
            // Handle click here
        }
    });
});

Getting this to work for iframes is a little trickier. The iframe should reside from the same domain due to the Same origin policy. If so you should be able to change the css selector above to something like $("#iframe_id").contents().find("[@href^=http]")




回答2:


There is an easy solution:

get all a elements
    check href to see if it begins with http or https
        if so and the domain is external to your site, add a click handler


来源:https://stackoverflow.com/questions/5097695/is-there-a-simple-universal-outbound-click-tracking-method-for-jquery-javascr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!