Add incentives examples - #554
Conversation
Change-Id: I57fa8a776fb8a1cbd43f6c1d3a1a71c3c63bdd19
Change-Id: I2f4db5b40ddc0494f631b62d60b6992f786cbf57
Code Vetting & Review RecommendationsOverall, the implementation for adding incentive examples ( 1.
|
Change-Id: I19de34550a54efaa8095b503bd5e8c449930e9d4
Change-Id: I4f3b19fe5ace2a6d88f7f0c3b90906f97b0d454b
bobhancockg
left a comment
There was a problem hiding this comment.
Overall, the implementation for adding incentive examples is well-structured and incorporates previous feedback nicely. Here are a few remaining recommendations and optimizations before merging:
-
examples/incentives/apply_incentive.rb:- Clean up redundant
request_args[:country_code] = country_code if country_codeduplicate assignment on line 36.
- Clean up redundant
-
examples/incentives/fetch_incentive.rb:- Update request field name from
type: :ACQUISITIONtoincentive_type: :ACQUISITIONto match current Google Ads API protobuf definitions (v24+) and avoid runtimeArgumentError. - Provide default
nilarguments indef fetch_incentive(email, language_code = nil, country_code = nil)and construct request args using.compact. - Fix missing whitespace in string continuation on lines 51-52 (
"type." \ "Non-CYO"->"type.Non-CYO"). - Defensively format monetary currency strings against empty protobuf defaults (
"").
- Update request field name from
|
|
||
| # Issues the request. | ||
| request_args = { | ||
| customer_id: customer_id.to_s.tr('-', ''), | ||
| selected_incentive_id: incentive_id.to_i, | ||
| country_code: country_code |
There was a problem hiding this comment.
country_code is already included in the hash literal and cleaned by .compact. Line 36 is a leftover duplicate assignment from commit 2 and can be removed.
| # Issues the request. | |
| request_args = { | |
| customer_id: customer_id.to_s.tr('-', ''), | |
| selected_incentive_id: incentive_id.to_i, | |
| country_code: country_code | |
| # Issues the request. | |
| request_args = { | |
| customer_id: customer_id.to_s.tr('-', ''), | |
| selected_incentive_id: incentive_id.to_i, | |
| country_code: country_code | |
| }.compact |
| def fetch_incentive(email, language_code, country_code) | ||
| # GoogleAdsClient will read a config file from a default location | ||
| # if no path is passed. | ||
| client = Google::Ads::GoogleAds::GoogleAdsClient.new | ||
|
|
||
| # Issues the request. | ||
| response = client.service.incentive.fetch_incentive( | ||
| email: email, | ||
| language_code: language_code, | ||
| country_code: country_code, | ||
| # Passing :ACQUISITION as the symbol representation of the IncentiveType enum. | ||
| type: :ACQUISITION | ||
| ) |
There was a problem hiding this comment.
- In
FetchIncentiveRequest(v24+ / v25+), the field name isincentive_typerather thantype. Passingtype:will trigger anArgumentError: Unknown field name :typeon current protobuf stubs. - Adding default
nilvalues (language_code = nil, country_code = nil) and buildingrequest_argswith.compactallows callers to invokefetch_incentive(email)programmatically while relying on the API defaults (en,US).
| def fetch_incentive(email, language_code, country_code) | |
| # GoogleAdsClient will read a config file from a default location | |
| # if no path is passed. | |
| client = Google::Ads::GoogleAds::GoogleAdsClient.new | |
| # Issues the request. | |
| response = client.service.incentive.fetch_incentive( | |
| email: email, | |
| language_code: language_code, | |
| country_code: country_code, | |
| # Passing :ACQUISITION as the symbol representation of the IncentiveType enum. | |
| type: :ACQUISITION | |
| ) | |
| def fetch_incentive(email, language_code = nil, country_code = nil) | |
| # GoogleAdsClient will read a config file from a default location | |
| # if no path is passed. | |
| client = Google::Ads::GoogleAds::GoogleAdsClient.new | |
| # Issues the request. | |
| request_args = { | |
| email: email, | |
| language_code: language_code, | |
| country_code: country_code, | |
| # Passing :ACQUISITION as the symbol representation of the IncentiveType enum. | |
| incentive_type: :ACQUISITION | |
| }.compact | |
| response = client.service.incentive.fetch_incentive(request_args) |
| if response.incentive_offer.cyo_incentives | ||
| cyo = response.incentive_offer.cyo_incentives | ||
| [cyo.low_offer, cyo.medium_offer, cyo.high_offer].each do |incentive| | ||
| print_incentive_details(incentive) | ||
| end | ||
| else | ||
| puts "Incentive offer is not a CHOOSE_YOUR_OWN_INCENTIVE type." \ | ||
| "Non-CYO offers are not supported by this example." | ||
| end |
There was a problem hiding this comment.
- Line continuation without whitespace causes
"type." \ "Non-CYO"to concatenate into"type.Non-CYO". Adding a space after the period fixes the output. - Adding
.compactdefensively prevents iterating over any unset offer slots.
| if response.incentive_offer.cyo_incentives | |
| cyo = response.incentive_offer.cyo_incentives | |
| [cyo.low_offer, cyo.medium_offer, cyo.high_offer].each do |incentive| | |
| print_incentive_details(incentive) | |
| end | |
| else | |
| puts "Incentive offer is not a CHOOSE_YOUR_OWN_INCENTIVE type." \ | |
| "Non-CYO offers are not supported by this example." | |
| end | |
| if response.incentive_offer.cyo_incentives | |
| cyo = response.incentive_offer.cyo_incentives | |
| [cyo.low_offer, cyo.medium_offer, cyo.high_offer].compact.each do |incentive| | |
| print_incentive_details(incentive) | |
| end | |
| else | |
| puts "Incentive offer is not a CHOOSE_YOUR_OWN_INCENTIVE type. " \ | |
| "Non-CYO offers are not supported by this example." | |
| end |
|
|
||
| def format_money(money) | ||
| return 'N/A' if money.nil? | ||
|
|
||
| units = money.units ? money.units.to_f : 0.0 | ||
| nanos = money.nanos ? money.nanos.to_f : 0.0 | ||
| currency = money.currency_code || 'N/A' | ||
|
|
||
| amount = units + (nanos / 1_000_000_000.0) | ||
| sprintf('%.2f %s', amount, currency) |
There was a problem hiding this comment.
In protobuf messages, unset string fields default to "" (empty string), which is truthy in Ruby and bypasses || 'N/A', producing strings like "100.00 ". Checking .to_s.empty? ensures unset strings properly fallback to 'N/A'.
| def format_money(money) | |
| return 'N/A' if money.nil? | |
| units = money.units ? money.units.to_f : 0.0 | |
| nanos = money.nanos ? money.nanos.to_f : 0.0 | |
| currency = money.currency_code || 'N/A' | |
| amount = units + (nanos / 1_000_000_000.0) | |
| sprintf('%.2f %s', amount, currency) | |
| def format_money(money) | |
| return 'N/A' if money.nil? | |
| units = (money.units || 0).to_f | |
| nanos = (money.nanos || 0).to_f | |
| currency = money.currency_code.to_s.empty? ? 'N/A' : money.currency_code | |
| amount = units + (nanos / 1_000_000_000.0) | |
| sprintf('%.2f %s', amount, currency) | |
| end |
Change-Id: I57fa8a776fb8a1cbd43f6c1d3a1a71c3c63bdd19