get All AdUnits

701 views
Skip to first unread message

Kamran Khan

unread,
Jun 26, 2019, 6:20:49 AM6/26/19
to Google Ad Manager API Forum
Hello All,

I want to generate an Ad unit report which will show all of my Ad unit impression, clicks and revenue, I have the following code that get only particular Ad unit, I want to change this and get all Ad units.

can someone please help and suggest or give my valid code example.

class Adunit_report
{
public function runExample(
ServiceFactory $serviceFactory,
AdManagerSession $session
) {
$reportService = $serviceFactory->createReportService($session);
$networkService = $serviceFactory->createNetworkService($session);
// Get the network's root ad unit ID to filter on.
$rootAdUnitId = 21809757847;
// Create statement to filter on a parent ad unit with the root ad unit
// ID to include all ad units in the network.
$statementBuilder = (new StatementBuilder())
->where('PARENT_AD_UNIT_ID = :parentAdUnitId')
->withBindVariableValue('parentAdUnitId', intval($rootAdUnitId));
// Create report query.
$reportQuery = new ReportQuery();
$reportQuery->setDimensions(
[
Dimension::AD_UNIT_ID,
Dimension::AD_UNIT_NAME
]
);
$reportQuery->setColumns(
[
Column::AD_SERVER_IMPRESSIONS,
Column::AD_SERVER_CLICKS,
Column::TOTAL_LINE_ITEM_LEVEL_IMPRESSIONS,
Column::AD_SERVER_INVENTORY_LEVEL_WITH_CPD_PERCENT_REVENUE
]
);
// Set the filter statement.
$reportQuery->setStatement($statementBuilder->toStatement());
// Set the ad unit view to hierarchical.
$reportQuery->setAdUnitView(ReportQueryAdUnitView::HIERARCHICAL);
// Set the start and end dates or choose a dynamic date range type.
$reportQuery->setDateRangeType(DateRangeType::CURRENT_AND_NEXT_MONTH);
// Create report job and start it.
$reportJob = new ReportJob();
$reportJob->setReportQuery($reportQuery);
$reportJob = $reportService->runReportJob($reportJob);
// Create report downloader to poll report's status and download when
// ready.
return;
$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(), 'inventory-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 function main()
{
$oAuth2Credential = (new OAuth2TokenBuilder())->fromFile()
->build();
$session = (new AdManagerSessionBuilder())->fromFile()
->withOAuth2Credential($oAuth2Credential)
->build();
$this->runExample(new ServiceFactory(), $session);
}
}

Ad Manager API Forum Advisor Prod

unread,
Jun 26, 2019, 5:01:47 PM6/26/19
to idofk...@gmail.com, google-doubleclick...@googlegroups.com

Hello,

Please use the sample code given here and change to line 53 like in the sample code to show the impression, clicks and revenue of all Ad units.

Regards,
Michael, Ad Manager API Team



ref:_00D1U1174p._5001UCZZYP:ref

Kamran Khan

unread,
Jun 27, 2019, 2:49:59 AM6/27/19
to Google Ad Manager API Forum
Hi Michael,

Thanks for the reply, on your suggestion I have changed the link to 

$rootAdUnitId = $networkService->getCurrentNetwork()->getEffectiveRootAdUnitId();
Now it's returning empty report, let me share my screens with you so for better understanding

The screen 1 shows all of my Ad unit mark in red , now I want to get all of my Adunit impressions, clicks and revenue.

Now my class looks like this, the xls sheet is also attach which is empty, now when I try with particular Ad unit ID I do get some results, I am attaching that xls sheet also see the file 'invE02E.tmp", hope this will clear things

<?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
*
*
* 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.
*/

require APPPATH . 'third_party/admanager/vendor/autoload.php';



use Google\AdsApi\AdManager\AdManagerSession;
use Google\AdsApi\AdManager\AdManagerSessionBuilder;
use Google\AdsApi\AdManager\Util\v201905\ReportDownloader;
use Google\AdsApi\AdManager\Util\v201905\StatementBuilder;
use Google\AdsApi\AdManager\v201905\Column;
use Google\AdsApi\AdManager\v201905\DateRangeType;
use Google\AdsApi\AdManager\v201905\Dimension;
use Google\AdsApi\AdManager\v201905\ExportFormat;
use Google\AdsApi\AdManager\v201905\ReportJob;
use Google\AdsApi\AdManager\v201905\ReportQuery;
use Google\AdsApi\AdManager\v201905\ReportQueryAdUnitView;
use Google\AdsApi\AdManager\v201905\ServiceFactory;
use Google\AdsApi\Common\OAuth2TokenBuilder;
use Google\AdsApi\AdManager\Util\v201905\AdManagerDateTimes;

/**
* This example retrieves and runs a saved report query.
*/
class Adunit_report
{
public function runExample(
ServiceFactory $serviceFactory,
AdManagerSession $session
) {
$reportService = $serviceFactory->createReportService($session);
$networkService = $serviceFactory->createNetworkService($session);
// Get the network's root ad unit ID to filter on.

// Get the network's root ad unit ID to filter on.
$rootAdUnitId = $networkService->getCurrentNetwork()
->getEffectiveRootAdUnitId();

// Create statement to filter on a parent ad unit with the root ad unit
// ID to include all ad units in the network.
$statementBuilder = (new StatementBuilder())
->where('PARENT_AD_UNIT_ID = :parentAdUnitId')
->withBindVariableValue('parentAdUnitId', intval($rootAdUnitId));
// Create report query.
$reportQuery = new ReportQuery();
$reportQuery->setDimensions(
[
Dimension::AD_UNIT_ID,
Dimension::AD_UNIT_NAME
]
);
$reportQuery->setColumns(
[
Column::AD_SERVER_IMPRESSIONS,
Column::AD_SERVER_CLICKS,
Column::TOTAL_LINE_ITEM_LEVEL_IMPRESSIONS,
Column::TOTAL_INVENTORY_LEVEL_ALL_REVENUE
]
);
// Set the filter statement.
$reportQuery->setStatement($statementBuilder->toStatement());
// Set the ad unit view to hierarchical.
$reportQuery->setAdUnitView(ReportQueryAdUnitView::HIERARCHICAL);
// Set the start and end dates or choose a dynamic date range type.
// Set the start and end dates or choose a dynamic date range type.
$reportQuery->setDateRangeType(DateRangeType::CUSTOM_DATE);
$reportQuery->setStartDate(
AdManagerDateTimes::fromDateTime(
new DateTime(
'-90 days',
new DateTimeZone('America/New_York')
)
)
->getDate()
);
$reportQuery->setEndDate(
AdManagerDateTimes::fromDateTime(
new DateTime(
'now',
new DateTimeZone('America/New_York')
)
)
->getDate()
);
// Create report job and start it.
$reportJob = new ReportJob();
$reportJob->setReportQuery($reportQuery);
$reportJob = $reportService->runReportJob($reportJob);
// Create report downloader to poll report's status and download when
// ready.

screen1.png

?>
inv293D.tmp.csv
invE02E.tmp.csv

Kamran Khan

unread,
Jun 27, 2019, 2:52:07 AM6/27/19
to Google Ad Manager API Forum
Michel, 

screen shot may not be clear, I am attaching screen shot with this reply.

Thanks.


On Thursday, 27 June 2019 02:01:47 UTC+5, admanagerapiforumadvisor wrote:
screen1.png

Ad Manager API Forum Advisor Prod

unread,
Jun 27, 2019, 4:18:48 PM6/27/19
to idofk...@gmail.com, google-doubleclick...@googlegroups.com

Hello Kamran,

You can remove your filter to get all your Ad units for your report. If you are still getting no results please send your network code and e-mail address of the API user so that I may further investigate? Use the reply privately to author option and I can take a closer look.



Regards,
Michael, Ad Manager API Team



ref:_00D1U1174p._5001UCZZYP:ref

Kamran Khan

unread,
Jun 28, 2019, 6:23:31 AM6/28/19
to Ad Manager API Forum Advisor Prod, google-doubleclick...@googlegroups.com
Hi  Michael,

I have send you all of the details in private reply waiting for your response.

Thanks
Kamran Khan

--
You received this message because you are subscribed to the Google Groups "Google Ad Manager API Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-doubleclick-for-p...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-doubleclick-for-publishers-api/SA6RQ000000000000000000000000000000000000000000000PTRXR600Bwoq9chSQZSvLws1FgegPA%40sfdc.net.


--
Kamran Khan.
Web Developer & Programmer (PHP)
Medialinkers Pvt. Ltd.
Mobile:- +92 03442855122+92 03442855122
You'll need Skype CreditFree via Skype

Ad Manager API Forum Advisor Prod

unread,
Jun 28, 2019, 3:57:10 PM6/28/19
to idofk...@gmail.com, google-doubleclick...@googlegroups.com

Hello Kamran,

If you want to try and match your report from the UI you can run a ReportQuery from the ReportService to generate the reports via API. You can find these available dimensions and columns. You can check these sample codes from the reportservice. There is also an easy way to run the report by running the saved query. For this, you can create and save the query with all the parameters that you would like in the GAM UI and give the query ID in this line 174 of sample code RunSavedQuery.java to pull the data.

You can check out this guide on reporting basics for more information on how to do that. 



Regards,
Michael, Ad Manager API Team



ref:_00D1U1174p._5001UCZZYP:ref
Reply all
Reply to author
Forward
0 new messages