How to load data from Google Analytics to PostgreSQL

Blendo Team

This post helps you with loading your data from Google Analytics to PostgreSQL. If you are looking to get analytics-ready data without the manual hassle you can integrate Google Analytics to PostgreSQL with Blendo, so you can focus on what matters, getting value out of your business data.

Access your data on Google Analytics

The first step in loading your Analytics data to any kind of data warehouse solution, is to access your data and start extracting it.

The Google Analytics Reporting API is the most advanced programmatic method to access report data in Analytics. The API also allows you to programmatically interact with your Analytics account, creating reports and dashboards that can be viewed from within your GA account but also to embed them into other applications.

Data from Analytics is always coming in the form of a report, which means that you have to construct a report and request it from Analytics for a specific time period.

Google Analytics is accessed in the same way that every other Google API is, you need to leverage the API console to manage applications and access to the various APIs, including Analytics.

You will need more time to read this post than integrating Google Analytics to PostgreSQL.

Effortlessly Sync All Your Google Analytics Data to PostgreSQL

In addition to the above, the things that you have to keep in mind when dealing with the Google Analytics API, are:

  1. Rate limits. Every API, has some rate limits that you have to respect.
  2. Authentication. You authenticate on Google Analytics using an OAuth.
  3. Paging and dealing with big amount of data. Platforms like Google Analytics that are dealing with clickstream data tend to generate a lot of data, like events on your web properties.

About Google Analytics

Analytics is a freemium web analytics service offered by Google that tracks and reports website traffic. Google Analytics is the most commonly used service for tracking the traffic on a website and an invaluable tool, especially for marketers.

Google Analytics has evolved into a very powerful tool, which offers reports about your visitors and a full set of tools to perform clickstream analysis ranging from a live view to complex funnel analytics and event tracking.

The free version of Google Analytics offers a sampled view of data which suffices for most cases as it gives a very accurate view of what is happening, there’s also the option of using Google Analytics Premium, which also grants you access to all the raw events, but this is part of a paid version of the service.

Transform and prepare your Google Analytics Data for PostgreSQL

After you have accessed your data on Analytics, you will have to transform it based on two main factors,

  1. The limitations of the database that the data will be loaded onto
  2. The type of analysis that you plan to perform

Each system has specific limitations on the data types and data structures that it supports. If for example you want to push data into Google BigQuery, then you can send nested data like JSON directly, but keep in mind that the data you get from Analytics are in the form of a tabular report closer to what a CSV or an spreadsheet looks like.

Ofcourse, when you are dealing with tabular data stores, like Microsoft SQL Server, this is not an option. Instead, you will have to flatten out your data, just as in the case of JSON, before loading into the database.

Also, you have to choose the right data types. Again, depending on the system that you will send the data to and the data types that the API exposes to you, you will have to make the right choices. These choices are important because they can limit the expressivity of your queries and limit your analysts on what they can do directly out of the database. Analytics has a very limited set of available data types which means that your work to do these mappings is much easier and straight forward, but nonetheless equally important with any other case of a data source.

In order to understand and model correctly your Analytics data, you will need to understand that the data coming out of it are in the form of a report. The report is like a spreadsheet and it can be naturally mapped into a table. So more or less you will end up with a one to one mapping between a report and a table on your database.

You also need to keep in mind that because of the report nature of the data, you will not find any primary keys that can be used for deduplication and reference. This is something that you have to construct by understanding the nature of your reports data.

Also, as Google analytics is sampling the data to generate the report, you might see slightly different values if you pull the same report, for the same period, more than once.

Each table is a collection of columns with a predefined data type like an integer or VARCHAR. PostgreSQL, like any other SQL database supports a wide range of different data types.

A typical strategy for loading data from Analytics to a Postgres database is to create a schema where you will map each API endpoint to a table. Each key inside the Analytics API endpoint response should be mapped to a column of that table and you should ensure the right conversion to a Postgres compatible data type.

Load data from Google Analytics to PostgreSQL

For example, if you have an endpoint from Google Analytics to return a value as String, you should convert it into a VARCHAR with a predefined max size or TEXT data type. tables can then be created on your database using the CREATE SQL statement.

Once you have defined your schema and you have created your tables with the proper data types, you can start loading data into your database.

The preferred way of adding larger datasets into a PostgreSQL database is by using the COPY command. COPY is copying data from a file on a file system that is accessible by the Postgres instance, in this way much larger datasets can be inserted into the database in less time. COPY requires physical access to a file system in order to load data.

Nowadays, with the cloud-based, fully managed databases, getting direct access to a file system is not always possible. If this is the case and you cannot use a COPY statement, then another option is to use PREPARE together with INSERT, to end up with optimized and more performant INSERT queries.

Updating your Google Analytics data on PostgreSQL

As you will be generating more data on Analytics, you will need to update your older data on PostgreSQL. This includes new records together with updates to older records that for any reason have been updated on Analytics.

You will need to periodically check Google Analytics 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 PostgreSQL table is achieved by creating UPDATE statements.

Another issue that you need to take care of is the identification and removal of any duplicate records on your database. Either because Analytics 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 your database.

In general, ensuring the quality of any data that is inserted in your database is a big and difficult issue and PostgreSQL features like TRANSACTIONS can help tremendously, although they do not solve the problem in the general case.

The best way to load data from Google Analytics to PostgreSQL

So far we just scraped the surface of what you can do with PostgreSQL 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 Blendo that can handle everything automatically for you.

Blendo with one click integrates with sources or services, creates analytics-ready data and syncs your Analytics to PostgreSQL right away.

Combine website traffic and event data that live in Google Analytics with Sales or Marketing data and supercharge your business insights.

Blendo is the easiest way to automate powerful data integrations.

Try Blendo free for 14 days. No credit card required.