I'm trying to track outbound clicks from my site. I had no problem figuring out how to add the data to my MySQL database using PHP or how to trigger a call to the function during the onClick, but I'm a JavaScript noob and can't figure out how to send that data to my PHP page, behind the scenes, at the moment of click. Basically I want to send two variables to the page using GET (something like this: "http://mysite.com/tracker.php?source=http://mysite.com/index.php&destination=http://othersite.com/) Any ideas how to do this behind the scenes? It has to be quick, since the click takes them off the site.
It would be easier to redirect it through the page directly, especially since you already have the PHP and MySQL worked out.
I didn't go into any details because I wasn't sure how kosher it was to do this, but I'm basically trying to track when a paid ad was clicked within my Google Custom Search Engine. AdSense won't tell me what queries resulted in a paid click, only the date of the click. In other words, I have no control of the outbound link. I've figured out that the paid ads have ids of "pa1", "pa2", "pa3", etc. and wrote this to identify the clicks and send them to function "adSenseClick": function load1 () { var AdSense = window.frames["googleSearchFrame"].document.getElementById('pa1'); if (AdSense) AdSense.onclick = adSenseClick; } PHP: ...with a separate "load#" for each "id=pa#" (i'm sure there's a better way to do it, but I don't care at this point). Then I use this to trigger the function function doload () { if (document.getElementsByTagName && document.getElementsByTagName ('body')) { load1 (); load2 (); load3 (); load4 (); load5 (); load6 (); } } window.onload = doload; PHP: My problem now is, what do I do with this? function adSenseClick() { } PHP: I need adSenseClick() to call a PHP page that pulls the GET queries from the URL and adds them to my MySQL database. I want to send two variables to the page (something like this: "http://mysite.com/tracker.php?source=http://mysite.com/index.php&destination=http://othersite.com/) Any ideas.
You can just create an empty img and set the src. function adSenseClick() { (new Image()).src = 'http://mysite.com/tracker.php?source=http://mysite.com/index.php&destination=http://othersite.com/' } PHP: and that url will be polled. You can even tell PHP to set the Content-Type to image/gif or something.
No luck with that one. I verified that it works on onLoad, but not onClick, which I need. I worry that the visitor is leaving the page before the JavaScript can react.
The Google iframe sets target=_top in their links. Does anyone know of a way to override this to target=_blank using JavaScript? This might take care of that.
I changed the anchor targets in Google's iframe to "_blank" vs. "_top" and it works great. It definitely seems that the problem lies with the JavaScript not executing before the visitor leaves the page. Anyone know how to override the anchor target in a frame you have no control over?