It's actually pretty easy to do.
To start in your adwords ad, add an extra parameter to the landing page url, which will tell you that the click came from adwords, and from a certain keyword for example. I tend to encode these, for example number 1 would represent adwords and a certain campaign.
On your landing page, read the parameter from the URL using $_GET['parameter_name'], and put it in a session or cookie, which will temporarily save it. To do this with a session, add the following code to the very top (above the html) of your landing page:
PHP Code:
<?php
session_start();
$_SESSION['tracking'] = $_GET['parameter_name'];
?>
On any page that you want to work with the session parameter, make sure that you add the following to the first line of the code, to activate session variables for that page:
PHP Code:
<?php session_start(); ?>
You now need to deal with putting the parameter in the session variable into your affiliate link.
At the very simplest level you would then just insert the keyword into the link from the session as the Donk said.
PHP Code:
http://www.awin1.com/awclick.php?mid=3&id=45628&clickref=<?php echo $_SESSION['tracking'];?>
For a more advanced setup, you would store your links in a database, and all your affiliate links would go to a certain page on your site - rather than straight to the network eg
Code:
http://www.mysite.com/tracking.php?link_id=1
On tracking.php, you would read the link from the database that has the id of 1. In your database you would store the url with a token in the click ref parameter eg
Code:
http://www.awin1.com/awclick.php?mid=3&id=45628&clickref=[clickref]
You can then use the str_replace command to replace this with the actual tracking id from the session and then redirect:
PHP Code:
$new_link = str_replace("[clickref]",$_SESSION['tracking'],$link_from_database);
header("Location: " . $new_link);
However, before you redirect you can update the click count for that link, giving you your own stats as to how many times a link has been clicked. This is useful as on your site you can see the number of people who clicked on a product to go to the merchants site, even if the person didn't decide to but that product. You could then move the most clicked products to the front page of your site for example.