How to load data from Google AdWords to Snowflake
Extract data from Google AdWords
The API from AdWords allows applications to interact directly with the AdWords platform. You can build applications to more efficiently manage large or complex AdWords accounts and campaigns. Contrary to the rest of the APIs that we have covered in this series of posts, the API is implemented only using the SOAP protocol and it doesn’t offer a RESTful web implementation.
Nevertheless, they offer a number of client libraries that you can use for your language or framework of choice. They officially support clients for the following languages:
- Java
- .Net
- PHP
- PERL
- Python
- Ruby
The AdWords API is a quite complex product that exposes a lot of functionality to the user, ranging from reporting to do the bidding and programmatic advertisement. As the scope of this post is the extraction of data from it, with the aim of loading data to a data warehouse for further analysis, we’ll focus only on that part of the AdWords API.
There are many ways of interacting with any data that API gathers. One way is to link your Google Analytics and AdWords accounts and actually enrich the data of your analytics with data coming from AdWords. The other possible way, if you have the luxury to afford a Google analytics premium account, is to load every data directly to Google BigQuery. From there, you can either do your analysis from BigQuery or export data you own to another data warehouse.
We’ll assume that you do not have a Google Analytics premium account, to be honest, if you had you wouldn’t be looking at this post anyway, but you still want to extract data and load it to your own data warehouse solution. To do that we’ll utilize the Report related functionality of the AdWords API. The API supports a huge number of reports that you can request, and it is possible to change the granularity of your results by passing specific parameters. Defining what kind of data you want to get back as part of your report can be done in two different ways.
- Using an XML-based report definition.
- Using an AWQL-based report definition.
If you want to use an XML-based report definition you have to include a parameter named __rdxml that will contain an XML serialized definition of the report you want to retrieve.
JAVASCRIPT
<reportDefinition xmlns="https://adwords.google.com/api/adwords/cm/v201509"><selector><fields>CampaignId</fields><fields>Id</fields><fields>Impressions</fields><fields>Clicks</fields><fields>Cost</fields><predicates><field>Status</field><operator>IN</operator><values>ENABLED</values><values>PAUSED</values></predicates></selector><reportName>Custom Adgroup Performance Report</reportName><reportType>ADGROUP_PERFORMANCE_REPORT</reportType><dateRangeType>LAST_7_DAYS</dateRangeType><downloadFormat>CSV</downloadFormat></reportDefinition>
AWQL is a SQL-like language for performing queries against most common AdWords API services. Any service with a query method is supported; queryable fields for each service are listed here.
As a comparison you can see the difference between using XML and AWQL below:
XML
JAVASCRIPT
<serviceSelector><fields>Id</fields><fields>Name</fields><predicates><field>Status</field><operator>EQUALS</operator><values>ENABLED</values></predicates><ordering><field>Name</field><sortOrder>ASCENDING</sortOrder></ordering><paging><startIndex>0</startIndex><numberResults>50</numberResults></paging></serviceSelector>
AWQL
HTML
CampaignPage p = campaignService.query("SELECT Id, NameWHERE Status = 'ENABLED'ORDER BY NameDESC LIMIT 0,50");
As we can see, the Google AdWords API has a very expressive way of defining what data we want to get from it and various options to do that. If you feel more comfortable with SQL like languages you can use AWQL, or if you prefer XML you can use that for defining your reports.
Regarding the format of the results you get from the API, there are also multiple options supported.
- CSVFOREXCEL – Microsoft Excel compatible format
- CSV – comma separated output format
- TSV – tab separated output format
- XML – xml output format
- GZIPPED-CSV – compressed csv
- GZIPPED-XML – compressed xml
AdWords exposes a very rich API which offers you the opportunity to get very granular data about your accounting activities and use it for analytic and reporting purposes. This richness comes with a price though, a large number of complex resources that have to be handled through an also complex protocol.
Google AdWords Data Preparation for Snowflake
The first step, before you start ingesting your own data into a Snowflake data warehouse instance, is to have a well-defined schema of your data.
Data in Snowflake is organized around tables with a well-defined set of columns with each one having a specific data type.
Snowflake supports a rich set of data types. It is worth mentioning that a number of semi-structured data types are also supported. With Snowflake, is possible to load directly data in JSON, Avro, ORC, Parquet, or XML format. Hierarchical data is treated as a first-class citizen, similar to what Google BigQuery offers.
There are also one notable common data type that is not supported by Snowflake. LOB or large object data type is not supported, instead, you should use a BINARY or VARCHAR type instead. But these types are not that useful for data warehouse use cases.
A typical strategy for loading data from AdWords to Snowflake is to create a schema where you will map each API endpoint to a table.
Each key inside the API for AdWords endpoint response should be mapped to a column of that table and you should ensure the right conversion to a Snowflake data type.
Of course you must ensure that as each of data types from AdWords API might change, you will adapt any database tables accordingly, there’s no such thing as automatic data type casting.
After you have a complete and well-defined data model or schema for Snowflake, you can move forward and start loading data into the database.
Load data from Google AdWords to Snowflake
Usually, data is loaded into Snowflake in a bulk way, using the COPY INTO command. Files containing data, usually in JSON format, are stored in a local file system or in Amazon S3 buckets. Then a COPY INTO command is invoked on the Snowflake instance and data is copied into any data warehouse.
The files can be pushed into Snowflake using the PUT command, into a staging environment before the COPY command is invoked.
Another alternative is to upload data directly into a service like Amazon S3 from where Snowflake can access every data directly.
Finally, Snowflake offers a web interface as a data loading wizard where someone can visually setup and copy data into the data warehouse. Just keep in mind, that the functionality of this wizard is limited compared to the rest of the methods.
Snowflake in contrast to other technologies like Redshift, does not require a data schema to be packed together with the data that will be copied. Instead, the schema is part of the query that will copy data into the data warehouse. This simplifies the data loading process and offers more flexibility on data type management.
Updating your Google AdWords data on Snowflake
As you will be generating more data on AdWords, you will have to update your older data on Snowflake. This includes new records together with updates to older records that for any reason have been updated on Google AdWords.
You will need to periodically check AdWords for new data and repeat the process that has been described previously while updating your currently available data if needed. Updating an already existing row on a Snowflake table is achieved by creating UPDATE statements.
Snowflake has a great tutorial on the different ways of handling updates, especially using primary keys.
Another issue that you need to take care of is the identification and removal of any duplicate records on your database. Either because Google AdWords does not have a mechanism to identify new and updated records or because of errors on your data pipelines, duplicate records might be introduced to a database.
In general, ensuring the quality of data that is inserted in your database is a big and difficult issue.
The best way to load data from Google AdWords to Snowflake
So far we just scraped the surface of what you can do with Snowflake and how to load data into it. Things can get even more complicated if you want to integrate data coming from different sources.
Are you striving to achieve results right now?
Instead of writing, hosting, and maintaining a flexible data infrastructure use Rudderstack that can handle everything automatically for you.
RudderStack with one click integrates with sources or services, creates analytics-ready data, and syncs your AdWords to Snowflake right away.