GoogleAdsFieldServiceto create that schema.
Retrieving the dataWe can retrieve most of the data for the schema described in Part 2 by making an API call to the
GoogleAdsFieldServicewith the following query:
SELECT name, category, data_type, selectable, filterable, sortable, selectable_with, metrics, segments, is_repeated, type_url, enum_values, attribute_resources
The result contains an array of JSON objects for every field available in the Google Ads API. Each object in the array will contain the fields in the
SELECTclause above. For example, the
ad_grouplist item would look like this:
We’ll restructure this into key-value pairs, where the keys are the entity names and the values are the metadata for each entity, making it easier to lookup the metadata for a given entity. For example, the
ad_groupentry would look like this:
As compared with the schema we designed in Part 2, this restructured object is missing several fields, which we’ll add in the following sections: attributes, fields, description, and display name.
AttributesIf you recall from the schema design, each resource should contain an array named
attributeswhich contains the names of all fields present on the resource itself and any attributed resources. We can create this array by iterating through the results of our
GoogleAdsFieldServicequery and adding the name of entries beginning with either our resource or one of its attributed resources, followed by a dot.
fieldsentry in our schema is an object with an entry for each (a) item in the attributes array we just created, (b) metric on the resource, and (c) segment on the resource. The value of each entry will be each respective field’s value in the object we created earlier. However, we still need to add an
incompatible_fieldsarray to each field.
To construct the
incompatible_fieldsarray for each entry in our fields object, we’ll check to see if each of the fields, metrics, and segments present on the top level object is
selectable_withthe given field being evaluated. If not, we’ll add that field, metric, or segment to the
DescriptionsNext, we need to add descriptions to each top level resource and item in its
fieldsentry. It is important to note that a field may have a different description depending on the top level resource. For example, the description of
ad_group.idis “Output only. The ID of the ad group.” but the description of
campaign.idis “Output only. The ID of the campaign.” The REST discovery docs contain nested descriptions that we can use to create a canonical descriptions object, which we’ll use to populate our schema. This step entails parsing and formatting, the details of which we will not discuss here. We just want you to know that the REST discovery docs exist in case you ever need them. This is currently the best solution available, although it would be easier if the descriptions were returned from the
Display namesAll that is left to do is populate the display names field in our resource schema. We can do this by simply replacing underscores with spaces and capitalizing the first letter of each word in the name.
Filtering for resourcesOur resource schema is now fully populated. However, it contains every resource, field, segment, and metric returned from our
GoogleAdsFieldServicequery. We can filter this schema to include only items with a
ConclusionWe have now created an expanded resource schema containing detailed field information and a list of incompatible fields for each field, which we can use in our Angular application. In this post, we’ve covered:
- How to use the
GoogleAdsFieldServiceto retrieve field metadata.
- Field compatibility in GAQL.
- The REST discovery API.
Hopefully this has deepened your understanding of what is possible with the Google Ads API. If you have any questions or need additional help, contact us via the forum or at [email protected]