Hack 74 Publish Your Associate Sales Statistics on Your Site


Share some insider knowledge with your site's audience.

Your web site has a unique audience, and looking at what they purchase through your associate links can tell you more about them. It can provide insights into other items you might want to sell, and can illustrate what's foremost on their minds (for better or worse). Just as Amazon shares its aggregated sales information in the form of purchase circles, you can create your own purchase circle list by publishing your associate sale information.

Your readers are probably just as curious about sales trends through your site as you are. Publishing the list can build a sense of community, and also drive more sales through associate links.

You could save the HTML reports available through your associates account (http://associates.amazon.com), but it'd be much easier to automate the process and integrate it into your site design with a few lines of Perl.

74.1 The Code

To run this code, you'll need to set the email address and password you use to log into your associates account. This script will then log into the account and download the appropriate sales report. Once the script has the report, it will reformat it as HTML.

#!/usr/bin/perl -w
# get_earnings_report.pl
# Logs into Amazon, downloads earning report,
# and writes an HTML version for your site.
# Usage: perl get_earnings_report.pl
use warnings;
use strict;
use URI::Escape;
use HTTP::Cookies;
use LWP::UserAgent;

# Set your associates account info.
my $email = 'insert Associates email address';
my $pass = 'insert Associates password';
my $aftag = 'insert affiliate tag';

# Create a user agent object.
# and fake the agent string.
my $ua = LWP::UserAgent->new;
$ua->agent("(compatible; MSIE 4.01; MSN 2.5; AOL 4.0; Windows 98)");
$ua->cookie_jar({}); # in-memory cookie jar.

# Request earning reports, logging in as one pass.
my $rpturl  = "http://associates.amazon.com/exec/panama/login/".
my $rptreq  = HTTP::Request->new(POST => $rpturl);
my $rptdata = "report-type=shipments-by-item".   # get individual items
              "&date-selection=qtd".             # all earnings this quarter
              "&login_id=".uri_escape($email).   # our email address.
              "&login_password=".uri_escape($pass). # and password.
              "&submit.download=Download my report". # get downloadble.
              "&enable-login-post=true"; # login and post at once.
$rptreq->content($rptdata); my $report = $ua->request($rptreq);
# Uncomment the following line to see
# the report if you need to debug
# print $report->content;

# Set the report to array.
my @lines = split(/\n/, $report->content);
# Get the time period.
my @fromdate = split(/        /, $lines[1]);
my @todate = split(/        /, $lines[2]);
my $from = $fromdate[1];
my $to = $todate[1];
# Print header...
print "<html><body>";
print "<h2>Items Purchased Through This Site</h2>";
print "from $from to $to <br><br>\n";
print "<ul>";
# Loop through the
# rest of the report
foreach my $line (@lines) {
    my @fields  = split(/        /, $line);
    my $title   = $fields[1];
    my $asin    = $fields[2];
    my $edition = $fields[4];
    my $items   = $fields[8];

    # Format items as HTML for display
    print "<li><a href='http://www.amazon.com/o/ASIN/$asin/ref=nosim/".
          "$aftag'>$title</a> ($items) $edition <br>\n";
print "</ul></body></html>";

You'll notice that $rpturl holds a standard http:// URL that's used to log into Amazon. It's not encrypted, so be sure you're comfortable with sending your associates password over a standard connection before running this script. If you'd like the connection to be encrypted, you'll need a Perl package called Net::SSL (http://search.cpan.org/author/CHAMAS/Crypt-SSLeay-0.51/). This package won't be referenced specifically in the script, but it provides SSL support to the LWP package making the request. Not all ISPs have Net::SSL installed, so check with them to see if it's available. Once you've confirmed that Net::SSL is present, change the URL in $rpturl to begin with https://. This will keep your entire request and response encrypted.

74.2 Running the Hack

Run the script from the command line:

perl get_earnings_report.pl

It prints out the formatted HTML results, so you may want to pipe its output to another file like this:

perl get_earnings_report.pl > amazon_report.html

You could also set this to run on a schedule so your page showing community buying habits stays up to date.