Go Back   CodingForums.com > :: Client side development > JavaScript programming > JavaScript frameworks

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-18-2012, 09:47 PM   PM User | #1
seancho
New to the CF scene

 
Join Date: Sep 2012
Posts: 3
Thanks: 2
Thanked 0 Times in 0 Posts
seancho is an unknown quantity at this point
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
seancho is offline   Reply With Quote
Old 09-18-2012, 10:25 PM   PM User | #2
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,461
Thanks: 52
Thanked 457 Times in 455 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
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) {
xelawho is online now   Reply With Quote
Users who have thanked xelawho for this post:
seancho (09-18-2012)
Old 09-18-2012, 11:56 PM   PM User | #3
seancho
New to the CF scene

 
Join Date: Sep 2012
Posts: 3
Thanks: 2
Thanked 0 Times in 0 Posts
seancho is an unknown quantity at this point
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..
seancho is offline   Reply With Quote
Old 09-19-2012, 12:05 AM   PM User | #4
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,461
Thanks: 52
Thanked 457 Times in 455 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
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()
xelawho is online now   Reply With Quote
Old 09-19-2012, 02:13 AM   PM User | #5
seancho
New to the CF scene

 
Join Date: Sep 2012
Posts: 3
Thanks: 2
Thanked 0 Times in 0 Posts
seancho is an unknown quantity at this point
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?
seancho is offline   Reply With Quote
Old 09-19-2012, 03:30 AM   PM User | #6
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,461
Thanks: 52
Thanked 457 Times in 455 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
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
xelawho is online now   Reply With Quote
Users who have thanked xelawho for this post:
seancho (09-21-2012)
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:12 AM.


Advertisement
Log in to turn off these ads.