建立查詢

Bid Manager API Query 資源代表您在 UI 中看到的內容 多媒體和「影片 360」報表。執行這項查詢以產生 Report 資源。如果成功,Report 資源會提供 可以下載產生的報表

本頁說明如何建構及建立 Query 資源。

選擇篩選器、維度和指標

請在每個 Query 中,提供下列 Parameters 值: params 欄位可讓您調整報表傳回的資料:

  • filters 會限制資料範圍。篩選器會根據 和 FilterPair 物件的結構,其中 typeFilter 列舉, value 是識別字串,例如相關 資源 ID
  • groupBys 會定義報表的維度。 由 Filter 列舉清單表示。
  • metrics 會定義要填入報表的資料。 以指標列舉清單表示。

如要進一步瞭解 Query 結構,請參閱參考資料 說明文件

選擇這些值時,請考量您的 ReportType, 這項資訊會顯示在 type 欄位中。您選取的指標和篩選條件 值必須與報表類型相容 以便順利產生

建立查詢

決定報表的核心結構後,您就可以建立查詢。

以下說明如何製作內含過去七天資料的臨時報表 天:

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