|
| 1 | +#!/usr/bin/env ruby |
| 2 | +# Encoding: utf-8 |
| 3 | +# |
| 4 | +# Copyright 2025 Google LLC |
| 5 | +# |
| 6 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +# you may not use this file except in compliance with the License. |
| 8 | +# You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, software |
| 13 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + |
| 15 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | +# See the License for the specific language governing permissions and |
| 17 | +# limitations under the License. |
| 18 | +# This example shows how to create a Demand Gen campaign with a video ad. |
| 19 | +# |
| 20 | +# For more information about Demand Gen campaigns, see |
| 21 | +# https://developers.google.com/google-ads/api/docs/demand-gen/overview |
| 22 | + |
| 23 | +require 'optparse' |
| 24 | +require 'google/ads/google_ads' |
| 25 | +require 'date' |
| 26 | +require 'open-uri' |
| 27 | + |
| 28 | +# Temporary IDs for resources. |
| 29 | +BUDGET_TEMPORARY_ID = -1 |
| 30 | +CAMPAIGN_TEMPORARY_ID = -2 |
| 31 | +AD_GROUP_TEMPORARY_ID = -3 |
| 32 | +VIDEO_ASSET_TEMPORARY_ID = -4 |
| 33 | +LOGO_ASSET_TEMPORARY_ID = -5 |
| 34 | + |
| 35 | +# URLs for assets |
| 36 | +DEFAULT_LOGO_IMAGE_URL = "https://gaagl.page.link/bjYi" |
| 37 | +DEFAULT_FINAL_URL = "http://example.com" |
| 38 | + |
| 39 | +def add_demand_gen_campaign(customer_id, video_id) |
| 40 | + client = Google::Ads::GoogleAds::GoogleAdsClient.new |
| 41 | + |
| 42 | + budget_resource_name = client.path.campaign_budget(customer_id, BUDGET_TEMPORARY_ID) |
| 43 | + campaign_resource_name = client.path.campaign(customer_id, CAMPAIGN_TEMPORARY_ID) |
| 44 | + ad_group_resource_name = client.path.ad_group(customer_id, AD_GROUP_TEMPORARY_ID) |
| 45 | + video_asset_resource_name = client.path.asset(customer_id, VIDEO_ASSET_TEMPORARY_ID) |
| 46 | + logo_asset_resource_name = client.path.asset(customer_id, LOGO_ASSET_TEMPORARY_ID) |
| 47 | + |
| 48 | + # [START add_demand_gen_campaign_1] |
| 49 | + operations = [] |
| 50 | + |
| 51 | + operations << client.operation.mutate do |m| |
| 52 | + m.campaign_budget_operation = create_campaign_budget_operation(client, budget_resource_name) |
| 53 | + end |
| 54 | + |
| 55 | + operations << client.operation.mutate do |m| |
| 56 | + m.campaign_operation = create_demand_gen_campaign_operation(client, campaign_resource_name, budget_resource_name) |
| 57 | + end |
| 58 | + |
| 59 | + operations << client.operation.mutate do |m| |
| 60 | + m.ad_group_operation = create_ad_group_operation(client, ad_group_resource_name, campaign_resource_name) |
| 61 | + end |
| 62 | + |
| 63 | + operations += create_asset_operations(client, video_asset_resource_name, video_id, logo_asset_resource_name).map do |asset_op| |
| 64 | + client.operation.mutate do |m| |
| 65 | + m.asset_operation = asset_op |
| 66 | + end |
| 67 | + end |
| 68 | + |
| 69 | + operations << client.operation.mutate do |m| |
| 70 | + m.ad_group_ad_operation = create_demand_gen_ad_operation(client, ad_group_resource_name, video_asset_resource_name, logo_asset_resource_name) |
| 71 | + end |
| 72 | + |
| 73 | + response = client.service.google_ads.mutate( |
| 74 | + customer_id: customer_id, |
| 75 | + mutate_operations: operations, |
| 76 | + ) |
| 77 | + # [END add_demand_gen_campaign_1] |
| 78 | + |
| 79 | + puts "Created campaign with resource name '#{response.mutate_operation_responses.first.campaign_result.resource_name}'" |
| 80 | +end |
| 81 | + |
| 82 | +def create_campaign_budget_operation(client, budget_resource_name) |
| 83 | + # Creates a campaign budget. |
| 84 | + client.operation.create_resource.campaign_budget do |cb| |
| 85 | + cb.name = "Demand Gen campaign budget #{Time.now.to_f}" |
| 86 | + # The budget period already defaults to DAILY. |
| 87 | + cb.amount_micros = 50_000_000 |
| 88 | + cb.delivery_method = :STANDARD |
| 89 | + |
| 90 | + # A Demand Gen campaign cannot use a shared campaign budget. |
| 91 | + cb.explicitly_shared = false |
| 92 | + |
| 93 | + # Set a temporary ID in the budget's resource name so it can be referenced |
| 94 | + # by the campaign in later steps. |
| 95 | + cb.resource_name = budget_resource_name |
| 96 | + end |
| 97 | +end |
| 98 | + |
| 99 | +# [START add_demand_gen_campaign_2] |
| 100 | +def create_demand_gen_campaign_operation(client, campaign_resource_name, budget_resource_name) |
| 101 | + client.operation.create_resource.campaign do |c| |
| 102 | + c.name = "Demand Gen ##{Time.now.to_f}" |
| 103 | + |
| 104 | + # Recommendation: Set the campaign to PAUSED when creating it to |
| 105 | + # prevent the ads from immediately serving. Set to ENABLED once you've |
| 106 | + # added targeting and the ads are ready to serve. |
| 107 | + c.status = :PAUSED |
| 108 | + |
| 109 | + # AdvertisingChannelType must be DEMAND_GEN. |
| 110 | + c.advertising_channel_type = :DEMAND_GEN |
| 111 | + |
| 112 | + # Assign the resource name with a temporary ID. |
| 113 | + c.resource_name = campaign_resource_name |
| 114 | + |
| 115 | + # Set the budget using the given budget resource name. |
| 116 | + c.campaign_budget = budget_resource_name |
| 117 | + |
| 118 | + # Use the Target CPA bidding strategy. |
| 119 | + c.bidding_strategy_type = :TARGET_CPA |
| 120 | + c.target_cpa = client.resource.target_cpa do |tc| |
| 121 | + tc.target_cpa_micros = 1_000_000 |
| 122 | + end |
| 123 | + |
| 124 | + # Declare whether or not this campaign serves political ads targeting the EU. |
| 125 | + # Valid values are CONTAINS_EU_POLITICAL_ADVERTISING and |
| 126 | + # DOES_NOT_CONTAIN_EU_POLITICAL_ADVERTISING. |
| 127 | + c.contains_eu_political_advertising = :DOES_NOT_CONTAIN_EU_POLITICAL_ADVERTISING |
| 128 | + |
| 129 | + # Optional fields |
| 130 | + c.start_date = DateTime.parse((Date.today + 1).to_s).strftime('%Y%m%d') |
| 131 | + c.end_date = DateTime.parse(Date.today.next_year.to_s).strftime('%Y%m%d') |
| 132 | + end |
| 133 | +end |
| 134 | +# [END add_demand_gen_campaign_2] |
| 135 | + |
| 136 | +# [START add_demand_gen_campaign_3] |
| 137 | +def create_ad_group_operation(client, ad_group_resource_name, campaign_resource_name) |
| 138 | + # Creates an ad group. |
| 139 | + client.operation.create_resource.ad_group do |ag| |
| 140 | + ag.resource_name = ad_group_resource_name |
| 141 | + ag.name = "Earth to Mars Cruises ##{Time.now.to_f}" |
| 142 | + ag.status = :ENABLED |
| 143 | + ag.campaign = campaign_resource_name |
| 144 | + |
| 145 | + # [START add_demand_gen_campaign_5] |
| 146 | + # Select the specific channels for the ad group. |
| 147 | + # For further information on Demand Gen channel controls, see |
| 148 | + # https://developers.google.com/google-ads/api/docs/demand-gen/channel-controls |
| 149 | + ag.demand_gen_ad_group_settings = client.resource.demand_gen_ad_group_settings do |dgas| |
| 150 | + dgas.channel_controls = client.resource.demand_gen_channel_controls do |dcc| |
| 151 | + dcc.selected_channels = client.resource.demand_gen_selected_channels do |dsc| |
| 152 | + dsc.gmail = false |
| 153 | + dsc.discover = false |
| 154 | + dsc.display = false |
| 155 | + dsc.youtube_in_feed = true |
| 156 | + dsc.youtube_in_stream = true |
| 157 | + dsc.youtube_shorts = true |
| 158 | + end |
| 159 | + end |
| 160 | + end |
| 161 | + # [END add_demand_gen_campaign_5] |
| 162 | + end |
| 163 | +end |
| 164 | +# [END add_demand_gen_campaign_3] |
| 165 | + |
| 166 | +def create_asset_operations(client, video_asset_resource_name, video_id, logo_asset_resource_name) |
| 167 | + [ |
| 168 | + create_video_asset_operation(client, video_asset_resource_name, video_id, "Video"), |
| 169 | + create_image_asset_operation(client, logo_asset_resource_name, DEFAULT_LOGO_IMAGE_URL, "Square Marketing Image"), |
| 170 | + ] |
| 171 | +end |
| 172 | + |
| 173 | +# [START add_demand_gen_campaign_4] |
| 174 | +def create_demand_gen_ad_operation(client, ad_group_resource_name, video_asset_resource_name, logo_asset_resource_name) |
| 175 | + client.operation.create_resource.ad_group_ad do |aga| |
| 176 | + aga.ad_group = ad_group_resource_name |
| 177 | + aga.status = :ENABLED |
| 178 | + aga.ad = client.resource.ad do |ad| |
| 179 | + ad.name = "Demand gen video responsive ad" |
| 180 | + ad.final_urls << DEFAULT_FINAL_URL |
| 181 | + ad.demand_gen_video_responsive_ad = client.resource.demand_gen_video_responsive_ad_info do |dgv| |
| 182 | + dgv.business_name = client.resource.ad_text_asset do |ata| |
| 183 | + ata.text = "Interplanetary Cruises" |
| 184 | + end |
| 185 | + dgv.videos << client.resource.ad_video_asset do |ava| |
| 186 | + ava.asset = video_asset_resource_name |
| 187 | + end |
| 188 | + dgv.logo_images << client.resource.ad_image_asset do |aia| |
| 189 | + aia.asset = logo_asset_resource_name |
| 190 | + end |
| 191 | + dgv.headlines << client.resource.ad_text_asset do |ata| |
| 192 | + ata.text = "Interplanetary cruises" |
| 193 | + end |
| 194 | + dgv.long_headlines << client.resource.ad_text_asset do |ata| |
| 195 | + ata.text = "Travel the World" |
| 196 | + end |
| 197 | + dgv.descriptions << client.resource.ad_text_asset do |ata| |
| 198 | + ata.text = "Book now for an extra discount" |
| 199 | + end |
| 200 | + end |
| 201 | + end |
| 202 | + end |
| 203 | +end |
| 204 | +# [END add_demand_gen_campaign_4] |
| 205 | + |
| 206 | +def create_image_asset_operation(client, asset_resource_name, url, asset_name) |
| 207 | + client.operation.create_resource.asset do |a| |
| 208 | + a.resource_name = asset_resource_name |
| 209 | + a.name = asset_name |
| 210 | + a.type = :IMAGE |
| 211 | + a.image_asset = client.resource.image_asset do |ia| |
| 212 | + ia.data = URI.open(url) { |f| f.read } |
| 213 | + end |
| 214 | + end |
| 215 | +end |
| 216 | + |
| 217 | +def create_video_asset_operation(client, asset_resource_name, video_id, asset_name) |
| 218 | + client.operation.create_resource.asset do |a| |
| 219 | + a.resource_name = asset_resource_name |
| 220 | + a.name = asset_name |
| 221 | + a.type = :YOUTUBE_VIDEO |
| 222 | + a.youtube_video_asset = client.resource.youtube_video_asset do |yva| |
| 223 | + yva.youtube_video_id = video_id |
| 224 | + end |
| 225 | + end |
| 226 | +end |
| 227 | + |
| 228 | +if __FILE__ == $0 |
| 229 | + options = {} |
| 230 | + # The following parameter(s) should be provided to run the example. You can |
| 231 | + # either specify them here or provide them as positional arguments when |
| 232 | + # running the code. |
| 233 | + # |
| 234 | + # e.g. ruby add_demand_gen_campaign.rb -C YOUR_CUSTOMER_ID -V YOUR_VIDEO_ID |
| 235 | + OptionParser.new do |opts| |
| 236 | + opts.banner = "Usage: add_demand_gen_campaign.rb [options]" |
| 237 | + |
| 238 | + opts.on("-C", "--customer_id CUSTOMER-ID", "Customer ID") do |v| |
| 239 | + options[:customer_id] = v |
| 240 | + end |
| 241 | + |
| 242 | + opts.on("-V", "--video_id VIDEO-ID", "Video ID") do |v| |
| 243 | + options[:video_id] = v |
| 244 | + end |
| 245 | + end.parse! |
| 246 | + |
| 247 | + begin |
| 248 | + add_demand_gen_campaign( |
| 249 | + options.fetch(:customer_id).tr("-", ""), |
| 250 | + options.fetch(:video_id), |
| 251 | + ) |
| 252 | + rescue Google::Ads::GoogleAds::Errors::GoogleAdsError => e |
| 253 | + e.failure.errors.each do |error| |
| 254 | + STDERR.printf("Error with message: %s\n", error.message) |
| 255 | + if error.location |
| 256 | + error.location.field_path_elements.each do |field_path_element| |
| 257 | + STDERR.printf("\tOn field: %s\n", field_path_element.field_name) |
| 258 | + end |
| 259 | + end |
| 260 | + end |
| 261 | + raise |
| 262 | + end |
| 263 | +end |
| 264 | + |
0 commit comments