Securing historical data of Universal Analytics using the Google Reporting API

Google Analytics 4

  • Article
  • Data Analytics
Michelle Gijsberts
Michelle Gijsberts
Data Analyst
8 min
12 May 2023

As of 1 July 2023, Google Universal Analytics (UA or GA3) will stop processing data. More and more companies are therefore transitioning to GA4. Unfortunately, historical data from GA3 is not visible in GA4, and if you don't want to lose the data, you must extract everything from UA before 1 July 2024. After that, it will no longer be possible.

Want to know more about GA4? We've gathered all important information and updates here!

There are several ways to do this, such as manually exporting your standard reports, using the Google Analytics Sheets add-on, or working with the Google Reporting API. We believe that the last option is the best. In this article, you will learn how to secure your UA data using the API.

The manual export of standard reports in UA only allows you to select 2 dimensions and export a maximum of 5,000 rows to a CSV or Excel file. This may be insufficient if detailed data about items (such as product title, categories, and SKU) or sessions (source/medium, campaign, content) is needed for a longer period. Google Analytics Sheets offers the possibility to add more dimensions, but the data can be sampled if the time period is too large. Another problem is that you might reach the maximum amount of rows in the spreadsheet based on the granularity of the data that you are trying to retrieve.

The best way to save UA data

The best option to retrieve UA data is the Google Reporting API. The API allows you to easily retrieve all data from longer periods at once. In this article, you will learn how to do this step by step. This way, you can secure the historical data that has been accumulated and save it for future use.

1. Determine which dimensions and metrics are important

Not all data in UA will be essential for your questions. Based on the KPI framework, it can be determined which dimensions and metrics should be included in the queries. A dimension is a descriptive element used to categorise a data point. A metric is a quantitative element used to measure the performance of a data point, such as the number of visitors to a page or the conversion rate of a specific action.

Do you need help determining your KPIs? Our Data Consultants have extensive experience in organising KPI sessions and building KPI frameworks. Contact us for advice or request a KPI inspiration session without obligation.

Examples of dimensions:

  • Date: A dimension that is always relevant if you want to be able to see data per day, week, or year or view data for a specific period. Without a date, all data for the selected period will be lumped together.
  • Source/medium: Indicates where the traffic comes from.
  • Page: Provides information about the important pages.
  • Product SKU: Provides information about products. For example: the number of times it has been viewed, added to cart or sold.

Examples of metrics:

  • Sessions: The number of times a user has visited the site or app.
  • Page views: The number of times a page has been viewed.
  • Conversions: The number of times an event that is considered a conversion has been triggered.

​​It is recommended to create several tables that are the most important for your organisation. For example, a session table, an item table, and a page table. Once you have chosen the appropriate dimensions and metrics, you can initiate the connection between the Google Analytics account and the API.

2. Connection between UA and Google Cloud Console

To use the API, you first need to create a project in the Google Cloud Console. Via the Google Service Account you can create a project or select an existing project. After you have selected a project, click on the 'Create Service Account' button at the top.

secure historical data 1
secure historical data 2
  1. Give the service account a recognisable name (as in the example below) and specify what the account will be used for.
  2. Click on 'Create and Continue'. You will now see the created service account, including a new e-mail address.
  3. Add the e-mail address to the Google Analytics account from which you want to retrieve data with analyst permissions.
secure historical data 3

3. Create a key

Next, you need to create a key that you can use in the API call.

  1. Click on the Google Cloud Console e-mail address that you just added in Google Analytics.
  2. Click on 'KEYS' at the top, then 'ADD KEY' and 'Create new key'.
  3. Choose a JSON key. This key will now be saved as a file on your computer.
secure historical data 4

4. Enable access to the API

The last step in the Google Cloud Console is to enable the API. You can enable the API via this link by confirming that you are in the correct project and clicking on 'ENABLE'.

secure historical data 5

5. Install the appropriate packages and link the key to the call

Now that you have set up the connection between Universal Analytics and Google Cloud Console and created the key, you can set up the API call. This example uses Python, but it can also be done with R, Java, PHP, and JavaScript.

