class ParallelReportDownload {
// Timeout between retries in seconds.
const BACKOFF_FACTOR = 5;
// Maximum number of retries for 500 errors.
const MAX_RETRIES = 5;
// The number of entries per page of the results.
const PAGE_LIMIT = 500;
public static function runExample(AdWordsServices $adWordsServices,
AdWordsSessionBuilder $sessionBuilder, $reportDir) {
// Construct an API session for the client customer ID specified in the
// configuration file.
$session = $sessionBuilder->build();
// Create selector.
$selector = new Selector();
$selector->setFields(['Month', 'Impressions', 'Clicks', 'Ctr', 'AverageCpc', 'AveragePosition', 'Cost', 'Conversions', 'CostPerConversion', 'ConversionRate', 'SearchImpressionShare']);
// Use a predicate to filter out paused criteria (this is optional).
//$selector->setPredicates([
//new Predicate('Impressions', PredicateOperator::GREATER_THAN, [1000]),
//new Predicate('CampaignName', PredicateOperator::CONTAINS, ['Branded'])
//]);
// Create report definition.
$reportDefinition = new ReportDefinition();
$reportDefinition->setSelector($selector);
$reportDefinition->setReportName('LAST_MONTH ACCOUNT_PERFORMANCE_REPORT');
$reportDefinition->setDateRangeType(
ReportDefinitionDateRangeType::CUSTOM_DATE);
$reportDefinition->setReportType(
ReportDefinitionReportType::ACCOUNT_PERFORMANCE_REPORT);
$reportDefinition->setDownloadFormat(DownloadFormat::CSV);
$customerIds = self::getAllManagedCustomerIds($adWordsServices, $session);
printf("Downloading reports for %d managed customers.\n",
count($customerIds));
$successfulReports = [];
$failedReports = [];
foreach ($customerIds as $customerId) {
$filePath = "../../../../../../clients/client_reports/accounts/" . $customerId . "_LAST_MONTH___account.csv";