How To Export Google Analytics Data: An Easy Guide

on Tutorials • January 7th, 2022 • Write for Hevo

Export Google Analytics Data

Introduction

Google Analytics enables organizations to get complete details of all the user interactions on their websites or mobile applications. The ease of integration to products and the comprehensive reporting dashboard makes Google Analytics the most popular choice for deriving customer analytics from internet-based products. In this post, you will learn how to export Google Analytics data.

Decision-makers can rely on data from Google Analytics to choose marketing alternatives and decide what to focus on, in their customer acquisition journey. They export Google Analytics data to Google Sheets or other Reporting tools to analyze and generate insights. It also provides a simple interface to set up machine learning algorithms on user data. Even though the reporting dashboard is comprehensive, it is sometimes necessary for organizations to get access to raw hit level or view level data from their websites to perform deeper analysis.

Export Google Analytics data: GA "Home" page
Image by Ajay Nainani via Google

Google allows us to export Google Analytics data through its paid offering called GA360. The cost of this offering makes it inaccessible for smaller organizations. The goal of this article is to explain how to export Google Analytics data. More specifically, you will cover how to get raw hit-level data from Google Analytics without having access to GA360.

Table of Contents

A Simpler Alternative To Replicate Google Analytics Data To Your Data Warehouse

Hevo Data, a No-code Data Pipeline, is a fully automated solution that can be used to extract, transform, and load Google Analytics data into a data warehouse for free with just a few clicks. Hevo provides an intuitive user interface and it supports robust data transformations.

GET STARTED WITH HEVO FOR FREE

Check out some of Hevo’s cool features:

  • Fully Automated: The Hevo platform can be set up in just a few minutes and requires minimal maintenance.
  • Real-time Data Transfer: Hevo provides real-time data migration. So you can have analysis-ready data always.
  • 100% Complete & Accurate Data Transfer: Hevo’s robust infrastructure ensures reliable data transfer with zero data loss.
  • Scalable Infrastructure: Hevo has in-built integrations for 100’s of sources including 30+ Free sources that can help you scale your data infrastructure as required.
  • Live Support: The Hevo team is available round the clock to extend its support to your team through chat, email, and support calls.
  • Schema Management: Hevo takes away the tedious task of schema management & automatically detects the schema of incoming data and maps it to the destination schema.

Simplify your Data Analysis with Hevo today!

SIGN UP HERE FOR A 14-DAY FREE TRIAL!

Available Options to Export Google Analytics Data

There are 3 available options to export Google Analytics data. The 3 methods are given below:

  • Manual export of Google Analytics dashboard
  • Add-on to export Google Analytics data to Google Sheets
  • Google Analytics API for fetching datasets

Manual export of Google Analytics dashboard

This is the most used method to export Google Analytics data. In this option, Google Analytics users manually export Google Analytics data from the dashboard.

  • First, select the Google Analytics report that you want to export.
  • Then, click on the “Export” button located at the top right corner.
  • Choose the format in which you want to export Google Analytics data.
  • After some loading and preparation of data, the report will be downloaded to your local system.
Download the Whitepaper on Automating Data Integration
Download the Whitepaper on Automating Data Integration
Download the Whitepaper on Automating Data Integration
Learn the key benefits of automating Data Integration

Add-on to export Google Analytics data to Google Sheets

In this option, a Google Analytics add-on is used to export Google Analytics data that you can find in Google Workspace Marketplace and install it. Get the add-on here. This option is widely used because of a few reasons:

  • Users can customize the dimensions and metrics to include in the report.
  • You can automate the export of Google Analytics data to Google Sheets by scheduling it.
  • You can specify the maximum number of rows to return for your Google Analytics report.

Google Analytics API for fetching datasets

Google Analytics API offers a great analytical experience using a few APIs. 

  • Reporting API: It allows you to access data in GA Universal Properties and export Google Analytics data.
  • Realtime API: It allows users to access real-time data.
  • Data API: It allows users access to report data of GA4 properties.

