Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Sep 2012
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    strip port # from e.currentTarget.host

    Hi. Web developer, JS newbie. I am setting up outbound link tracking in Google Analytics and I want to use a short jquery script to test every clicked link to see if it is outbound or not. I have the following script:

    Code:
    $("a").on('click',function(e){
        var url = $(this).attr("href");
        if (e.currentTarget.host != window.location.host) {
            _gaq.push(['_trackEvent', 'Outbound Links', e.currentTarget.host, url, 0]);
            if (e.metaKey || e.ctrlKey) {
                 var newtab = true;
            }
            if (!newtab) {
                 e.preventDefault();
                 setTimeout('document.location = "' + url + '"', 100);
            }
        }
    });
    The script works, and pushes outbound link events to GA, but it is also catching many local links as well, throwing off my outbound link stats. They show up in GA as myhost.com:80. Since myhost.com:80 != myhost.com, the script incorrectly pushes the outbound link event when the link is actually local.

    I'm not sure why some local links have the port appended, but I need to modify the above code to strip that port number from e.currentTarget.host before it is compared to window.location.host. Unfortunately, I'm not sure how to do it. Very grateful for any pointers.. Sean

  • #2
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,772
    Thanks
    55
    Thanked 518 Times in 515 Posts
    you may have to convert it to a string first, but you could try
    Code:
    if (e.currentTarget.host.replace(/[\d:]/g,"") != window.location.host) {

  • Users who have thanked xelawho for this post:

    seancho (09-18-2012)

  • #3
    New to the CF scene
    Join Date
    Sep 2012
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by xelawho View Post
    Code:
    if (e.currentTarget.host.replace(/[\d:]/g,"") != window.location.host) {
    It's still early, but I think this is working! Thank you! Now, if I wanted to go the convert-to-string route, how would that look? I'm thinking it would be good to strip out the port from e.currentTarget.host on the actual event push as well, since I sometimes get port numbers on real outbound links. If I had a de-ported string, I could use it both places. But I don't get how to make variable declarations in jquery..
    Last edited by seancho; 09-18-2012 at 11:59 PM.

  • #4
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,772
    Thanks
    55
    Thanked 518 Times in 515 Posts
    it looks to me like it is already a string. you can check by doing this:
    Code:
    alert(typeof(e.currentTarget.host))
    but if it isn't then you could do this:
    Code:
    var mystring=e.currentTarget.host.toString()

  • #5
    New to the CF scene
    Join Date
    Sep 2012
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by xelawho View Post
    it looks to me like it is already a string. you can check by doing this:
    Code:
    alert(typeof(e.currentTarget.host))
    but if it isn't then you could do this:
    Code:
    var mystring=e.currentTarget.host.toString()
    But I would have to add the 'replace' as well. So something like

    Code:
    var mystring=e.currentTarget.host.replace(/[\d:]/g,"").toString()
    Can I do that?

    And then the whole thing would be something like

    Code:
    $("a").on('click',function(e){
        var url = $(this).attr("href");
        var linkclick=e.currentTarget.host.replace(/[\d:]/g,"").toString();
        if (linkclick != window.location.host) {
            _gaq.push(['_trackEvent', 'Outbound Links', linkclick, url, 0]);
            if (e.metaKey || e.ctrlKey) {
                 var newtab = true;
            }
            if (!newtab) {
                 e.preventDefault();
                 setTimeout('document.location = "' + url + '"', 100);
            }
        }
    });
    How about that?

  • #6
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,772
    Thanks
    55
    Thanked 518 Times in 515 Posts
    probably. for some reason I prefer it like this

    Code:
    var linkclick=e.currentTarget.host.toString().replace(/[\d:]/g,"");
    but I don't guess it will make much difference

    obviously you don't have this problem, but for future reference, that regex just replaces all numbers and colons, which would be problematic if your host were say my3host.com. A better one I think would be
    Code:
    .replace(/:[^:]+\d/g,"");
    which says replace all the numbers after the colon and the colon

  • Users who have thanked xelawho for this post:

    seancho (09-21-2012)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •