The Query Builder Blog Series: Part 4 – Creating the Resource Service

This blog series follows the journey of building the new and improved Interactive Google Ads Query Builder tool. Part 3 of this series described how we used the GoogleAdsFieldService to 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.

Background

One 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 ResourceService that 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.

Objective

The 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 ResourceService that provides the relevant fields for a given clause and category.

Implementation

We 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 fields that match specific criteria.


Let’s begin by categorizing our fields by clause.
  • Fields in the SELECT clause have the selectable property equal to true.
  • Fields in the WHERE clause have the filterable property equal to true.
  • Fields in the ORDER BY clause have the sortable property equal to true.
We can further break these field lists into attribute fields, metrics, segments, and attributed resource fields. Retrieving the metrics and segments in a given clause is trivial because our resource schema provides a list of metrics and segments (including segmenting resources). For each clause, we’ll include all of the segments and metrics that meet the clause-related criteria listed above.

Similarly, we can retrieve each clause’s main attribute fields by looking at the attributes of 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 attributes entry 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 attributes excluding 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 ResourceService can simply call the corresponding method to retrieve the appropriate list of fields.

Conclusion

We now have created a ResourceService that 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].