GoogleAdsFieldServiceto create a detailed JSON resource schema to serve as the canonical data set for the angular application. In Part 4, we will discuss how to create a resource service that determines which fields are displayed to users in various parts of the application.
BackgroundOne of the benefits of the new Interactive Google Ads Query Builder is that fields are dynamically updated based on user selections to display whether a field is selectable or not, and if not, users are provided with feedback to understand why a given field is not selectable. However, we must first present lists of fields that are available to select in each clause of a Google Ads Query Language (GAQL) string based on the main resource in the FROM clause of that GAQL string. We will create a service called the
ResourceServicethat will contain this logic. Then, we can use Angular’s services and dependency injection model to pull the appropriate fields and their related information into any component.
ObjectiveThe app is designed such that the FROM resource is determined by the user’s current URL. Therefore, the resource in the FROM clause and list of all available fields is constant based on that URL. For the sake of this post, we are only concerned with three GAQL clauses, SELECT, WHERE, and ORDER BY, because these are the only clauses that can be dynamically populated with fields. LIMIT accepts an integer, and PARAMETERS only has a single option.
Within each clause, we will organize each of the available fields into four categories: attribute fields, metrics, segments, and attributed resource fields for a better user experience. Our objective is to create a
ResourceServicethat provides the relevant fields for a given clause and category.
ImplementationWe can leverage the resource schema we generated earlier to select the entry for the main resource in the FROM clause as determined by the URL and filter its fields sub-entry to provide only
fieldsthat match specific criteria.
Let’s begin by categorizing our fields by clause.
- Fields in the SELECT clause have the
selectableproperty equal to true.
- Fields in the WHERE clause have the
filterableproperty equal to true.
- Fields in the ORDER BY clause have the
sortableproperty equal to true.
Similarly, we can retrieve each clause’s main attribute fields by looking at the
attributesof the resource in our FROM clause, filtering for those which begin with the resource name followed by a dot, and applying the clause-related filtering criteria above.
All of the other fields in our
attributesentry are attributed resource fields. We can generate a list of attributed resources by creating a set of the unique prefixes (i.e., the text that precedes the dot) of the resource’s
attributesexcluding our main resource. Finally, we can create a list of fields by clause for each attributed resource by selecting the fields that are prefixed with each respective attributed resource’s name.
With all of this logic in place, we can create an interface that exposes methods for returning a list of fields in a given category and clause. Then, any component that is injected with the
ResourceServicecan simply call the corresponding method to retrieve the appropriate list of fields.
ConclusionWe now have created a
ResourceServicethat we can use to display relevant fields to a user constructing a GAQL query based on the clause and category they are viewing in the app. In this post, we’ve covered:
- GAQL query structure.
- The various types of fields that can appear in GAQL clauses.
- Field properties and how they correspond to different GAQL clauses.
Hopefully this has deepened your understanding of constructing GAQL queries with the Google Ads API. If you have any questions or need additional help, contact us via the forum or at [email protected]