How to load data from Google AdWords to Redshift

Extract your data from Google AdWords

The AdWords API 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 Google AdWords API is implemented only using the SOAP protocol, and it doesn’t offer a RESTful web implementation.

Nevertheless, they offer several client libraries that you can use for your language or framework of choice. They officially support clients in the following languages:

  • Java
  • .Net
  • PHP
  • PERL
  • Python
  • Ruby

The AdWords API is a 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 the data to a data warehouse for further analysis, we’ll focus only on that part of the Google AdWords API.

There are many ways of interacting with the data that AdWords 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. If you have the luxury to afford a Google analytics premium account, the other possible way is to load your data directly to Google BigQuery. From there, you can either do your analysis from BigQuery or export your data 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.

  1. Using an XML-based report definition.
  2. 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.

MARKDOWN
<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

MARKDOWN
<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

JAVASCRIPT
CampaignPage p = campaignService.query("SELECT Id, Name
WHERE Status = 'ENABLED'
ORDER BY Name
DESC 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

Google 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 many complex resources have to be handled through an also complex protocol.

Prepare your Google AdWords Data for Amazon Redshift

Amazon Redshift is built around industry-standard SQL with added functionality to manage very large datasets and high-performance analysis. So, to load your data into it, you will have to follow its data model, a typical relational database model. The data you extract from your data source should be mapped into tables and columns where you can consider the table as a map to the resource you want to store and columns the attributes of that resource. Also, each attribute should adhere to the data types that are supported by Redshift. Currently, the datatypes that are supported are the following:

  • SMALLINT
  • INTEGER
  • BIGINT
  • DECIMAL
  • REAL
  • DOUBLE PRECISION
  • BOOLEAN
  • CHAR
  • VARCHAR
  • DATE
  • TIMESTAMP

As your data are probably coming in a representation like JSON that supports a much smaller range of data types, you have to be careful about what data you feed into Redshift. You also have to ensure that you have mapped your types into one of the data that Redshift supports. Designing a Schema for Redshift and mapping the data from your data source to it is a process that you should take seriously as it can both affect the performance of your cluster and the questions you can answer. It’s always a good idea to have in your mind the best practices that Amazon has published regarding the design of a Redshift database. When you have concluded on the design of your database, you need to load your data on one of the data sources that are supported as input by Redshift, these are the following:

Load data from Google AdWords to Redshift

The first step to load your Google AdWords data to Redshift is to put them in a source Redshift can pull it from. As mentioned earlier, there are three main data sources supported, Amazon S3, Amazon DynamoDB, and Amazon Kinesis Firehose, with Firehose being the most recent addition to insert data into Redshift.

To upload your data to Amazon S3, you will have to use the AWS REST API. APIs play an important role in both the extraction and the loading of data into our data warehouse. The first task that you have to perform is to create a bucket, and you do that by executing an HTTP PUT on the Amazon AWS REST API endpoints for S3. You can do this by using a tool like CURL or Postman. You can also use the libraries provided by Amazon for your favorite language. You can find more information by reading the API reference for the Bucket operations on Amazon AWS documentation.

After you have created your bucket, you can start sending your data to Amazon S3, using the same AWS REST API again but by using the endpoints for Object operations. As in the Bucket case, you can either access the HTTP endpoints directly or use your preferred library.

DynamoDB imports data again from S3. It adds another step between S3 and Amazon Redshift so if you don’t need it for other reasons; you can avoid it.

Amazon Kinesis Firehose is the latest addition to insert data into Redshift and offers a real-time streaming approach to data importing. The necessary steps for adding data to Redshift through Kinesis Firehose are the following:

  1. create a delivery stream
  2. add data to the stream

Whenever you add new data to the stream, Kinesis adds these data to S3 or Redshift. Again going through S3, in this case, is redundant if your goal is to move your data to Redshift. The execution of the previous two steps can be performed either through the REST API or through your favorite library, just as in the previous two cases. The difference here is that you’ll be using a Kinesis Agent for pushing your data into the stream.

Amazon Redshift supports two methods for loading data into it. The first one is by invoking an INSERT command. You can connect to your Amazon Redshift instance with your client using either a JDBC or ODBC connection, and then you perform an INSERT command for your data.

SH
insert into category_stage values
(12, 'Concerts', 'Comedy', 'All stand-up comedy performances');

The way you invoke the INSERT command is the same as you would do with any other SQL database. For more information, you can check the INSERT examples page on the Amazon Redshift documentation.

Redshift is not designed for INSERT-like operations. On the contrary, the most efficient way of loading data into it is by doing bulk uploads using a COPY command. You can perform a COPY command for data that lives as flat files on S3 or from an Amazon DynamoDB table. When you perform COPY commands, Redshift can read multiple files simultaneously and automatically distributes the workload to the cluster nodes, and performs the load in parallel. As a command, COPY is quite flexible and allows for many different ways of using it, depending on your use case. Performing a COPY on amazon S3 is as simple as the following command:

SH
copy listing
from 's3://mybucket/data/listing/'
credentials 'aws_access_key_id=;aws_secret_access_key=';

For more examples of invoking a COPY command, you can check the COPY examples page on Amazon Redshift documentation. As in the INSERT case, the way to perform the COPY command is by connecting to your Amazon Redshift instance using a JDBC or ODBC connection and then invoke the commands you want using the SQL Reference from Amazon Redshift documentation.

The best way to load data from Google AdWords to Amazon Redshift and possible alternatives

So far, we just scraped the surface of what can be done with Amazon Redshift and how to load data into it. The way to proceed relies heavily on the data you want to load, from which service they are coming from, and the requirements of your use case. Things can get even more complicated if you want to integrate data coming from different sources.

Instead of writing, hosting, and maintaining a flexible data infrastructure, a possible alternative is to use an ETL as a service product like RudderStack that can handle this kind of problem automatically for you.

RudderStack integrates with multiple sources or services like databases, CRM, email campaigns, analytics, and more. Quickly and safely move all your data from Google Adwords into your data warehouse and start generating insights from your data.

Sign Up For Free And Start Sending Data
Test out our event stream, ELT, and reverse-ETL pipelines. Use our HTTP source to send data in less than 5 minutes, or install one of our 12 SDKs in your website or app.
Don't want to go through the pain of direct integration? RudderStack's Google Ads integration makes it easy to send data from Google Ads to Amazon Redshift.