Extract Amazon Affiliate earnings script

Extract Amazon Affiliate earnings script

My earlier posts have been about creating links to Amazon that are localised in WordPress, see my posts here and here. One perk of using the excellent Amazon Link plugin for WordPress is that I get a small commission for items sold through the Amazon Affiliate program. It is not much, but it keeps this site running plus a cup of coffee or two a year. I have an Amazon Affiliate account in

  • United States
  • United Kingdom
  • Germany
  • France
  • Spain
  • Italy
  • Canada
  • China

I can log into each site and get a report telling me how much I earned in the local currency. These site use different currencies, we have American dollars, Canadian dollars, British pounds, European euros and Chinese yuan. But I just needed a very simple number, how much is my total commission across all the sites in my local Danish currency? For this purpose I found a PHP framework called Open Affiliate Report Aggregator (OARA)1 to create a small script to extract the information from each site and use google currency converter to sum the total to my local currency.

Example output

When executing the script using three Amazon Affiliate accounts (US, UK and DE) one gets the following simple output

Example output from script using Amazon US, UK and DE. This can also be sent by mail using CRON

Notice that the final output is the ~13 USD and ~5 EUR summed and converted into Danish kroner.

Installation

  1. Follow the installations instructions found on the PHP-OARA GITHUB here
  2. Download the file AmazonAffiliateReportExtractor.php from GITHUB here
  3. Copy AmazonAffiliateReportExtractor.php to the php-oara/examples folder

Configuration

Edit the AmazonAffiliateReportExtractor.php script

  1. Change $credentials["user"] and $credentials["password"] to match your login to Amazon Affiliate. This scripts assumes that you use the same login and password for all sites. I recommend you create a serviceuser account with limited access if you are going to have any password in a script
  2. Change $startDate and $endDate
  3. Set which currency you want the final result in $totalCommisionCurrency
  4. Add a name for the report in $reportName
  5. For each Amazon Affiliate account you want to login you need the following four lines. Change only the first and second line to the corresponding Amazon locale and currency
$credentials["network"] = "us"; //Change this to match the Amazon locale
$currency = "USD"; //Change this to match the currency used
$network = Oara_Factory::createInstance($credentials);
$totalCommision += ReportExtractor::currency($currency, $totalCommisionCurrency, ReportExtractor::Extract($network, "Amazon ".strtoupper($credentials["network"]), $currency, $startDate, $endDate));

Run the script by invoking it

php AmazonAffiliateReportExtractor.php

You should get an output similar to the example above. If you want this report mailed every month you can use CRON like this (assuming sending mail has been configured, I use Googles SMTP server for this):

@monthly php /some/path/php-oara/examples/AmazonAffiliateReportExtractor.php | mail -s "Amazon Affiliate report" [email protected]

Conclusion

This setup sends me a short report every month to my email with all my Amazon Affiliate commissions summed into a single total in my chosen currency. This means I spend less time logging into my 8 Amazon Affiliate account and manually extracting the numbers, converting them into a common currency and calculating a total. If you know of a better way to do this, please leave me a comment.

UPDATE 08-04-2016
A few weeks ago, extracting data from Amazon.com always failed. I performed the change described here to patch php-oara and it worked again

  1. Open Affiliate Report Aggregator (OARA) is a set of PHP classes that can download affiliate reports from a number of affiliate networks 

2 Comments

Neloy

why I faced this error/warnning….. Please solve this…

  1. Warning: require_once(vendor/autoload.php): failed to open stream: No such file or directory in C:\wamp\www\php-oara\settings.php on line 22
  2. Fatal error: require_once(): Failed opening required ‘vendor/autoload.php’ (include_path=’.:/usr/local/lib/php:/www/google-api-client/src’) in C:\wamp\www\php-oara\settings.php on line 22

Poul Serek

Hi Neloy

I use linux so I am not sure why it does not work with your installation, but it seems that the include paths are wrong, those are linux paths, not Windows paths. That might be why it just works on linux. You might try following the instructions on the GitHub page to see if it works: https://github.com/fubralimited/php-oara

/Poul

Leave a reply