创建查询

Bid Manager API Query 资源表示您在界面中看到的内容, 展示广告和Video 360 报告。运行此查询以生成 Report 资源。如果成功,Report 资源会提供其中 您可以下载生成的报告

本页介绍了如何构建和创建 Query 资源。

选择过滤条件、维度和指标

在每个Query中,Parameters params 字段,用于优化报告中返回的数据:

  • filters 用于限制数据范围。过滤器使用列表 一组 FilterPair 对象,其中 type 是一个过滤器枚举,而 value 是一个标识字符串,如相关的 资源 ID。
  • groupBys 定义报告的维度, 由 Filter 枚举列表表示。
  • metrics 定义用于填充报告的数据, 由一系列 Metric 枚举表示。

如需详细了解 Query 结构,请参阅参考文档 文档

选择这些值时,请考虑您的ReportType、 (在 type 字段中设置)。您选择的指标和过滤条件 值必须与报告的类型兼容 才能成功生成

创建查询

确定报告的核心结构后,您就可以创建查询了。

下面介绍了如何创建包含过去 7 天内数据的临时报告 天:

Java

// Display name of the query to create.
String displayName = display-name;

// The report type.
String reportType = report-type;

// The advertisers and campaigns by which to filter report data.
List<String> advertiserIdFilters =
    Arrays.asList(advertiser-id,...);
List<String> campaignIdFilters =
    Arrays.asList(campaign-id,...);

// The dimensions and metrics for the report.
List<String> dimensions = Arrays.asList(dimension,...);
List<String> metrics = Arrays.asList(metric,...);

// Build a list of filter pairs from given IDs.
List<FilterPair> filters = new ArrayList<FilterPair>();
if (advertiserIdFilters != null) {
  for (String advertiserId : advertiserIdFilters) {
    filters.add(
        new FilterPair().setType("FILTER_ADVERTISER").setValue(advertiserId));
  }
}
if (campaignIdFilters != null) {
  for (String campaignId : campaignIdFilters) {
    filters.add(
        new FilterPair().setType("FILTER_MEDIA_PLAN").setValue(campaignId));
  }
}

// Create the query structure.
Query query = new Query();

// Build and set the metadata object.
QueryMetadata metadata = new QueryMetadata();
metadata.setTitle(displayName);
metadata.setDataRange(new DataRange().setRange("LAST_7_DAYS"));
metadata.setFormat("CSV");
query.setMetadata(metadata);

// Build the parameters object.
Parameters parameters = new Parameters();
parameters.setType(reportType);
parameters.setGroupBys(dimensions);
parameters.setFilters(filters);
parameters.setMetrics(metrics);

// Set parameters object in query.
query.setParams(parameters);

// Build and set the schedule object.
QuerySchedule schedule = new QuerySchedule();
schedule.setFrequency("ONE_TIME");
query.setSchedule(schedule);

// Create the query.
Query queryResponse = service.queries().create(query).execute();

// Log query creation.
System.out.printf("Query %s was created.%n", queryResponse.getQueryId());

Python

# Display name of the query to create.
display_name = display-name

# The report type.
report_type = report-type

# The advertisers and campaigns by which to filter report data.
filtered_advertiser_ids = [advertiser-id,...]
filtered_campaign_ids = [campaign-id,...]

# The dimensions and metrics for the report.
dimensions = [dimension,...]
metrics = [metric,...]

# Build list of FilterPair objects.
filters = []
if filtered_campaign_ids != None:
  filters.extend([
    {"type": "FILTER_MEDIA_PLAN", "value": id}
    for id in filtered_campaign_ids
  ])
if filtered_advertiser_ids != None:
  filters.extend([
    {"type": "FILTER_ADVERTISER", "value": id}
    for id in filtered_advertiser_ids
  ])

# Build query object.
query_obj = {
  "metadata": {
    "title": display_name,
    "dataRange": {"range": "LAST_7_DAYS"},
    "format": "CSV",
  },
  "params": {
    "type": report_type,
    "groupBys": dimensions,
    "filters": filters,
    "metrics": metrics,
  },
  "schedule": {"frequency": "ONE_TIME"}
}

# Create query object.
query_response = service.queries().create(body=query_obj).execute()

# Print new query ID.
print(f'Query {query_response["queryId"]} was created.')

PHP

// Display name of the query to create.
$displayName = display-name;

// The report type.
$reportType = report-type;

  // Advertiser ID and campaign ID by which to filter data.
$advertiserIdFilters = array(advertiser-id,...);
$campaignIdFilters = array(campaign-id,...);

// The dimensions and metrics for the report.
$dimensions = array(dimension,...);
$metrics = array(metric,...);

// Build a list of filter pairs from given IDs.
$filters = array();
foreach ($advertiserIdFilters as $advertiserId) {
    $filterPair = new Google_Service_DoubleClickBidManager_FilterPair();
    $filterPair->setType("FILTER_ADVERTISER");
    $filterPair->setValue($advertiserId);
    array_push($filters, $filterPair);
}
foreach ($campaignIdFilters as $campaignId) {
    $filterPair = new Google_Service_DoubleClickBidManager_FilterPair();
    $filterPair->setType("FILTER_MEDIA_PLAN");
    $filterPair->setValue($campaignId);
    array_push($filters, $filterPair);
}

  // Create the query structure.
$query = new Google_Service_DoubleClickBidManager_Query();

  // Build and set the metadata object.
$metadata = new Google_Service_DoubleClickBidManager_QueryMetadata();
$metadata->setTitle($displayName);
$metadata->setFormat("CSV");

$dataRange = new Google_Service_DoubleClickBidManager_DataRange();
$dataRange->setRange("LAST_7_DAYS");
$metadata->setDataRange($dataRange);

$query->setMetadata($metadata);

  // Build and set the parameters object.
$parameters = new Google_Service_DoubleClickBidManager_Parameters();
$parameters->setType($reportType);
$parameters->setGroupBys($dimensions);
$parameters->setFilters($filters);
$parameters->setMetrics($metrics);
$query->setParams($parameters);

  // Build and set the schedule object.
$schedule = new Google_Service_DoubleClickBidManager_QuerySchedule();
$schedule->setFrequency("ONE_TIME");
$query->setSchedule($schedule);

// Call the API, creating the query.
$queryResult = $this->service->queries->create($query);

printf('Query %s was created.<br>', $queryResult->getQueryId());