Google Analytics to BigQuery

This page provides you with instructions on how to extract data from Google Analytics and load it into Google BigQuery. (If this manual process sounds onerous, check out Stitch, which can do all the heavy lifting for you in just a few clicks.)

What is Google BigQuery?

Google BigQuery is a data warehouse that delivers super-fast results from SQL queries, which it accomplishes using a powerful engine dubbed Dremel. With BigQuery, there's no spinning up (and down) clusters of machines as you work with your data. With all of that said, it's clear why some claim that BigQuery prioritizes querying over administration. It's super fast, and that's the reason why most folks use it.

Getting data out of Google Analytics

This can be tricky with Google Analytics because the API's we're working with here don't allow us to extract event level data. It would be great to just extract page_views or visitors, but that option is only available on the paid tier of Google Analytics, which carries a big price tag. Therefore, the data we'll be working with is rolled up into an aggregated format.

The gateway to your Google Analytics data is the Google Core Reporting API. By following the process in the API documentation, you'll make calls to it in order to retrieve your data.

Example Google Analytics code

You can call the GA API programmatically, it returns JSON formatted data. This is an example of what that response might look like:

{
  "kind": "analytics#gaData",
  "id": string,
  "selfLink": string,
  "containsSampledData": boolean,
  "query": {
    "start-date": string,
    "end-date": string,
    "ids": string,
    "dimensions": [
      string
    ],
    "metrics": [
      string
    ],
    "samplingLevel": string,
    "sort": [
      string
    ],
    "filters": string,
    "segment": string,
    "start-index": integer,
    "max-results": integer
  },
  "itemsPerPage": integer,
  "totalResults": integer,
  "previousLink": string,
  "nextLink": string,
  "profileInfo": {
    "profileId": string,
    "accountId": string,
    "webPropertyId": string,
    "internalWebPropertyId": string,
    "profileName": string,
    "tableId": string
  },
  "columnHeaders": [
    {
      "name": string,
      "columnType": string,
      "dataType": string
    }
  ],
  "rows": [
    [
      string
    ]
  ],
  "sampleSize": string,
  "sampleSpace": string,
  "totalsForAllResults": [
    {
      metricName: string,
      ...
    }
  ]
}

Loading data into Google BigQuery

Google Cloud Platform offers a helpful guide for loading data into BigQuery. You can use the bq command-line tool to upload the files to your awaiting datasets, adding the correct schema and data type information along the way. The bq load command is your friend here. You can find the syntax in the bq command-line tool quickstart guide. Iterate through this process as many times as it takes to load all of your tables into BigQuery.

Keeping Google Analytics data up to date

Awesome work. Your script will extract data from Google Analytics and load it into your data warehouse for Analysis. All done right? Not just yet. What about when there are new and updated records?

One option here would be to load all of your data over again. This will work, but it's most assuredly the slowest and the most painful option. It won't be a good solution if latency is a priority for you.

The only sustainable way to keep your data up to date is to build your script in such a way as to identify new and updated records. You can do this by building in logic around some primary keys that auto-increment like updated_at or created_at. When you've built in this functionality, you can set up your script as a cron job or continuous loop to grab new data as it appears in Google Analytics.

Other data warehouse options

BigQuery is really great, but sometimes you need to optimize for different things when you're choosing a data warehouse. Some folks choose to go with Postgres or Redshift, which are two RDBMSes that use similar SQL syntax. If you're interested in seeing the relevant steps for loading this data into Postgres or Redshift, check out To Redshift and To Postgres.

Easier and faster alternatives

If all this sounds a bit overwhelming, don’t be alarmed. If you have all the skills necessary to go through this process, chances are building and maintaining a script like this isn’t a very high-leverage use of your time.

Thankfully, products like Stitch were built to solve this problem automatically. With just a few clicks, Stitch starts extracting your Google Analytics data via the API, structuring it in a way that is optimized for analysis, and inserting that data into your Google BigQuery data warehouse.