googleads/google-ads-ruby

How to determine whether a numeric field value is actually set or not?

Closed this issue · 1 comments

Hi, I'm facing an issue with the library in gem version 15.0.0. When my query contains numeric fields and the response does not contain them (e.g. because they are not set for the account, campaign, ad_group etc.), the library still returns 0. Ideally I'd prefer receiving nil, or having some way to check whether the underlying response contained a value at all.

Let's pick a simple example. I use following query via the google_ads_service search_stream:

SELECT
  customer.id,
  customer.conversion_tracking_setting.conversion_tracking_id,
  customer.conversion_tracking_setting.cross_account_conversion_tracking_id
FROM customer

The response row looks like the following (IDs obviously manually replaced for the example):

<Google::Ads::GoogleAds::V9::Services::GoogleAdsRow: customer: <Google::Ads::GoogleAds::V9::Resources::Customer: resource_name: "customers/1234567890", id: 1234567890, conversion_tracking_setting: <Google::Ads::GoogleAds::V9::Resources::ConversionTrackingSetting: conversion_tracking_id: 1234567891>, pay_per_conversion_eligibility_failure_reasons: [], optimization_score_weight: 0.0>>

So the query asked for customer.conversion_tracking_setting.cross_account_conversion_tracking_id, but the response does not contain this information (because it is not set for the account).

Now when I try to get the value for customer.conversion_tracking_setting.cross_account_conversion_tracking_id, the library returns 0. If it would return nil, I'd understand it as "value not set", but getting 0 is a defined numeric value. This might be even more important for other numeric field values where the actual value range supports also single digit values including 0. I guess the underlying nil value is turned to 0 because the library calls somewhere a value.to_i which in ruby results in 0 for nil. However this loses the information whether the value was set at all or not. I couldn't find a way in the google-ads-ruby library to check whether a certain value is set or not before getting its actual value, e.g. calling nil? or present? on the field doesn't work since the value is already 0 due to the way the library provides it.

I wonder whether

  • nil could be provided instead of 0 for numeric fields where the value is not set in the actual response,
  • and/or the library could provide a different way to check whether a value is defined or not in the response.

Maybe I'm overlooking something obvious here, so I'd be happy about any hint.

Thanks,

Robert

All "optional" fields have a "has" method to check for presence. For example, customer.conversion_tracking_setting.has_cross_account_conversion_tracking_id? will return false if it wasn't set at all in the response from the API. If a field is not optional then this method is not defined.

Right now you can't check if a field is optional from our documentation. I'm going to see if we can do something about that. In the meanwhile, you can check whether or not a field is optional by consulting the protos directly, for example: https://github.com/googleapis/googleapis/blob/master/google/ads/googleads/v9/resources/customer.proto#L154