Search API Connector Documentation

Print

Import Google Analytics (UA) Data to Google Sheets

premium

In this guide, we’ll walk through how to pull Google Analytics data data directly into Google Sheets, using the API Connector add-on for Sheets.

Google Analytics is, of course, an extremely popular web analytics tool, so there are many other, specialized connection tools like Google’s official Google Analytics spreadsheet add-on. So definitely go give that one a try if you’d prefer not to work with the API directly.

Still here? Nice 🙂 This article will show how to access the following Google Analytics reporting APIs:

  • Core Reporting API v3 (Universal Analytics)
  • Reporting API v4 (Universal Analytics)

(The new GA4 Data API is discussed here: Import Google Analytics GA4 Data to Google Sheets)

Contents

Before You Begin

Click here to install the API Connector add-on from the Google Marketplace.

Part 1: Connect to Google Analytics

The easiest way to get started with the Google Analytics API is through API Connector’s built-in integration.

  1. Select Google Analytics (UA) from the drop-down list of applications
    ga-application
  2. Under Authorization, click Connect to Google Analytics (UA)
    ga-authorization
  3. You will be asked to allow API Connector to view your Google Analytics data. Click Allow.
    google-analytics-img2
  4. You’ll then be returned to your Google Sheet, and can verify that your Google Analytics connection is active.

Part 2: Pull Data from Google Analytics into Sheets

Now that we’re connected, let’s pull some data into Sheets.

  1. Under Endpoint, choose the data you’d like to retrieve. The /data/ga endpoint will return your GA data.
    ga-endpoints
  2. Set all required parameters, including your view ID, the fields in your report, date range, and so on. Get your view ID while logged into the admin section of GA, or by running a request to the /management/accountSummaries endpoint,
    ga-parameters
  3. Optionally set any optional parameters such as filters and sorting.
  4. Select a destination sheet, name your request, and click Run.
    ga-response
  5. To give your column headers more reader-friendly names, click Edit Fields and name your fields in the field editor.

Part 3: Create a Custom Request to the V3 Core Reporting API

Alternatively, you can run a custom request instead of using API Connector’s built-in integration, using any of the parameters shown in GA’s API documentation.  Here is a complete sample request to get Universal Analyics data from the V3 Google Analytics API.

Request configuration example

  • Method: GET
  • Request URL: https://www.googleapis.com/analytics/v3/data/ga?ids=ga:100425068&start-date=2021-09-01&end-date=2021-10-01&metrics=ga:users,ga:newusers,ga:sessions,ga:transactions,ga:transactionRevenue&dimensions=ga:isoYearIsoWeek,ga:channelGrouping
    substitute in your own property ID where it says 100425068, as well as your metrics and dimensions
  • OAuth: Google Analytics
  • Headers: Key = Content-Type, Value = application/json
  • JMESPath: {id:id,profileInfo:profileInfo,rows:rows}

Notes

  • To easily convert from a preset request to a custom API URL, tick the Add request URL box before running your preset request (under Output options). This will print out your complete API URL, which you can then copy/paste into the request URL field.
  • To give your column headers more reader-friendly names, click Edit Fields and name your fields in the field editor.
  • Reasons to use V3 instead of V4:
    • Easier to construct queries. Fields are listed as URL parameters rather than in the request body.
    • Access metadata like the view and property ID. The V4 API doesn’t return these fields.
    • Migrate from or merge data with other systems pulling from GA. Google supports both V3 and V4, so some BI tools still provide data through the V3 API.

Part 4: Create a Custom Request to the V4 Google Analytics API

Here is a complete sample request to get Universal Analyics data from the V4 Google Analytics API.

Request configuration example

  • Method: POST
  • Request URL: https://analyticsreporting.googleapis.com/v4/reports:batchGet
  • OAuth: Google Analytics
  • Headers: Key = Content-Type, Value = application/json
  • Request Body: {"reportRequests":[{"viewId":"111111111","dateRanges":[{"startDate":"2021-09-01","endDate":"2021-09-30"}],"metrics":[{"expression":"ga:users"},{"expression":"ga:sessions"}],"dimensions":[{"name":"ga:browser"},{"name":"ga:country"}],"pageSize":5000}]}
  • JMESPath: reports[].data.rows[]

The whole thing should look like this:

Notes:

  1. To give your column headers more reader-friendly names, click Edit Fields and name your fields in the field editor.
  2. Filters can be added to the request body like this:
    {"reportRequests":[{"viewId":"111111111","dateRanges":[{"startDate":"2021-09-01","endDate":"2021-09-30"}],"metrics":[{"expression":"ga:users"},{"expression":"ga:sessions"}],"dimensions":[{"name":"ga:browser"},{"name":"ga:country"}],"dimensionFilterClauses":[{"filters":[{"dimensionName":"ga:browser","operator":"EXACT","expressions":["Chrome"]}]}],"pageSize":5000}]}

Part 5: Handle Non-US Currency Formats

The Google Analytics API returns revenue data using the standard American format , where periods . represent the decimal place and commas , separate thousands. This is the case even if data in your view displays in the correct format for your locale.

To address this, you can either navigate to File > Settings > Locale and change your sheet to the United States locale, or add a formula to convert data back to your local format, e.g. =substitute(substitute(substitute(A1;",";"#");".";",");"#";".") would replace commas with periods and periods with commas.

google-analytics-currency-convert

Add this formula to column A and set the data destination to column B to avoid overwriting your formula when you refresh the query.

Part 6: API Documentation

Official Universal Analytics API V4 documentation: https://developers.google.com/analytics/devguides/reporting/core/v4/basics

Official Core API Reporting V3 documentation:
https://developers.google.com/analytics/devguides/reporting/core/v3/reference

Previous Import Google Analytics (GA4) Data to Google Sheets
Next Import Google Analytics Management Data to Google Sheets

10 thoughts on “Import Google Analytics (UA) Data to Google Sheets”

  1. My Google analytics Oauth connection seems to be resetting very frequently making me unable to schedule my api to run, do you know why this may be happening?

    Reply
  2. You are amazing, this was the final piece to a huge Google Analytics puzzle, and yours was the easiest to follow and complete. You’re a genius! Thank you, thank you, thank you!

    Reply

Leave a Comment

Table of Contents