From 5ffb6aa7802161a658a8180e0c4e240efbcec4b6 Mon Sep 17 00:00:00 2001 From: Daniel Rodriguez Date: Fri, 1 Dec 2023 10:05:00 -0600 Subject: [PATCH 1/2] Combine detect_and_apply_recommendations examples Change-Id: Ibe38e3a124318893ca4917e388c0ed9cf73a4f06 --- .../recommendations/apply_recommendation.rb | 120 ------------------ .../detect_and_apply_recommendations.rb | 64 ++++++++-- .../get_text_ad_recommendations.rb | 118 ----------------- 3 files changed, 50 insertions(+), 252 deletions(-) delete mode 100644 examples/recommendations/apply_recommendation.rb delete mode 100755 examples/recommendations/get_text_ad_recommendations.rb diff --git a/examples/recommendations/apply_recommendation.rb b/examples/recommendations/apply_recommendation.rb deleted file mode 100644 index 29733fab4..000000000 --- a/examples/recommendations/apply_recommendation.rb +++ /dev/null @@ -1,120 +0,0 @@ -#!/usr/bin/env ruby -# Encoding: utf-8 -# -# Copyright 2018 Google LLC -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# https://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# -# This example applies a given recommendation. To retrieve recommendations for -# text ads, run get_text_ad_recommendations.rb. - -require 'optparse' -require 'google/ads/google_ads' - -# [START apply_recommendation] -def apply_recommendation(customer_id, recommendation_id) - # GoogleAdsClient will read a config file from - # ENV['HOME']/google_ads_config.rb when called without parameters - client = Google::Ads::GoogleAds::GoogleAdsClient.new - - recommendation_resource = - client.path.recommendation(customer_id, recommendation_id) - apply_recommendation_operation = client.operation.apply_recommendation - apply_recommendation_operation.resource_name = recommendation_resource - - # Each recommendation type has optional parameters to override the recommended - # values. This is an example to override a recommended ad when a - # TextAdRecommendation is applied. - # For details, please read - # https://developers.google.com/google-ads/api/reference/rpc/google.ads.google_ads.v1.services#google.ads.google_ads.v1.services.ApplyRecommendationOperation - # - # text_ad_parameters = client.resource.text_ad_parameters do |tap| - # tap.ad = client.resource.ad do |ad| - # ad.id = "INSERT_AD_ID_AS_INTEGER_HERE" - # end - # end - # apply_recommendation_operation.text_ad = text_ad_parameters - - # Issues a mutate request to apply the recommendation. - recommendation_service = client.service.recommendation - response = recommendation_service.apply_recommendation( - customer_id: customer_id, - operations: [apply_recommendation_operation], - ) - applied_recommendation = response.results.first - - puts "Applied recommendation with resource name: '#{applied_recommendation.resource_name}'." -end -# [END apply_recommendation] - -if __FILE__ == $0 - PAGE_SIZE = 1000 - - options = {} - # The following parameter(s) should be provided to run the example. You can - # either specify these by changing the INSERT_XXX_ID_HERE values below, or on - # the command line. - # - # Parameters passed on the command line will override any parameters set in - # code. - # - # Running the example with -h will print the command line usage. - options[:customer_id] = 'INSERT_CUSTOMER_ID_HERE' - # Recommendation ID is the last alphanumeric portion of the value from the - # resource_name field of a Recommendation, which has the format of - # customers//recommendations/. - # Its example can be retrieved from get_text_ad_recommendations.rb. - options[:recommendation_id] = 'INSERT_RECOMMENDATION_ID_HERE' - - OptionParser.new do |opts| - opts.banner = sprintf('Usage: %s [options]', File.basename(__FILE__)) - - opts.separator '' - opts.separator 'Options:' - - opts.on('-C', '--customer-id CUSTOMER-ID', String, 'Customer ID') do |v| - options[:customer_id] = v - end - - opts.on('-r', '--recommendation-id RECOMMENDATION-ID', String, - 'Recommendation ID') do |v| - options[:recommendation_id] = v - end - - opts.separator '' - opts.separator 'Help:' - - opts.on_tail('-h', '--help', 'Show this message') do - puts opts - exit - end - end.parse! - - begin - apply_recommendation(options.fetch(:customer_id).tr("-", ""), options[:recommendation_id]) - rescue Google::Ads::GoogleAds::Errors::GoogleAdsError => e - e.failure.errors.each do |error| - STDERR.printf("Error with message: %s\n", error.message) - if error.location - error.location.field_path_elements.each do |field_path_element| - STDERR.printf("\tOn field: %s\n", field_path_element.field_name) - end - end - error.error_code.to_h.each do |k, v| - next if v == :UNSPECIFIED - STDERR.printf("\tType: %s\n\tCode: %s\n", k, v) - end - end - raise - end -end diff --git a/examples/recommendations/detect_and_apply_recommendations.rb b/examples/recommendations/detect_and_apply_recommendations.rb index 767a267a4..e0c871678 100644 --- a/examples/recommendations/detect_and_apply_recommendations.rb +++ b/examples/recommendations/detect_and_apply_recommendations.rb @@ -33,7 +33,8 @@ def detect_and_apply_recommendations(customer_id) client = Google::Ads::GoogleAds::GoogleAdsClient.new query = <<~QUERY - SELECT recommendation.resource_name + SELECT recommendation.resource_name, recommendation.campaign, + recommendation.keyword_recommendation FROM recommendation WHERE recommendation.type = KEYWORD LIMIT #{MAX_RESULT_SIZE} @@ -41,26 +42,25 @@ def detect_and_apply_recommendations(customer_id) reccomendation_service = client.service.recommendation google_ads_service = client.service.google_ads + NUMBER_OF_RUNS.times do |i| search_response = google_ads_service.search( customer_id: customer_id, query: query, + page_size: PAGE_SIZE, ) - operations = search_response.map do |row| - client.operation.apply_recommendation do |aro| - aro.resource_name = row.recommendation.resource_name - end - end - unless operations.empty? - response = recommendation_service.apply_recommendation( - customer_id: customer_id, - operations: operations, - ) + search_response.each do |row| + recommendation = row.recommendation - response.each do |result| - puts "Applied recommendation with resource name #{result.resource_name}." + puts "Keyword recommendation ('#{recommendation.resource_name}') was found for "\ + "campaign '#{recommendation.campaign}'." + if recommendation.keyword_recommendation + keyword = recommendation.keyword_recommendation.keyword + puts "\tKeyword = '#{keyword.text}'\n\ttype = '#{keyword.match_type}'" end + + apply_recommendation(client, customer_id, recommendation.resource_name) end if i < NUMBER_OF_RUNS - 1 @@ -70,10 +70,46 @@ def detect_and_apply_recommendations(customer_id) end end +# [START apply_recommendation] +def apply_recommendation(client, customer_id, recommendation_resource) + # If you have a recommendation_id instead of the resournce_name + # you can create a resource name from it like this: + # recommendation_resource = + # client.path.recommendation(customer_id, recommendation_id) + + apply_recommendation_operation = client.operation.apply_recommendation + apply_recommendation_operation.resource_name = recommendation_resource + + # Each recommendation type has optional parameters to override the recommended + # values. This is an example to override a recommended ad when a + # TextAdRecommendation is applied. + # For details, please read + # https://developers.google.com/google-ads/api/reference/rpc/google.ads.google_ads.v1.services#google.ads.google_ads.v1.services.ApplyRecommendationOperation + # + # text_ad_parameters = client.resource.text_ad_parameters do |tap| + # tap.ad = client.resource.ad do |ad| + # ad.id = "INSERT_AD_ID_AS_INTEGER_HERE" + # end + # end + # apply_recommendation_operation.text_ad = text_ad_parameters + + # Issues a mutate request to apply the recommendation. + recommendation_service = client.service.recommendation + response = recommendation_service.apply_recommendation( + customer_id: customer_id, + operations: [apply_recommendation_operation], + ) + applied_recommendation = response.results.first + + puts "Applied recommendation with resource name: '#{applied_recommendation.resource_name}'." +end +# [END apply_recommendation] + if __FILE__ == $0 MAX_RESULT_SIZE = 2 - NUMBER_OF_RUNS = 3 + NUMBER_OF_RUNS = 1 PERIOD_IN_SECONDS = 5 + PAGE_SIZE = 1000 options = {} # The following parameter(s) should be provided to run the example. You can diff --git a/examples/recommendations/get_text_ad_recommendations.rb b/examples/recommendations/get_text_ad_recommendations.rb deleted file mode 100755 index e5a1bc217..000000000 --- a/examples/recommendations/get_text_ad_recommendations.rb +++ /dev/null @@ -1,118 +0,0 @@ -#!/usr/bin/env ruby -# Encoding: utf-8 -# -# Copyright 2018 Google LLC -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# https://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# -# This example gets all TEXT_AD recommendations. - -require 'optparse' -require 'google/ads/google_ads' - -# [START get_text_ad_recommendations] -def get_text_ad_recommendations(customer_id) - # GoogleAdsClient will read a config file from - # ENV['HOME']/google_ads_config.rb when called without parameters - client = Google::Ads::GoogleAds::GoogleAdsClient.new - - ga_service = client.service.google_ads - - query = <<~QUERY - SELECT recommendation.type, recommendation.campaign, - recommendation.text_ad_recommendation - FROM recommendation - WHERE recommendation.type = TEXT_AD - QUERY - - response = ga_service.search( - customer_id: customer_id, - query: query, - page_size: PAGE_SIZE, - ) - - response.each do |row| - recommendation = row.recommendation - recommended_ad = recommendation.text_ad_recommendation.ad - - puts "Recommendation ('#{recommendation.resource_name}') was found for "\ - "campaign '#{recommendation.campaign}'." - if recommended_ad.expanded_text_ad - eta = recommended_ad.expanded_text_ad - puts "\tHeadline 1 = '#{eta.headline_part1}'\n\tHeadline2 = '#{eta.headline_part2}'\n" + - "\tDescription = '#{eta.description}'" - end - if recommended_ad.display_url - puts "\tDisplay URL = '#{recommended_ad.display_url}'" - end - recommended_ad.final_urls.each do |url| - puts "\tFinal Url = '#{url}'" - end - recommended_ad.final_mobile_urls.each do |url| - puts "\tFinal Mobile Url = '#{url}'" - end - end -end -# [END get_text_ad_recommendations] - -if __FILE__ == $0 - PAGE_SIZE = 1000 - - options = {} - # The following parameter(s) should be provided to run the example. You can - # either specify these by changing the INSERT_XXX_ID_HERE values below, or on - # the command line. - # - # Parameters passed on the command line will override any parameters set in - # code. - # - # Running the example with -h will print the command line usage. - options[:customer_id] = 'INSERT_CUSTOMER_ID_HERE' - - OptionParser.new do |opts| - opts.banner = sprintf('Usage: %s [options]', File.basename(__FILE__)) - - opts.separator '' - opts.separator 'Options:' - - opts.on('-C', '--customer-id CUSTOMER-ID', String, 'Customer ID') do |v| - options[:customer_id] = v - end - - opts.separator '' - opts.separator 'Help:' - - opts.on_tail('-h', '--help', 'Show this message') do - puts opts - exit - end - end.parse! - - begin - get_text_ad_recommendations(options.fetch(:customer_id).tr("-", "")) - rescue Google::Ads::GoogleAds::Errors::GoogleAdsError => e - e.failure.errors.each do |error| - STDERR.printf("Error with message: %s\n", error.message) - if error.location - error.location.field_path_elements.each do |field_path_element| - STDERR.printf("\tOn field: %s\n", field_path_element.field_name) - end - end - error.error_code.to_h.each do |k, v| - next if v == :UNSPECIFIED - STDERR.printf("\tType: %s\n\tCode: %s\n", k, v) - end - end - raise - end -end From d0bd5ab4d870cb2273312ec3129c4bec0da4bcb8 Mon Sep 17 00:00:00 2001 From: Daniel Rodriguez Date: Fri, 12 Jan 2024 10:02:02 -0600 Subject: [PATCH 2/2] Rename to get_product_category_constants Change-Id: I17867fc4eae63c6e7b36f4f2720b322df65583da --- ...uct_category_constant.rb => get_product_category_constants.rb} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename examples/shopping_ads/{get_product_category_constant.rb => get_product_category_constants.rb} (100%) diff --git a/examples/shopping_ads/get_product_category_constant.rb b/examples/shopping_ads/get_product_category_constants.rb similarity index 100% rename from examples/shopping_ads/get_product_category_constant.rb rename to examples/shopping_ads/get_product_category_constants.rb