Hack 55 Publishing Amazon.com Associates Statistics

Hack 55 Publishing Amazon.com Associates Statistics

figs/moderate.gif figs/hack55.gif

Share some insider knowledge, such as the most popular item sold, with your site's audience by republishing your Amazon.com Associates sales statistics .

Your web site has a unique audience, and looking at what they purchase through your Amazon.com Associate links can tell you more about them. It can provide insights into other items you might want to sell on your site, and it can help show what's foremost on your visitors ' minds (for better or worse ). Just as Amazon.com shares its aggregated sales information in the form of purchase circles, you can create your own purchase circle list by publishing your Associates sales 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, don't forget, drive more sales through Associate links.

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

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 do the logging in for you, and download the appropriate sales report. Once the script has the report, it will reformat it as HTML.

Because this script logs into Amazon.com, it requires the use of a cookie to remind Amazon.com that you're an authenticated user . Since this is a one-time-only request, we use an in-memory cookie (which is forgotten when the script is finished).

The code listed here intentionally logs you in under an unsecured HTTP connection, to better ensure that the script is portable across systems that don't have the relevant SSL libraries installed. If you know you have them working properly, be sure to change http:// to https :// to gain some added protection for your login information.


Save the following script to a file called get_earnings_report.pl :

 #!/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 strict; use URI::Escape; use HTTP::Cookies; use LWP::UserAgent; # Set your Associates account info. my $email = '   insert email address   '; my $pass = '   insert password'   ; my $aftag = '   insert associates 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/".               "attempt/customer/associates/no-customer-id/25/".               "associates/resources/reporting/earnings/"; 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"; # log in and post at once. $rptreq->content_type('application/x-www-form-urlencoded'); $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(/\t/, $lines[1]); my @todate = split(/\t/, $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. splice(@lines,0,5); foreach my $line (@lines) {     my @fields  = split(/\t/, $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>"; 

Running the Hack

Run the hack from a command line:

 %  perl get_earnings_report.pl  

It prints out the formatted HTML results, so you might 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 regular schedule [Hack #90] so your community's buying habits stay up-to-date.

See Also

  • Amazon Hacks (http://oreilly.com/catalog/amazonhks/) by Paul Bausch

Paul Bausch



Spidering Hacks
Spidering Hacks
ISBN: 0596005776
EAN: 2147483647
Year: 2005
Pages: 157

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net