建立及存取定期執行報表

您可以透過 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);
}