您可以使用 Google Display & Video 360 界面或 API 创建并安排定期生成报告。例如,最终用户可以设置显示昨天的展示次数的每日报告,或显示总支出的每月报告。
创建定期生成的报表
在 Display & Video 360 报告界面中或使用 queries.create
API 方法创建新的定期生成报告。如需详细了解 Display & Video 360 的报告功能以及如何创建新报告,请访问 Display & Video 360 支持网站。
您可以在创建报告时使用界面中的 Repeats
字段和 API 中的 schedule
字段安排定期生成报告。这些字段应设置为适当的时间间隔。
设置适当的字段后,此报告将按该时间表运行,随后生成的报告便可供下载。
访问定期生成的报告
定期生成的报告是作为简单的 CSV 文件生成的,会自动安全地存储在 Google Cloud Storage 中。但是,您无法直接访问包含这些文件的 Google Cloud Storage 存储分区。不过,您可以从 Display & Video 360 界面手动下载这些文件,也可以使用从 API 中获取的预授权网址以编程方式检索这些文件。
下面的示例展示了如何使用 API 下载报告文件。在此示例中,将 queries.reports.list
与 orderBy
参数搭配使用,以检索某个查询下的最新报告。然后,使用 Report.reportMetadata.googleCloudStoragePath
字段查找报告文件并将其内容下载到本地 CSV 文件中。
Java
必需的导入:
import com.google.api.client.googleapis.media.MediaHttpDownloader; import com.google.api.client.googleapis.util.Utils; import com.google.api.client.http.GenericUrl; import com.google.api.services.doubleclickbidmanager.DoubleClickBidManager; import com.google.api.services.doubleclickbidmanager.model.ListReportsResponse; import com.google.api.services.doubleclickbidmanager.model.Report; import java.io.FileOutputStream; import java.io.OutputStream;
代码示例:
long queryId = query-id; // Call the API, listing the reports under the given queryId from most to // least recent. ListReportsResponse reportListResponse = service .queries() .reports() .list(queryId) .setOrderBy("key.reportId desc") .execute(); // Iterate over returned reports, stopping once finding a report that // finished generating successfully. Report mostRecentReport = null; if (reportListResponse.getReports() != null) { for (Report report : reportListResponse.getReports()) { if (report.getMetadata().getStatus().getState().equals("DONE")) { mostRecentReport = report; break; } } } else { System.out.format("No reports exist for query Id %s.\n", queryId); } // Download report file of most recent finished report found. if (mostRecentReport != null) { // Retrieve GCS URL from report object. GenericUrl reportUrl = new GenericUrl(mostRecentReport.getMetadata().getGoogleCloudStoragePath()); // Build filename. String filename = mostRecentReport.getKey().getQueryId() + "_" + mostRecentReport.getKey().getReportId() + ".csv"; // Download the report file. try (OutputStream output = new FileOutputStream(filename)) { MediaHttpDownloader downloader = new MediaHttpDownloader(Utils.getDefaultTransport(), null); downloader.download(reportUrl, output); } System.out.format("Download of file %s complete.\n", filename); } else { System.out.format( "There are no completed report files to download for query Id %s.\n", queryId); }
Python
必需的导入:
from contextlib import closing from six.moves.urllib.request import urlopen
代码示例:
query_id = query-id # Call the API, listing the reports under the given queryId from most to # least recent. response = service.queries().reports().list(queryId=query_id, orderBy="key.reportId desc").execute() # Iterate over returned reports, stopping once finding a report that # finished generating successfully. most_recent_report = None if response['reports']: for report in response['reports']: if report['metadata']['status']['state'] == 'DONE': most_recent_report = report break else: print('No reports exist for query Id %s.' % query_id) # Download report file of most recent finished report found. if most_recent_report != None: # Retrieve GCS URL from report object. report_url = most_recent_report['metadata']['googleCloudStoragePath'] # Build filename. output_file = '%s_%s.csv' % (report['key']['queryId'], report['key']['reportId']) # Download the report file. with open(output_file, 'wb') as output: with closing(urlopen(report_url)) as url: output.write(url.read()) print('Download of file %s complete.' % output_file) else: print('There are no completed report files to download for query Id %s.' % query_id)
PHP
$queryId = query-id; // Call the API, listing the reports under the given queryId from most to // least recent. $optParams = array('orderBy' => "key.reportId desc"); $response = $service->queries_reports->listQueriesReports($queryId, $optParams); // Iterate over returned reports, stopping once finding a report that // finished generating successfully. $mostRecentReport = null; if (!empty($response->getReports())) { foreach ($response->getReports() as $report) { if ($report->metadata->status->state == "DONE") { $mostRecentReport = $report; break; } } } else { printf('<p>No reports exist for query ID %s.</p>', $queryId); } // Download report file of most recent finished report found. if ($mostRecentReport != null) { // Build filename. $filename = $mostRecentReport->key->queryId . '_' . $mostRecentReport->key->reportId . '.csv'; // Download the report file. file_put_contents($filename, fopen($mostRecentReport->metadata->googleCloudStoragePath, 'r')); printf('<p>Download of file %s complete.</p>', $filename); } else { printf('<p>There are no completed report files to download for query Id %s.</p>', $queryId); }