Amazon does not provide an API for automatic download of the data in your amazon affiliate dashboard.

So, the only way to get the data in an automated fashion is to use web scraping.

Puppeteer

Puppeteer (https://github.com/GoogleChrome/puppeteer) is a headless chrome Node.JS API. This means that you can control chrome using javascript.

Using Puppeteer to download the amazon affiliate reports

Launch Puppeteer

For now, we will launch the browser in window mode. Later on, we can run it in headless mode. In this mode, you will not see the browser at all, but everything will work like as though there is a real browser.

const puppeteerOptions = {
        headless: false,
        args: [
            `--window-size=${ width },${ height }`
        ],
        verboseLog: true
    };
const browser = await puppeteer.launch(puppeteerOptions);

Open Amazon Affiliate URL in puppeteer

const page = await browser.newPage();
// Set width and height of the viewport
await page.setViewport({ width: 1400, height: 1600 })
// Navigate to the affiliate URL page
await page.goto(amazonAffiliateUrl, {waitUntil: 'networkidle2'});

Here we are creating a new page and then setting the width and height of the viewport.

Next, we navigate to the affiliate URL ( amazonAffiliateUrl ). The value of this URL depends on the country you have your account.

For USA, it is https://affiliate-program.amazon.com/ and for India, it is https://affiliate-program.amazon.in/ .

Ok, so now if you run the script, you should Puppeteer launch chrome and navigate to the Affiliate page.

You should see something like this, but for your country.

1. Amazon Affiliate Page
Amazon Affiliate Page

Click Login button

The next step is to login to your account. Get started by clicking the login button with Puppeteer.

Login Button
Login Button

If you see the DOM, the login button is inside a div with class=ac-header-item. The a tag inside it is the Login button. Using Puppeteer, click that button.

const loginButton = await this.page.$(".ac-header-item>a[href='/login']");
    if (loginButton) {
      await Promise.all([
        this.page.waitForNavigation(),
        this.page.click(".ac-header-item>a[href='/login']", { delay: humanDelay(100) })
      ])
    }

Fill Username and Password

The next step is to fill username and password in the Login form.

Login Form
Login Form

Using page.evaluate, we will run some javascript to populate the fields and then click the Login button.

// Fill username and password
await this.page.evaluate((a, b) => {
      document.querySelector('#ap_email').value = a;
      document.querySelector('#ap_password').value = b;
}, 'YOUR_USERNAME', 'YOUR_PASSWORD');

// Click Login Button
await Promise.all([
      this.page.click('#signInSubmit', { delay: humanDelay(100) }),
      this.page.waitForNavigation()
])

Dashboard page

Once you login, you will land on your dashboard page. In this page, you should be seeing your Earnings Overview.

4. Earnings Overview
Earnings Overview

From here, you can go to many places depending on the data you want to download.

Monthly Summary

The montly summary is available on the right panel. To read/download that data, you need to inspect the DOM and figure out the classes and IDs that you can use to navigate the HTML until you reach your data element.

There are 5 rows of data in the monthly summary.

  1. Total Items Shipped
  2. Total Earnings
  3. Total Ordered Items
  4. Clicks
  5. Conversion

The rows can be accessed using the code below.

let monthlyDataRow = await this.page.$$('div[data-assoc-eid="ac-home-month-summary"] .ac-home-summary-data .ac-card-data-row');

If you check monthlyDataRow.length, it will be 5. Now, for each row, get innerHTML to get to the value. Cool, so now you have the monthly summary data. You can log this to a file or save to your database or excel sheet or whatever your requirement is.

Reports

To access more Reports, look at the navigation menu.

We know that under Reports, we have access to much more granular data.

So, click on Reports button.

await Promise.all([
      this.page.click('a[data-assoc-eid="ac-home-view-report-link"]', humanDelay(500)),
      this.page.waitForNavigation()
    ])

To get the exact data that you want, you have to change the time range for the reports. For this, using your mouse, you need to hover over the Report Date. But, is this possible in Puppeteer? Of course it is.

Reports Range
Reports Range

To hover over the data, look at the DOM to get to the node to hover on.

// Hover over the Date under Reports
await this.page.hover('.ac-report-summary-filter .ac-report-summary-filter-item-main');

Here, let's click on the radio button for Yesterday.

// Click on "Yesterday" radio button
await this.page.click('#ac-daterange-radio-report-timeInterval-yesterday [name="ac-daterange-radio-report-timeInterval"]');

Click Apply

// Click Apply
await this.page.click('button#ac-daterange-ok-button-report-timeInterval-announce');

The rendered chart has all the data in the bottom section.

Chart Data
Chart Data

Now, for each of the data that you want, you have to get the text content. So lets create a function that takes a css selector and returns the text inside it.

async getTextContentFromElementId(id) {
    const element = await this.page.$(`#${id}`);
    const text = await this.page.evaluate((ele) => {
      return ele.textContent
    }, element);
    return text;
  }

Now, we need to find the selector for each of the data points and pass it to the getTextContentFromElementId function.

The selector for Clicks is 'ac-report-commission-commision-clicks', the selector for Ordered Items is 'ac-report-commission-commision-ordered'. It should be fairly easy to find the selector for the remaining items. To get the value, call your previous function.

// Get Clicks
const clicks = await this.getTextContentFromElementId('ac-report-commission-commision-clicks');
// Get Ordered Items
const orderedItems = await this.getTextContentFromElementId('ac-report-commission-commision-ordered');
//...etc

This way you can retrieve all the values that you want from your Amazon Affiliate Reports.

Headless mode

When launching Puppeteer, in the options pass headless: true . In this mode, the browser's window will not be launched. Instead Puppeteer will run everything in memory.

This is required for running your code in servers where there is no display for rendering the UI. Locally, it will allow you to run your tool in the background without opening any windows.

What next?

There are some options on where you can go from here.

Create a dashboard on your local computer

Run the affiliate download script every morning and glance at the data that it produces. The annoying task of clicking buttons and searching for the data can easily be replaced with this script.

Email the data to yourself

Run this job on a server and send the output to your email every morning. This way you can see the results on your phone without even logging in to affiliate dashboard.

Send reports to your clients

Do you manage affiliate sites for your clients? Now you can send them automatic emails.

Disclaimer

Please read Amazon's terms and conditions before running such a script. Do not overload their servers (lol).

If you want a custom hosted script, I will be happy to build it. Contact me at thepagemonk [at] gmail [dot] com.