[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 519
  • Last Modified:

Conversion Tracking in OnClick=""...

I’m trying to track conversions, but want natural pagerank to pass through my links too.  Am I correct in assuming that the following will always do conversion tracking, and then go to the destination url afterwards?

<a href="http://www.site.com" onClick="javascript: tconv=new Image(); tconv.src='http://www.url.com/track.php';">track click</a>

Now I’m trying to do this so that most of all clicks are tracked.  However, I’m wondering if url.com/track.php is not running and down, will the click not go to the link location then (href of www.site.com)?  Or is it possible that doing the above might result in some clicks that are not truly tracked fast enough by url.com/track.php?  From my testing this looks to work all the time for me.  Maybe it's not iron-clad for some reason though?

I’ve seen people do similar things in the past, but never knew if this was a surefire way of tracking clicks or not.  (Personally I’ve always used the method of sending the person to url.com/redirect.php?page=http://www.site… but now want to do it while tracking the clicks AND sending page rank to the destination URL.)

Andrew


0
rebies
Asked:
rebies
  • 8
  • 5
  • 4
  • +1
2 Solutions
 
XxavierCommented:
Hi rebies!!
  I have not the feintest idea what you are talking about. :?
0
 
Michel PlungjanIT ExpertCommented:
If the url is down, you will still go to the site.
Only if you return false on the onClick will you stay where you are...

Will the page rank go down if you would do this:


<a href="http://www.site.com" onClick="this.href='url.com/redirect.php?page='+this.href">track click</a>

???

you could even do it to all links on a page in an external script:

<script>
function myClick() {
  this.href='url.com/redirect.php?page='+this.href
}
function addClick() {
  for (i=0,n=document.links.length; i<n;i++) {
    if (document.links[i].href.indexOf('http')==0 && document.links[i].onclick==null)  document.links[i].onclick=myClick;
  }
}
window.onload=addClick
</script>
0
 
rebiesAuthor Commented:
mplungjan:

That is a very interesting solution for sure!   I guess if it works, I'm hopeing to do it the other way without having to send the user to 'url.com/redirect.php?page='+this.href at all.

So my prime question (although that is an elegant workaround for sure), is if I do:

<a href="http://www.site.com" onClick="alert('here');">track click</a>

I get a message box saying "here" every time I try that.  So it would seem that any code in the OnClick is run through fully before the user is ever sent to the target location, right?  I think this especially because when I:

<a href="http://www.site.com" onClick="alert('here'); return false;">track click</a>

The click does not work even if I sit there for 2 minutes before hitting "okay".

But if I'm doing

tconv=new Image();
tconv.src='http://www.url.com/track.php';

inside the OnClick, am I guaranteed that http://www.url.com/track.php will load every time and record every click?  Or is it maybe possible on a very slow Internet connection that the user is redirected to the target document before that 1x1 pixel is loaded, and properly tracked?

Make sense what I am trying to understand here?
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
rebiesAuthor Commented:
Below is a better way of explaining my concerns...

I found a large image using Google Image search.  The image at the URL below is 718k in size.  However, when I run the following code I immediately get alerted "now alerting myself", and I know there is no way my computer downloaded that image in .001 seconds!

<script language="JavaScript">
tconv=new Image();
tconv.src='http://www.cyber-heritage.co.uk/sunset.jpg';
alert('now alerting myself');
</script>

So in the case of the following code, should I be worried that at times the visitor is sent to www.site.com before my 1x1 pixel at url.com/track.php is loaded??

<a href="http://www.site.com" onClick="javascript: tconv=new Image(); tconv.src='http://www.url.com/track.php';">track click</a>

From my tests, it seems to be working well, but I don't know for sure.
0
 
COBOLdinosaurCommented:
Use the complet property of the image to delay until it is loaded.

function getImg()
{
tconv=new Image();
tconv.src="whatever.php?etc...";
x=0;
while (!tconv.complete) {x=x;}
return true;
}
<a href="http://www.site.com" onClick="return getImg();">track click</a>


Cd&
0
 
rebiesAuthor Commented:
Thats a nice try, but defaintely not stable!  "A script on this page is causing Internet Explorer to respond slowly...."
I suspected as much before running it.

<script language="JavaScript">
tconv=new Image();
tconv.src='http://www.cyber-heritage.co.uk/sunset.jpg';
x=0;
while (!tconv.complete) {x=x;}
alert('now alerting myself');
</script>

In any case, still not quite an exact answer to the question.  In terms of tracking, should I be worried that at times the visitor is sent to www.site.com before my 1x1 pixel at url.com/track.php is loaded??  I am suspecting it could be a problem after my little test above.

<a href="http://www.site.com" onClick="javascript: tconv=new Image(); tconv.src='http://www.url.com/track.php';">track click</a>
0
 
COBOLdinosaurCommented:
All it take is a slow response either because of server load, or a slow hop and it will be back late.  If the packet gets dropped and has to be re-sent; back late.  if the connection fails anywhere on the route; back late or not at all.  Even client load can affect it .  If the user is runnin a virus scan in background or doing a big download in another window, you could get lost in the shuffle.

Cd&
0
 
rebiesAuthor Commented:
Hmm, that brings up and interesting point I had not thought of though...

So if the user clicks the link, his browser is going to immediately send a request for the 1x1 image (assuming his computer isn't too slow).   Thus, even if the load of that image takes a while and he is redirected to target URL, that request is still most likely going to get to my server, and track the click, right?  Even though my server will respond with an image that does not loaded in the users browser - it will track when my server serves up the image, weither or not that image has been loaded in the users browser.

I realize dropped packets and connection faliures along the way will make this not a surefire way of tracking clicks.  (As will antivirus in other programs or other large downloads.)  But would you think it be fairly accurate?  Now that I think about it, yes, it's not foolproof, but it could be accurate 95% of the time, right?
0
 
rebiesAuthor Commented:
Sorry, just read my last post to make sure it made sense.  A few grammatical problems in that.  I can clarify if it the above post does not make sense.
0
 
COBOLdinosaurCommented:
It make sense.  I would say as long as you have a reliable site that you are going to get hit up around 98% of the time.  As long as you are counting the hits to the image on the server you are going to have it pretty close unless you ISP really screws up, in which case you are going to know it because you r whole site will be affected.

Cd&
0
 
Michel PlungjanIT ExpertCommented:
Sorry I was out.

Cobol's method is not stable because the loop is too tight. It needs setTimeout to allow the image to acually load while the loop is running.

try this one.

<script>
theHref=""
</script>
<img name="tracker" src="dummy.gif" width="1" height="1"
onLoad="if (this.src.indexOf('dummy')==-1) location=theHref">
<a href="page.html" onClick="theHref=this.href;
document.tracker.src='http://www.url.com/track.php?href='+theHref;
return false">page</a>
0
 
rebiesAuthor Commented:
Okay one final question then...

How do we know that JavaScript will execute the code in the OnClick method before telling the browser to load the page in the HREF attribute?  Is this a part of the HTML standards that says IF there is an OnClick event defined then run that code, ELSE point the browser to the location specified in the HREF attribute?  I guess I'm asking - will the browser  make the request for the 1x1 pixel 100% of the time before telling itself to go to the new page as defined by HREF=""?

I assume this is the case 99% of the time, without factoring really slow machines, etc.  However, I am just trying to verify and understand how and why the code works the way it does.
0
 
Michel PlungjanIT ExpertCommented:
Yes, the onClick is fired before the link is followed.

I am not sure if IE will not try to execute things as fast as possible.
0
 
COBOLdinosaurCommented:
even if the machine is slow both actions result in an http request so I would think the link would queue behind the image request anyway. I don't think you get a cancel of the image request when the href fires so it should always go.

Cd&
0
 
rebiesAuthor Commented:
Cool.

Thanks for helping me work though this guys!  Sometimes I feel I understand the issue but really other people to help me work through the issue!  (If not sparking additional concerns and thoughts along the way!)

COBOL will get the answer since he is the one that really helped me figure it out.   Mplugjan an assist for a great alternative and the quick help.

Andrew
0
 
COBOLdinosaurCommented:
Glad we could help.  Thanks for the A. :^)

Cd&
0
 
Michel PlungjanIT ExpertCommented:
Did you try this one? It had a missing end script tag but it will not let the link go unless the image is loaded

<script>
theHref=""
</script>
<img name="tracker" src="dummy.gif" width="1" height="1"
onLoad="if (this.src.indexOf('dummy')==-1) location=theHref">
<a href="page.html" onClick="theHref=this.href;
document.tracker.src='http://www.url.com/track.php?href='+theHref;
return false">page</a>
0
 
rebiesAuthor Commented:
Yes, I saw that and understood it.   Unfortunately I was not looking for a workaround, but needed help confirming if the code I already had was stable.  Thats why COBOL got the accepted answer as he provided the insight and direction I needed to fully understand how and why my code works. (almost always, anyway)

0

Featured Post

Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

  • 8
  • 5
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now