Making the embedding of an amazon referrals sales widget easier and more intelligent
Wouldn't it be great if you could easily earn money through referral sales to Amazon.com and their sister websites with a mobile compatible widget that is easy to embed into your own website?
Well the great news is that you can thanks to the "Search" sales widget Amazon have developed. The problem is that if you want to use it throughout a medium to large website in different locations at different dimensions it can become a bit of an administrative nightmare with all the code you will need to paste into various pages.
As a solution to this I've made a function to ease the process. In addition it has a few clever tricks to hopefully improve the results it displays to your customers and improve the chance that they will click on something.
Lets say I have a blog that discusses computer hardware. With my function installed in the website all I need to do is add the following single line of PHP code to a page....
echo GetAmazonAdWidget("Apple MacBook Pro",712,325);
And this is what I get...
If a user clicks any of those links they are taken to Amazon.com (or one of the sister websites) using my unique referral code. If they then purchase one of the items listed in the widget or pretty much anything else before checking out I will receive a percentage of their sales total!
The clever part
My function (available for download at the bottom of this page) automatically detects the users country based on the browser reported IP address and then uses this information to show them more relevant products.
If the user is surfing in a country where Amazon operate then assuming you have configured the function with the required Amazon Associates membership details it will automatically list products from that countries Amazon store and use the stores local currency. In this demo I have it setup to work for Amazon.co.uk as well as Amazon.com. If the user is in another country it will default to the USA Amazon.com store.
Tips for improving the results in your own website
- You will probably want to wrap your Amazon widget within a div of a fixed or minimum height so that the page and any content below the widget doesn't shift a few times as the widget draws into the page.
- If you specify "auto" as both the width and height parameters for my function the widget will be completely responsive to the space allowed within your page design! The Amazon widget with fully responsive dimensions is running in this working demo.
- The Amazon widget will very likely be blocked if a user has an ad-blocker installed in their browser. A great solution to that is to add a jQuery function to the page to check after it has loaded if there are contents within the containing div that you expect your amazon widget to appear within. So if my containing div had a css class of "wrapper" I would add the following function:
function checkAmazonWidget() { if ($(".wrapper").height() == "0") { $(".wrapper").after('<div class="adBlockMsg">Please disable your adblocker for this website!</div>'); } }
You can naturally replace the content of the adBlockMsg div with other content, images or whatever you want so that you can try to persuade users to disable ad-blocking on your site. Now just add the following to the jQuery $( document ).ready(function() function in the header of your page:
setTimeout("checkAmazonWidget();", 1500);
The 1500 value is the wait in milliseconds that we wait before running the function - this is useful due to the time taken to load the widget. You may want to tweak that value depending on the page your loading.
- You can make the function serve quasi random results but all still relevant to your own choice of subject by storing them in an array with a randomized index value. For example...
$phrases=array("Apple MacBook Pro","PC Desktop","PC Laptop","Apple iPhone 5");
$chosen_index = array_rand($phrases);
$searchQuery=$phrases[$chosen_index];
echo GetAmazonAdWidget($searchQuery,356,250);
The following sample uses the above code. If you refresh this page a few times you will see that a randomized different phrase appears on each refresh.
The disclaimer
This function is not endorsed in any way by Amazon and relies on their "Search" widget to function - you will need to be an approved Amazon Associates member in order to gain access to this and the required membership codes that my function needs in order to run.
You will also need the fantastic GeoIPLoc function by Chirag Mehta that you can download here. Once you've obtained the GeoIPLog php file just place it in the same folder as the php file you paste my function into.
-> Download the GetAmazonAdWidget() PHP function
If you need any help using this function or expanding it for your own website I'm available for hire.