Can Google Analytics Export All Data?

There is always a requirement to export all Google Analytics data in one go. Users need historical data and cannot manually export Google Analytics data for different time frames or intervals. There are 3 ways to export all Google Analytics data:

  • Manually exporting reports: It can take some time based on the volume of data you have in Google Analytics.
  • Exporting aggregated reports to Google Sheets using the Google Analytics add-on.
  • Exporting aggregates reports using Google Analytics API.

How to Automate Data Export From Google Analytics

Apart from programmatically export Google Analytics data, there is only one option available that is using Google Analytics add-on for Google Sheets. It provides you options for frequency and intervals to export Google Analytics data and custom reports.

Prerequisites

  • Google Analytics account with admin privileges.
  • Basic knowledge of dimensions and metrics in Google Analytics.

How to Export Data from Google Analytics

Google enables programmatic access to GA data through Reporting API V4. The data is structured in terms of dimensions and metrics. Dimensions are factors based on which data is aggregated and metrics are the keys that provide information. For example, ‘country’ is a dimension and the number of sessions is a metric.

Google Analytics provides a set of dimensions and metrics as default. Users can also create their own dimensions and metrics by making small modifications to the tracking code that is deployed with the website. To get hold of raw hit level data from Google Analytics, you need to use some custom dimensions.

Here are the steps you will cover in this section:

Creating Custom Dimensions In Google Analytics

Go to the admin section and navigate to the property where the custom dimension is to be added. Click on ‘Create New Custom Dimension’ and add the dimension name. For now, let us define it as hit_id. Select the scope as ‘hit’, check the active box, and click on ‘Create’.

Adding The Custom Dimension Tracking Code To The Google Analytics Script

Google Analytics uses a script called gtag.js to track user behavior on your website. Add the below snippet in your gtag.js part on the web page for which you want to get hit level data:

gtag('config', 'GA_MEASUREMENT_ID', {
  'custom_map': {'dimension1': hit_id, ‘dimension2’:brow_id}
});

In the above code, use an arbitrary identifier as the GA_MEASUREMENT_ID. Google Analytics follows a convention of dimension<number from 1-20> as the key in the custom map. The value is the name of the dimension that will be used here. 

You must now create a unique id for the user’s browser using the below snippet:

if (document.cookie.indexOf('browser_uuid_set=1') == -1) {

gtag(‘set’,{‘brow_id’,Math.random()})

document.cookie = 'browser_uuid_set=1; expires=Fri, 01 Jan 2100 12:00:00 UTC; domain=.arem.us; path=/';

}

gtag(‘set’,{'hit_id',new Date().getTime()})

gtag('event', 'page_view');

Time is being used as an identifier of the hit here. Every time a hit is made, gtag.js will send a page_view event with two details – The browser id which was created, and the timestamp of that view.

In the next step, this raw hit level data that is reaching the GA server will be gotten hold of.

Installing Google Reporting API V4 Python Library

You will now try to access the data using Google Reporting API V4. Install the python library for Google Reporting API using the below command:

sudo pip install --upgrade google-api-python-client

Importing The Required Libraries

Using the python library, create a script to download the hit level data into a CSV. Begin by importing the required libraries. Use the below snippet:

from apiclient.discovery import build
from oauth2client.service_account import ServiceAccountCredentials

Initializing The Required Variables

SCOPES = ['https://www.googleapis.com/auth/analytics.readonly']
KEY_FILE_LOCATION = '<KEY_JSON_FILE>'
VIEW_ID = '<REPLACE_WITH_VIEW_ID>'

The above variables are required for OAuth authentication. You have to replace the key file location and the view id with the assets you obtained while setting up the service account for Google Analytics. VIEW_ID can be seen in the admin section.

Initializing The Objects

Initialize the objects for accessing the data using the below snippet:

credentials = ServiceAccountCredentials.from_json_keyfile_name(KEY_FILE_LOCATION, SCOPES)
 # Build the service object.
  analytics = build('analyticsreporting', 'v4', credentials=credentials)

