工廠

factories 提供高階介面,可使用用戶端程式庫建立作業和資源。

系統會自動為 Google Ads API 提供的所有資源、列舉、作業和服務類型產生工廠方法。本頁的範例假設您已初始化 GoogleAdsClient 例項和目標 customer_id:

require 'google/ads/google_ads'

client = Google::Ads::GoogleAds::GoogleAdsClient.new
customer_id = '1234567890'

作業

程式庫提供 client.operation.create_resource.<resource_type>、client.operation.update_resource.<resource_type> 和 client.operation.remove_resource.<resource_type> 等便利方法,可建構作業來使用 Google Ads API。

以下範例會建立廣告活動預算資源:

campaign_budget_operation =
  client.operation.create_resource.campaign_budget do |budget|
    budget.name = "Interplanetary Budget #{(Time.new.to_f * 1000).to_i}"
    budget.delivery_method = :STANDARD
    budget.amount_micros = 500_000
  end

return_budget = client.service.campaign_budget.mutate_campaign_budgets(
  customer_id: customer_id,
  operations: [campaign_budget_operation]
)

請注意,產生至區塊 (budget) 的物件是 CampaignBudget 的新例項,您可以變動該例項,並傳回 CampaignBudgetService 的適當建立作業。

同樣地,程式庫也提供更新資源的便利方法:

campaign_service = client.service.campaign

# If you only have a resource name:
update_operation =
  client.operation.update_resource.campaign(campaign_resource_name) do |camp|
    camp.status = :PAUSED
  end

campaign_service.mutate_campaigns(
  customer_id: customer_id,
  operations: [update_operation]
)

# If you have a full resource proto:
update_operation = client.operation.update_resource.campaign(campaign) do |camp|
  camp.name = "A different Cruise #{(Time.new.to_f * 1000).to_i}"
end

campaign_service.mutate_campaigns(
  customer_id: customer_id,
  operations: [update_operation]
)

這些呼叫會傳回格式正確的更新作業,並預先填入欄位遮罩,以便在 Google Ads API 中更新資源。

以下範例會使用資源路徑移除資源:

campaign_service = client.service.campaign
remove_operation =
  client.operation.remove_resource.campaign(campaign_resource_name)
campaign_service.mutate_campaigns(
  customer_id: customer_id,
  operations: [remove_operation]
)

如要自行處理作業,可以取得原始作業,然後手動填入欄位:

operation = client.operation.campaign

資源

程式庫提供 client.resource.<resource_type>,可初始化資源物件:

campaign.network_settings = client.resource.network_settings do |ns|
  ns.target_google_search = true
  ns.target_search_network = true
  ns.target_content_network = false
  ns.target_partner_search_network = false
end

系統會將所要求資源類型的新例項,產生至傳遞的區塊,以設定欄位。

服務

程式庫提供 client.service.<service_name>,可擷取服務用戶端物件:

campaign_service = client.service.campaign

列舉

使用符號語法靜態設定列舉欄位 (例如 campaign.status = :PAUSED)。如要列舉列舉的所有有效值,請使用 client.enum 工廠:

client.enum.ad_type.each { |ad_type| p ad_type }
# Output:
# :UNSPECIFIED
# :UNKNOWN
# :TEXT_AD
# :EXPANDED_TEXT_AD
# :EXPANDED_DYNAMIC_SEARCH_AD
# :HOTEL_AD
# :SHOPPING_SMART_AD
# :SHOPPING_PRODUCT_AD
# :VIDEO_AD
# :IMAGE_AD
# :RESPONSIVE_SEARCH_AD
# :LEGACY_RESPONSIVE_DISPLAY_AD
# :APP_AD
# :LEGACY_APP_INSTALL_AD
# :RESPONSIVE_DISPLAY_AD
# :LOCAL_AD
# :HTML5_UPLOAD_AD
# :DYNAMIC_HTML5_AD
# :APP_ENGAGEMENT_AD
# :SHOPPING_COMPARISON_LISTING_AD
# :VIDEO_BUMPER_AD
# :VIDEO_NON_SKIPPABLE_IN_STREAM_AD
# :VIDEO_TRUEVIEW_IN_STREAM_AD
# :VIDEO_RESPONSIVE_AD
# :SMART_CAMPAIGN_AD
# :CALL_AD
# :APP_PRE_REGISTRATION_AD
# :IN_FEED_VIDEO_AD
# :DEMAND_GEN_MULTI_ASSET_AD
# :DEMAND_GEN_CAROUSEL_AD
# :TRAVEL_AD
# :DEMAND_GEN_VIDEO_RESPONSIVE_AD
# :DEMAND_GEN_PRODUCT_AD
# :YOUTUBE_AUDIO_AD

明確設定 Google Ads API 版本

您也可以在任何工廠呼叫中明確設定主要 API 版本 (例如 v25;次要 API 版本 (例如 v25.1) 使用主要版本方法 v25):

client.resource.v25.[entity]
client.operation.v25.[operation]
client.service.v25.[service]
client.enum.v25.[enum]