factories
provides a high-level interface for creating operations and resources
with the client library.
Factories methods are automatically generated for all resources, enums, operations, and service types provided by the Google Ads API.
Operations
The library provides client.operation.create_resource.<resource_type>
,
client.operation.update_resource.<resource_type>
, and
client.operation.remove_resource.<resource_type>
convenience methods to easily create operations to work with the Google Ads API.
Here is an example for creating a resource:
campaign_budget_operation = client.operation.create_resource.campaign_budget do |cb|
cb.name = "Interplanetary Budget #{(Time.new.to_f * 1000).to_i}"
cb.delivery_method = :STANDARD
cb.amount_micros = 500000
end
return_budget = client.service.campaign_budget.mutate_campaign_budgets(
customer_id,
[campaign_budget_operation]
)
Note that the object yielded to the block cb
is a new instance of
CampaignBudget
that you can then mutate, and the appropriate create operation for the
CampaignBudgetService
is returned.
Similarly, we provide convenience methods for updating:
# 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 = "A different interplanetary Cruise #{(Time.new.to_f * 1000).to_i}"
end
campaign_service.mutate_campaigns(customer_id, [update_operation])
These calls return a well-formed update operation, with a prepopulated field mask to update the resource in the Google Ads API.
Here is an example of removing a resource using a resource path:
remove_operation = client.operation.remove_resource.campaign(campaign_resource_name)
campaign_service.mutate_campaigns(customer_id, [remove_operation])
If you'd prefer to work with the operation yourself, you can get a raw operation and then manually populate the fields.
operation = client.operation.campaign
Resources
The library provides client.resource.<resource_type>
as a convenient way to
initialize resource objects:
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
A new instance of the resource type requested is yielded to the passed block for setting fields.
Services
The library provides client.service.<service_name>
as a convenient way to
get service objects:
campaign_service = client.service.campaign
Enums
We recommend using the symbol syntax for statically setting enum fields
(e.g., campaign.status = :PAUSED
). However, if you want to enumerate all the
valid values for an enum, we also provide methods for that:
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
Explicitly setting Google Ads API versions
You can also explicitly set a version:
client.resource.v18.[entity]
client.operation.v18.[operation]
client.service.v18.[service]
client.enum.v18.[enum]