Bid Manager API 1.1 版已淘汰,將於 2023 年 4 月 27 日停用。

遷移至 v2 以避免服務中斷。請參閱遷移指南,瞭解遷移至 v2 的步驟。

建立及存取定期執行報表

透過集合功能整理內容 你可以依據偏好儲存及分類內容。

您可以使用 Google Display &Video 360 UI 或 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 UI 手動下載,或透過 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);
}