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: Fetch Data from Google Analytics

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 other available endpoint (/management/accountSummaries).
    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 human-friendly names, click Edit Fields and name your fields in the field editor.

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

Alternatively, you can run a custom request instead of using API Connector’s built-in integration.

  1. In API Connector, click Create, choose POST from the dropdown menu, and paste https://analyticsreporting.googleapis.com/v4/reports:batchGet into the URL field. All V4 requests use the same URL.
    google-analytics-img3
  2. Choose Google Analytics from the OAuth dropdown menu.
    google-analytics-img6
  3. Under headers enter Key = content-type, Value = application/json
    google-analytics-img4
  4. All details of the request, e.g. metrics, dimensions, and filters, are specified in the request body, like this: {"reportRequests":[{"viewId":"XXXX","dateRanges":[{"startDate":"2021-09-01","endDate":"2021-09-30"}],"metrics":[{"expression":"ga:users"}]}]}
    google-analytics-img5
  5. Create a new tab and click Set current to use that tab as your data destination.
  6. Name your request and click Run. A moment later you’ll see user counts populate your sheet.google-analytics-img7
  7. That’s pretty cool.. but also quite messy looking. The GA v4 Reporting API returns data in a rather strange format, where metrics and dimension names are returned separately from the actual values. To simplify, let’s use the following JMESPath expression: reports[].data.rows[]. This will become even more useful as our queries become more complex, so we’ll keep a JMESPath filter in place for all our requests.google-analytics-img8

More Example V4 API Requests

  • Multiple metrics
{
  "reportRequests":
  [
    {
      "viewId": "100425068",
      "dateRanges": [{"startDate": "2021-09-01", "endDate": "2021-09-30"}],
     "metrics": [
        {"expression": "ga:users"},
        {"expression": "ga:sessions"}
      ],
    }
  ]
}
  • Multiple metrics and dimensions (the “pageSize” parameter indicates how many records to retrieve)
{
  "reportRequests":
  [
    {
      "viewId": "100425068",
      "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
    }
  ]
}
google-analytics-img9

To rename your columns, click Edit Fields.

  • With filters
{
  "reportRequests":
  [
    {
      "viewId": "100425068",
      "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 4: Create a Custom Request to the V3 Core Reporting API

As an alternative to connecting through the V4 API, you can get Universal Analytics data through Google Analytics’ older V3 Core Reporting API.

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.

Instructions:

First, click connect on Google Analytics from the Connections menu, as shown above. Now, set up your request like this:

  • 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}

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 Reply to Avery Arasin Cancel reply

Table of Contents