The packages that will help you with this are:

secure historical data 6

To connect your script to your Google Universal Analytics account, you need to use the JSON file that you downloaded when you created your API key. Place this file in the same folder as the script you are creating. Then use the code below to establish the connection:

secure historical data 7

Also, modify the file name and ID of the selected Universal Analytics view.

6. Retrieve the data of a certain time period

To retrieve the desired data from the API, you need to specify the desired metrics and dimensions you determined earlier. With Google's Query Explorer, you can find the correct names for these dimensions and metrics, including the names for custom dimensions. To do this, you need to sign in with a Google account that is linked to the Google Universal Analytics account and select the appropriate property. In the following example, we show how to retrieve data necessary for an item table:

Secure historical data 8

Since sampling can occur in the API response, the data is individually retrieved on a daily basis. By setting a pageSize of 10,000, the maximum pageSize, all data is always retrieved. You can also choose not to retrieve the data in a while loop, but instead choose a smaller start and end date. If you do this, you can check whether the data has been sampled using the following code:

Secure historical data 9

Make sure to store the data correctly. For example, it is recommended to store the data in an empty dataframe and then export it to a CSV file. This way you can retrieve all historical data from Google Analytics. In the above example, you see an item table, but you can work with other dimensions and metrics as well. To use the data in analyses and dashboards, you can save these CSV tables in BigQuery, for example. You can combine the data with other Google Analytics 4 tables.

Need advice or help with securing your data?

Our data specialists are happy to assist you. Can we help you with a seamless transition without losing important web data? Contact us.

This is an article by Michelle Gijsberts, Data Analyst at Digital Power

Michelle is a Data Analyst at Digital Power who focuses on uncovering the question behind the question. Through advanced analysis, she extracts insights from data that are not immediately visible and translates them into actionable recommendations.

Michelle Gijsberts

Data Analystmichelle.gijsberts@digital-power.com

Receive data insights, use cases and behind-the-scenes peeks once a month?


Sign up for our email list and stay 'up to data':

Get inspired about working with GA4

training

Google Analytics 4 training

Do you want to keep insight into the performance of your website or app? And do you want to be able to spot growth opportunities in your organisation? GA4 is an advanced web analytics platform offered by Google. It allows you to create reports, funnels or dashboards. Learn all about GA4 in our training sessions.

Read more
Google Analytics on a laptop screen

Google Analytics 4 vs. Google Analytics 3: What are the pros and cons?

If you want to keep track of free statistics about website visitors and their behaviour on your website, you will quickly turn to Google Analytics. With the free version of Universal Analytics (better known as GA3), it only takes a few minutes to set up basic metrics such as users, sessions and pageviews. For more extensive analyses, you have the paid version Google Analytics 360. As of July 2023, GA3 will no longer be supported by Google. Time to switch to the new GA4. Read all about the pros and cons of the new Google Analytics in this article.

Read more
Google Analytics logo

Implementation of Google Analytics 4

An interest group for homeowners offers a wide range of content and services on its website that can be closed online. In order to serve its website visitors in the best possible way, interactions with the services, funnels and content pages must be properly measured. They already did this with our support, with Universal Analytics (GA3). To be well prepared for the future, they asked us to implement Google Analytics 4.

Read more

Measure ecommerce events in GA4 and Universal Analytics with only the updated datalayer pushes

With our variable in the Google Tag Manager Community Template Gallery it is easy to start using all the new ecommerce analytics capabilities that Google Analytics 4 offers while fully supporting the 'old' enhanced ecommerce of Universal Analytics. Find out how the variable works.

Read more
GA Dashboard

What to do about broken Looker Studio dashboards?

Did the following scenario happen to you last week? While enjoying your morning coffee, you take a look at your pride and joy: a meticulously created Looker Studio dashboard built with the GA4 connector. You rub the sleep from your eyes and see your charts are all broken. Frantic phone calls from colleagues are pouring in. What is happening? Find out in this post what the consequences of Google’s newly imposed quota on the Google Analytics Data API are.

Read more