Skip to content

Instantly share code, notes, and snippets.

@dbrent-amazon
Created September 30, 2016 19:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dbrent-amazon/46d660dafa22b619c83ab8465d7f7565 to your computer and use it in GitHub Desktop.
Save dbrent-amazon/46d660dafa22b619c83ab8465d7f7565 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