Tạo và truy cập báo cáo được lập lịch

Bạn có thể tạo và lên lịch báo cáo bằng giao diện người dùng Google Display & Video 360 hoặc API. Ví dụ: người dùng cuối có thể thiết lập báo cáo hằng ngày cho biết số lượt hiển thị của ngày hôm qua hoặc báo cáo hằng tháng cho biết tổng mức chi tiêu.

Tạo báo cáo định kỳ

Tạo báo cáo định kỳ mới trong giao diện báo cáo của Display & Video 360 hoặc bằng phương thức API queries.create. Bạn có thể xem toàn bộ thông tin chi tiết về chức năng báo cáo của Display & Video 360 và cách tạo báo cáo mới trên trang web hỗ trợ của Display & Video 360.

Bạn có thể lên lịch báo cáo tại thời điểm tạo bằng cách sử dụng trường Repeats trong giao diện người dùng và trường schedule trong API. Bạn nên đặt các trường này thành một khoảng thời gian thích hợp.

Sau khi bạn đặt trường thích hợp, báo cáo này sẽ chạy theo lịch đó và sau đó, bạn có thể tải các báo cáo đã tạo xuống.

Truy cập vào Báo cáo định kỳ

Báo cáo định kỳ được tạo dưới dạng tệp CSV đơn giản và được lưu trữ tự động cũng như an toàn trong Google Cloud Storage. Tuy nhiên, bạn không thể truy cập trực tiếp vào các vùng chứa Google Cloud Storage chứa những tệp này. Thay vào đó, bạn có thể tải các tệp xuống theo cách thủ công từ giao diện người dùng Display & Video 360 hoặc truy xuất theo phương thức lập trình bằng cách sử dụng URL được uỷ quyền trước lấy từ API.

Dưới đây là ví dụ về cách sử dụng API để tải tệp báo cáo xuống. Trong ví dụ này, queries.reports.list được dùng với tham số orderBy để truy xuất báo cáo gần đây nhất theo một truy vấn. Sau đó, trường Report.reportMetadata.googleCloudStoragePath được dùng để xác định vị trí tệp báo cáo và tải nội dung của tệp đó xuống một tệp CSV cục bộ.

Java

Các dữ liệu nhập bắt buộc:

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;

Ví dụ về mã:

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

Các dữ liệu nhập bắt buộc:

from contextlib import closing
from six.moves.urllib.request import urlopen

Ví dụ về mã:

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);
}