工場

factories は、クライアント ライブラリを使用してオペレーションとリソースを作成するための高レベル インターフェースを提供します。

ファクトリ メソッドは、Google Ads API で提供されるすべてのリソース、列挙型、オペレーション、サービスタイプに対して自動的に生成されます。

運用

このライブラリには、Google Ads API と連携するオペレーションを簡単に作成するための client.operation.create_resource.<resource_type>client.operation.update_resource.<resource_type>client.operation.remove_resource.<resource_type> のコンビニエンス メソッドが用意されています。

リソースの作成例を次に示します。

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 の適切な create オペレーションが返されます。

同様に、以下の更新用の便利なメソッドも用意されています。

# 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]