Skip to content

Instantly share code, notes, and snippets.

@quangnd-pgvn
Forked from dbrent-amazon/report-data
Created March 29, 2017 03:24
Show Gist options
  • Save quangnd-pgvn/ded0fdb091abf01e9eb70df1a8d18b47 to your computer and use it in GitHub Desktop.
Save quangnd-pgvn/ded0fdb091abf01e9eb70df1a8d18b47 to your computer and use it in GitHub Desktop.
/* requestReport */
$req = $client->requestReport(
"productAds",
array("reportDate" => "20160916",
"campaignType" => "sponsoredProducts",
"metrics" => "impressions,clicks,cost,attributedConversions30dSameSKU,attributedConversions30d,attributedSales30dSameSKU,attributedSales30d"));
/* getReport */
$req = $client->getReport("REPORT_ID");
file_put_contents("/Users/dbrent/Downloads/report.json", $req["response"]);
Then I processed the data like so:
$report_json = json_decode(file_get_contents("/Users/dbrent/Downloads/report.json"), true);
$tsv = "sku\tasin\tadId\tclicks\timpressions\tattributedSales30dSameSKU\tattributedSales30d\tattributedConversions30dSameSKU\tattributedConversions30d\n";
foreach ($report_json as $rj) {
$clicks = "";
$impressions = "";
$adId = $rj["adId"];
$clicks = $rj["clicks"];
$impressions = $rj["impressions"];
echo "Processing {$adId}\n";
$req = $client->getProductAd($adId);
$json_ad = json_decode($req["response"], true);
$sku = $json_ad["sku"];
$asin = $json_ad["asin"];
$tsv .= "{$sku}\t{$asin}\t{$adId}\t{$clicks}\t{$impressions}\t{$rj['attributedSales30dSameSKU']}\t{$rj['attributedSales30d']}\t{$rj['attributedConversions30dSameSKU']}\t{$rj['attributedConversions30d']}\n";
usleep(5000);
}
file_put_contents("/Users/dbrent/Downloads/report.tsv", $tsv);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment