Archive for the 'AWS' Category

Short Amazon affiliate links - a bookmarklet

Sunday, October 19th, 2008

It's a pain to link to a specific product on Amazon if you have to use their UI to build an affiliate link. It's good to have nice, clean and short affiliate links. This post gives you the details and also a bookmarklet to built the links by visiting the product page you want to link to.

Anatomy of an Amazon affiliate link

Let's see the anatomy of a link in its shortest possible form.
Basically you have:

  • http://www.amazon.com/ - self-explanatory, I think
  • /dp/ - standing for "details product" or maybe "details page"
  • /1847194141/ - a 10 character product code, aka ASIN code, Amazon Standard Identification Number
  • ?tag=affiliatecode-20 - your affiliate code, or tag

For example my Amazon affiliate code is w3clubs-20, and an affiliate link to my book would be:
http://www.amazon.com/dp/1847194141/?tag=w3clubs-20

Amazon also uses longer search engine friendly URLs containing the book title, like:
http://www.amazon.com/Object-Oriented-JavaScript-Stoyan-Stefanov/dp/1847194141/?tag=w3clubs-20

But the book title/product name doesn't really matter, so this is fine too:

http://www.amazon.com/War-and-Piece-by-Stoyan-Stefanov/dp/1847194141/?tag=w3clubs-20

So I went ahead and created a bookmarklet that lets you visit the product page you want to link to, click the bookmarklet and copy a short affiliate URL.

Install the bookmarklet

Drag the link below to your favorites/bookmarks or right-click and add to favorites. Then right click the new bookmark and change its properties, replacing my Amazon affiliate code "w3clubs-20" with yours.

Amazon aff

After installing the bookmarklet, just go to any Amazon product page, click the bookmarklet and it will create an affiliate link for you to copy.

The bookmarklet code, readable version

Here's the code for the bookmarklet, simply find the ASIN (product code) and build a new URL with it.

(function(){
    var aff = 'w3clubs-20';
    if (!document.getElementById('ASIN')) {
        alert('Can\'t find the product ID');
        return;
    }
    var asin = document.getElementById('ASIN').value;
    prompt(
        'Here is the link:',
        'http://www.amazon.com/dp/' + asin + '/?tag=' + aff);
})()

Misc

There's a page in the Amazon affiliates section that let's you check if the links are OK, it's here

Sometimes you might see (usually for non-book products) URLs that instead of /dp/ have /gp/product/, but if you build /dp/ links it's totally fine, according to their link checker, see previous paragraph.

 

Using PEAR and AWS to keep an eye on Amazon

Wednesday, January 10th, 2007

What could possibly be better for a writer's ego other than being read and being praised? Hmm...

So I wanted to have a page that shows the books I've written, together with their Amazon sales rank and the average customer rating and number of reviews. It's really easy. I took one example out of the PEAR book and slightly modified it.

The result is here.

Implementation at a glance

When making a request, you need to say what type of request it is and what type of response you want. It's documented here. For the type of request, look under "API reference -> Operations" and for the response type, look under "API reference -> Response Groups"

The code

<?php

// include the PEAR package
require_once 'Services/AmazonECS4.php';

// Your AWS subscription id
$subscriptionId = '1WQDAES5PQ**********';

// create a new client by supplying
// subscription id
$amazon  = new Services_AmazonECS4($subscriptionId);
$amazon->setLocale('US');

// output options
// what do you need returned?
$options = array();
$options['ResponseGroup'] = 'SalesRank,ItemAttributes,Reviews';

// for which books
// comma-delimited list of ISBNs
$items = '1904811795,1904811914,1904811132';

// do the request
$result = $amazon->ItemLookup($items, $options);

// check for errors
if (PEAR::isError($result)) {
    print "An error occured<br/>";
    print $result->getMessage() . "<br/>";
    exit();
}

// some spaghetti to display HTML response
echo '<ul>';
foreach ($result['Item'] as $book) { // loop the books
    // URL
    echo '<li><a href="'. $book['DetailPageURL'] .'">';
    // book title
    echo $book['ItemAttributes']['Title'], '</a><br />';
    // authors, comma-delimited
    echo implode(', ',$book['ItemAttributes']['Author']);
    // sales rank
    echo '<br />Sales rank: ', $book['SalesRank'];
    // average rating
    if (!empty($book['CustomerReviews'])) {
        echo '<br />Rating: ';
        echo $book['CustomerReviews']['AverageRating'];
        echo ', based on ';
        echo $book['CustomerReviews']['TotalReviews'], ' reviews';

    }
    echo '</li>';
}
echo '</ul>';
?>

<a
  href="#"
  onclick="javascript:document.getElementById('response').style.display='block';"
  >Show complete response</a>
<pre id="response" style="display: none">
<?php print_r($result); ?>
</pre>