Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
1 contributor

Users who have contributed to this file

executable file 115 lines (99 sloc) 3.79 KB
<?php
/**
* Copyright 2016 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Google\AdsApi\Examples\AdManager\v201902\ReportService;
require __DIR__ . '/../../../../vendor/autoload.php';
use Google\AdsApi\AdManager\AdManagerSession;
use Google\AdsApi\AdManager\AdManagerSessionBuilder;
use Google\AdsApi\AdManager\Util\v201902\ReportDownloader;
use Google\AdsApi\AdManager\Util\v201902\StatementBuilder;
use Google\AdsApi\AdManager\v201902\ExportFormat;
use Google\AdsApi\AdManager\v201902\ReportJob;
use Google\AdsApi\AdManager\v201902\ReportQueryAdUnitView;
use Google\AdsApi\AdManager\v201902\ServiceFactory;
use Google\AdsApi\Common\OAuth2TokenBuilder;
use UnexpectedValueException;
/**
* This example retrieves and runs a saved report query.
*/
class RunSavedQuery
{
const SAVED_QUERY_ID = 'INSERT_SAVED_QUERY_ID_HERE';
public static function runExample(
ServiceFactory $serviceFactory,
AdManagerSession $session,
$savedQueryId
) {
$reportService = $serviceFactory->createReportService($session);
// Create statement to retrieve the saved query.
$statementBuilder = (new StatementBuilder())->where('id = :id')
->orderBy('id ASC')
->limit(1)
->withBindVariableValue('id', $savedQueryId);
$savedQueryPage = $reportService->getSavedQueriesByStatement(
$statementBuilder->toStatement()
);
$savedQuery = $savedQueryPage->getResults()[0];
if ($savedQuery->getIsCompatibleWithApiVersion() === false) {
throw new UnexpectedValueException(
'The saved query is not compatible with this API version.'
);
}
// Optionally modify the query.
$reportQuery = $savedQuery->getReportQuery();
$reportQuery->setAdUnitView(ReportQueryAdUnitView::HIERARCHICAL);
// Create report job using the saved query.
$reportJob = new ReportJob();
$reportJob->setReportQuery($reportQuery);
$reportJob = $reportService->runReportJob($reportJob);
// Create report downloader to poll report's status and download when
// ready.
$reportDownloader = new ReportDownloader(
$reportService,
$reportJob->getId()
);
if ($reportDownloader->waitForReportToFinish()) {
// Write to system temp directory by default.
$filePath = sprintf(
'%s.csv.gz',
tempnam(sys_get_temp_dir(), 'saved-report-')
);
printf("Downloading report to %s ...%s", $filePath, PHP_EOL);
// Download the report.
$reportDownloader->downloadReport(
ExportFormat::CSV_DUMP,
$filePath
);
print "done.\n";
} else {
print "Report failed.\n";
}
}
public static function main()
{
$oAuth2Credential = (new OAuth2TokenBuilder())->fromFile()
->build();
$session = (new AdManagerSessionBuilder())->fromFile()
->withOAuth2Credential($oAuth2Credential)
->build();
self::runExample(
new ServiceFactory(),
$session,
intval(self::SAVED_QUERY_ID)
);
}
}
RunSavedQuery::main();
You can’t perform that action at this time.