Retrieving Response From Reporting API

Use the batchGet method in the library to retrieve the response from the Reporting API. 

response = analytics.reports().batchGet(
      body={
        'reportRequests': [
        {
          'viewId': VIEW_ID,
          'dateRanges': [{'startDate': '7daysAgo', 'endDate': 'today'}],
          'dimensions': [{ "name":"ga:dimension1" },
{"name":"ga:dimension2" }],
        }]
      }
  ).execute()

The response will be a JSON file that contains a nested map of the two dimensions that were sent to Google Analytics for each page view. The JSON file can be parsed into CSV or directly loaded into a data warehouse after required transformations.

How to Enable Google Analytics API for Exporting Data

 Google API client libraries offer support for many languages such as Java, .NET, Python, etc. These client libraries allow you to access and program using the Google Analytics APIs. But you can only access these libraries and APIs after enabling the target API and getting the application credentials. Let’s have a look at how to enable Google Analytics API. The steps are listed below:

Step 1: Create a New Project on Google API Console

  • Log in to your Google API Console account here.
  • Here, create a new project and name as per your wish.
  • Then, click on the Create button.

Step 2: Enabling the Google Analytics API

  • Now, select the project from the top menu bar.
  • Then, go to the Dashboard of the APIs & Services menu.
  • Here, click on the “Enable APIs and Services“.
  • Now, search for “Google Analytics” and select ay of the Google Analytics API that you want to use.
  • Then, click on the “Enable” button.

Step 3: OAuth Consent Screen and Credentials

  • Click on the “Create Credentials” option and then navigate to the OAuth consent screen.
  • Select the user type as “External” and click on the “Create” button.
  • It will navigate you to the next page where you need to configure the following settings.
  • App Information:
    • App Name 
    • User Support E-Mail
  • Developer Contact Information
    • E-Mail Address
  • Then click on the “Save and Continue” button.
  • Navigate to the Scopes page and click on the “Save and Continue” button.
  • Go to Test Users Page and click on the “Add users” option to add a new E-Mail address.
  • Then, click on the “Save and Continue” button.
  • On the Summary page click on the “Back to Dashboard“.
  • Now, you have to navigate to the Credentials Menu, and here, click on the “Create Credentials” option.
  • Select the type of credentials to use.
  • Then create the credentials.

Conclusion

Great! You have now learned how to export Google Analytics data. You have learned how to add custom dimensions to Google Analytics tracking code and also how to extract hit-level data using custom dimensions. Here are some of the typical challenges developers face while implementing the above approach in production:

  • This approach requires you to modify the tracking code as well as write a custom script using the Reporting API python library to download the data. Taking this to production will require you to write even more code. Cloud-based ETL tools like Hevo can accomplish this without the need to write any custom code. 
  • The above approach will work well for one offload, but in data warehousing scenarios, that is rarely the case. You will need to build additional logic to execute this continuously. Hevo’s scheduling capability will handle such problems for you, and hence saving critical development time. Reporting API has quotas and limits associated with it. So the application logic should have the ability to work around the limits. Hevo automatically adheres to such throttling limits and relieves you of this implementation. 
  • The raw data in most cases will have to be transformed into different formats before inserting it into the data warehouse. Hevo comes with comprehensive transformation support and helps you accomplish these in a few clicks.
VISIT OUR WEBSITE TO EXPLORE HEVO

So if all that scripting magic and the associated challenges feel like too much effort, you can think of using a completely automatic, cloud-based ETL tool like Hevo that can extract, transform and load Google Analytics data to almost any data warehouse for Free in a matter of a few clicks. Hevo provides a simple user interface and comes with complex transformation support.

Want to take Hevo for a spin?

SIGN UP and experience the feature-rich Hevo suite first hand. You can also have a look at the unbeatable pricing that will help you choose the right plan for your business needs.

Share your thoughts on how to export Google Analytics raw data in the comments. We would love to hear from you!

Automate Marketing Reporting & Dashboards