スケジュール設定されたレポートの作成とアクセス

レポートの作成とスケジュール設定は、Google ディスプレイ&ビデオ 360 の UI または API を使用して行えます。たとえば、エンドユーザーは、前日のインプレッション数を示す日次レポートや、合計費用を示す月次レポートを設定できます。

スケジュール設定レポートを作成する

ディスプレイ&ビデオ 360 のレポート インターフェースまたは queries.create API メソッドを使用して、新しいスケジュール設定レポートを作成します。ディスプレイ&ビデオ 360 のレポート機能と新しいレポートの作成方法について詳しくは、ディスプレイ&ビデオ 360 のサポートサイトをご覧ください。

レポートは、UI の Repeats フィールドと API の schedule フィールドを使用して、作成時にスケジュール設定できます。これらのフィールドは適切な時間間隔に設定する必要があります。

適切なフィールドが設定されると、このレポートはそのスケジュールで実行され、生成されたレポートをダウンロードできるようになります。

スケジュール設定レポートにアクセスする

スケジュール設定されたレポートは、シンプルな CSV ファイルとして生成され、Google Cloud Storage に自動的かつ安全に保存されます。ただし、これらのファイルを含む Google Cloud Storage バケットに直接アクセスすることはできません。代わりに、ディスプレイ&ビデオ 360 の管理画面から手動でファイルをダウンロードするか、API から取得した事前承認済み URL を使用してプログラムでファイルを取得します。

API を使用してレポート ファイルをダウンロードする例を以下に示します。この例では、queries.reports.listorderBy パラメータとともに使用され、クエリで最新のレポートを取得しています。次に、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);
}