Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

With jQuery, How Do I Intercept Hyperlink Click Events Temporarily?

This question refers to affiliate marketing, but really is a generic question about intercepting hyperlinks before they go off to another site, where you can log visitor activity to a database.

My affiliate marketing client had a really good question. Imagine a scenario where you have products pulled back from Amazon over its API, given a seed keyword. Now imagine a visitor clicks one of those products to view it on Amazon. The URL for that product might look like this (and this is just a demo):

http://www.amazon.com/dp/B0042RU3Y0/?tag=xxxxxxxxxxxxxxxx-20

But guess what's wrong with that? It's not passing that seed keyword. So, we have no idea which seed keywords were the most effective. Instead, she was wishing we could pass the following and then track that somehow:

http://www.amazon.com/dp/B0042RU3Y0/?tag=xxxxxxxxxxxxxxxx-20&seed=laptops

I didn't see any docs on Amazon where we could pass extra params and then track them in the reports by a filter.

So, the only thing I could think of is that we need to capture the click before it goes off to Amazon. In other words, before that event bubbles up and is executed, somehow in jQuery I can intercept it first, parse the href URL for that hyperlink, tack on this extra seed keyword information, send it over AJAX back to a PHP page and a database table, and then release that click event so that it is executed and the browser goes off to Amazon.

Does anyone know how this is done in jQuery? I know the AJAX part -- just not the click intercept part that grabs the click, then releases it.

like image 809
Volomike Avatar asked Dec 08 '10 05:12

Volomike


2 Answers

You can bind a click event to all the anchor tags, like

$("a").click(function(){
    // write your code
});

If you want to do the default action then put a return true; at the end of this function.

like image 188
rahul Avatar answered Sep 28 '22 06:09

rahul


var seed = "&seed=laptops";
$("a").live('click',function(){
    $(this).attr('href', $(this).attr('href')+seed);
});
like image 37
Mark Avatar answered Sep 28 '22 06:09

Mark