factories
提供高階介面,讓您透過用戶端程式庫建立作業和資源。
系統會自動為 Google Ads API 提供的所有資源、列舉、作業和服務類型產生 Factories 方法。
作業套件
這個程式庫提供 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 |cb|
cb.name = client.wrapper.string(
"Interplanetary Budget #{(Time.new.to_f * 1000).to_i}",
)
cb.delivery_method = :STANDARD
cb.amount_micros = client.wrapper.int64(500000)
end
return_budget = client.service.campaign_budget.mutate_campaign_budgets(
customer_id,
[campaign_budget_operation]
)
請注意,產生至區塊 cb
的物件是 CampaignBudget
的新執行個體,您可以接著進行變更,然後傳回 CampaignBudgetService
的適當建立作業。
同樣地,我們也提供便利的更新方法:
# 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, [update_operation])
# if you have a full resource proto
update_operation = client.operation.update_resource.campaign(campaign) do
campaign.name = client.wrapper.string(
"A different interplanetary Cruise #{(Time.new.to_f * 1000).to_i}",
)
end
campaign_service.mutate_campaigns(customer_id, [update_operation])
這些呼叫會傳回格式正確的更新作業,其中包含預先填入的欄位遮罩,以更新 Google Ads API 中的資源。
以下示範如何使用資源路徑移除資源:
remove_operation = client.operation.remove_resource.campaign(campaign_resource_name)
campaign_service.mutate_campaigns(customer_id, [remove_operation])
如果您希望自行操作,可以取得原始作業,然後手動填入欄位。
operation = client.operation.campaign
資源
程式庫提供 client.resource.<resource_type>
,方便您初始化資源物件:
campaign.network_settings = client.resource.network_settings do |ns|
ns.target_google_search = client.wrapper.bool(true)
ns.target_search_network = client.wrapper.bool(true)
ns.target_content_network = client.wrapper.bool(false)
ns.target_partner_search_network = client.wrapper.bool(false)
end
系統會將要求的資源類型的新例項計入所傳遞的設定欄位。
服務
程式庫提供 client.service.<service_name>
,方便您取得服務物件:
campaign_service = client.service.campaign
列舉
我們建議您使用符號語法來靜態設定列舉欄位 (例如campaign.status = :PAUSED
)。不過,如果您想要列舉某列舉的所有有效值,也可以使用我們提供的方法:
client.enum.ad_type.each { |x| p x }
:SHOPPING_PRODUCT_AD
:GMAIL_AD
:UNKNOWN
:UNSPECIFIED
:CALL_ONLY_AD
:VIDEO_AD
:IMAGE_AD
:EXPANDED_DYNAMIC_SEARCH_AD
:RESPONSIVE_DISPLAY_AD
:TEXT_AD
:LEGACY_RESPONSIVE_DISPLAY_AD
:LEGACY_APP_INSTALL_AD
:APP_AD
:SHOPPING_SMART_AD
:EXPANDED_TEXT_AD
:HOTEL_AD
:RESPONSIVE_SEARCH_AD
明確設定 Google Ads API 版本
您也可以明確設定版本:
client.resource.v16.[entity]
client.operation.v16.[operation]
client.service.v16.[service]
client.enum.v16.[enum]