ข้อเท็จจริง

factories มีอินเทอร์เฟซระดับสูงสำหรับการสร้างการดำเนินการและ ทรัพยากรด้วยไลบรารีของไคลเอ็นต์

ระบบจะสร้างเมธอด Factory โดยอัตโนมัติสำหรับทรัพยากร, Enum, การดำเนินการ และประเภทบริการทั้งหมดที่ 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

Enum

ใช้ไวยากรณ์สัญลักษณ์เพื่อตั้งค่าฟิลด์ enum แบบคงที่ (เช่น campaign.status = :PAUSED) หากต้องการแจงนับค่าที่ถูกต้องทั้งหมดสำหรับ enum ให้ใช้ client.enum factory:

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 เวอร์ชันหลักอย่างชัดเจนในการเรียก Factory ใดก็ได้ (เช่น v25; การเผยแพร่ API เวอร์ชันย่อย เช่น v25.1 จะใช้วิธีเวอร์ชันหลัก v25)

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