Closed jtj9817 closed 1 month ago
Theoretically, name_to_field
could be used to print all fields available and infer the getter and setter of each field.
But it belongs to the internal implementation of the Protobuf class, which can be changed at any time, so I doubt it's a reliable enough to be used to create a generic util for this Google Ads API library.
That sounds more like a generic util for the Protobuf repo.
We can leave this open as an feature request for now, but to be honest, this would take long time until we can schedule time to implement this.
I think this issue can be closed. The goal was to print all the fields and values for a given Asset
and the serializeToJsonString()
works just fine for this use case.
For example:
public function getAdWordsSnippets(Request $request)
{
$this->validateCustomerId($request->all());
$customer_id = $request->input('customer_id');
$googleAdsServiceClient = $this->getGoogleAdsServiceClient();
try {
$structured_snippets = [];
$query = "SELECT asset.structured_snippet_asset.header, asset.structured_snippet_asset.values FROM asset WHERE asset.source = 'ADVERTISER' AND asset.policy_summary.approval_status = 'APPROVED'";
$response = $googleAdsServiceClient->search(SearchGoogleAdsRequest::build($customer_id, $query), ['returnTotalResultsCount' => true]);
foreach ($response->iterateAllElements() as $googleAdsRow) {
if ($googleAdsRow->getAsset() != null && $googleAdsRow->getAsset()->getStructuredSnippetAsset() != null) {
$structured_snippets[] = json_decode($googleAdsRow->getAsset()->serializeToJsonString(), true);
}
}
return response()->json(['structured_snippets' => $structured_snippets], 200);
} catch (\Exception $e) {
return response()->json(['error' => "Error occurred! " . $e->getMessage()], 422);
}
}
So this can be closed, unless there's enough warrant for this helper class to be formalized as part of Google Ads API.
Thanks for confirming. serializeToJson()
should handle this case very well.
Problem you are trying to solve: There's a helper function in the examples called
printResponseDetails
, This is very handy when using printing details from amutateGoogleAdsResponse
object. However, there's no equivalent for aSearchAdsResponse
object, specifically when iterating through the results and you want information of an asset. Consider:These 2 functions are pretty similar in structure. The only differences are the fields being handled as well as the values. From the
googleAdsRow->getAsset()->getCallAsset()
object, there's an attribute calledname_to_field
under thedesc
which gives the fields and thegetter
andsetter
functions for each field. Perhaps there could be a generic way to print all the fields? Solution you'd like: A Helper class forSearchAdsResponse
object that determines the type of asset, its fields, values, and the appropriate enums for a value if needed. Alternatives you've considered: I have to manually figure out the enums and the fields.