API Connector Documentation
Import Google Analytics (UA) Data to Google Sheets
In this guide, we’ll walk through how to pull Google Analytics (UA) data directly into Google Sheets, using the API Connector add-on for Sheets. Specifically, we'll show how to fetch data from the following two APIs:
- Core Reporting API v3 (Universal Analytics)
- Reporting API v4 (Universal Analytics)
Both of these APIs have been deprecated, so you may also want to check out the following article: Import Google Analytics (GA4) Data to Google Sheets
Contents
- Before You Begin
- Part 1: Connect to Google Analytics
- Part 2: Pull Data from Google Analytics into Sheets
- Part 3: Create a Custom Request to the V3 Google Analytics API
- Part 4: V3 Pagination
- Part 5: Create a Custom Request to the V4 Core Reporting API
- Part 6: API Documentation
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.
- In Sheets, open API Connector and create a new request (Extensions > API Connector > Open > Create request)
- Select Google Analytics (UA) from the drop-down list of applications
- Under Authorization, click Connect to Google Analytics (UA)
- You will be asked to allow API Connector to view your Google Analytics data. Click Allow.
- 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.
- Under Endpoint, choose the data you’d like to retrieve. The
/data/ga
endpoint will return your GA data. - 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, - Optionally set any optional parameters such as filters and sorting.
- Select a destination sheet, name your request, and click Run.
- 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 sample request to get Universal Analyics data from the V3 Google Analytics API. Substitute in your own property ID where it says 111111111
.
- Application:
Custom
- Method:
GET
- Request URL:
https://www.googleapis.com/analytics/v3/data/ga?ids=ga:11111111&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
- OAuth:
Google Analytics
- Headers
Content-Type
:application/json
- JMESPath (under Output options):
{id:id,profileInfo:profileInfo,rows:rows}
Notes
- 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: V3 Pagination
By default, Google Analytics' V3 API sends back just 10,000 records at a time. To get more, set the limit
parameter to 10000 and use the next page link in their response, like this:
- Pagination type:
next page URL
- Next page path:
nextLink
- Run until: choose when to stop fetching data
Part 5: Create a Custom Request to the V4 Google Analytics API
Here is a sample request to get Universal Analyics data from the V4 Google Analytics API. The v4 API works by including parameters in the request body instead of the URL. Again, substitute in your own view ID.
- Application:
Custom
- Method:
POST
- Request URL:
https://analyticsreporting.googleapis.com/v4/reports:batchGet
- OAuth:
Google Analytics
- Headers
Content-Type
: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 (under Output options):
reports[].data.rows[]
Notes:
- To give your column headers more reader-friendly names, click Edit Fields and name your fields in the field editor.
- 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 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
How do you extract data from your Google Ads campaign?
Hey Rami, you can use our Google Ads connection.
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?
Sorry, there was an issue - fixed now!
There no longer is a View or View ID for GA4. What GA4 field should be used to connect to the data?
Looks like the Google analytics Reporting API doesn't support GA4 accounts. You will need to use the new analytics data api There is currently very little documentation for this api but im sure it will arrive in the coming months. ( https://developers.google.com/analytics/devguides/reporting/data/v1 )
Thanks, John! Yeah this article is for Universal Analytics, the Google Analytics Reporting API v4 doesn't work for the new GA4, which is a bit confusing. GA4 uses the new Data API, which is still in Beta. I'll investigate it and update this article.
Update: API Connector's Google Analytics connection now supports GA4, I've created a new article with info on that.
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!
What a nice comment, thank you!
Hello! When I try to run the request, I get a 403-message saying I do not have sufficient permissions. I am an Admin, so I am not sure why that's the case. Any ideas of what I could be doing wrong?
Thank you!
I think that's the error message discussed here, can you please disconnect/reconnect, making sure to accept all requested permissions?