Import Google Analytics Data to Google Sheets

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 dig into the Google Analytics v4 Reporting API. This API only supports authentication via OAuth2, so we’ll be connecting using API Connector’s built-in OAuth2 integration for Google Analytics. This is a paid feature; please upgrade to access.

PART 1: CONNECT TO THE GOOGLE ANALYTICS API

If you haven’t connected API Connector to Google Analytics before, you’ll first need to initiate the connection as follows:

  1. Open up Google Sheets and click Add-ons > API Connector > Manage Connections.
  2. In the list of available connections, find “Google Analytics” and click Connect.
    google-analytics-img1
  3. You will be directed to google.com and asked to allow API Connector to view your Google Analytics data.
    google-analytics-img2
  4. You’ll then be returned to your Google Sheet, and can verify that your Google Analytics connection is active in the Connections screen.

PART 2: CREATE YOUR GOOGLE ANALYTICS API REQUEST

For our first request, we’ll pull in users for a specific view. The Google Analytics Reporting API v4 is accessed by sending POST requests to this API Request URL:

https://analyticsreporting.googleapis.com/v4/reports:batchGet

All the details of the request, e.g. metrics, dimensions, filters, and so on, are specified in the POST body. We’ll start with a simple request for users in October 2020.

{
  "reportRequests":
  [
    {
      "viewId": "XXXX",
      "dateRanges": [{"startDate": "2020-10-01", "endDate": "2020-10-31"}],
      "metrics": [{"expression": "ga:users"}]
    }
  ]
}

Substitute in your own date range and view ID, which you can find in the Google Analytics account menu.

PART 3: PULL GOOGLE ANALYTICS API DATA INTO SHEETS

We can now enter our values into API Connector and start importing GA data into Google Sheets.

  1. In API Connector, click Create, choose POST from the dropdown menu, and paste in the Request URL we created above.
    google-analytics-img3
  2. Under headers enter Key = content-type, Value = application/json
    google-analytics-img4
  3. In the POST Body section, paste in the POST body we created above.
    google-analytics-img5
  4. Choose ‘Google Analytics’ from the authentication dropdown.
    google-analytics-img6
  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: reports[].data.rows[]. This will become even more useful as our queries become more complex, so we’ll keep this JMESPath filter in place for all our requests.
    google-analytics-img8

PART 4: MORE EXAMPLE API REQUESTS

  • Multiple metrics
{
  "reportRequests":
  [
    {
      "viewId": "100425068",
      "dateRanges": [{"startDate": "2020-10-01", "endDate": "2020-10-30"}],
     "metrics": [
        {"expression": "ga:users"},
        {"expression": "ga:sessions"}
      ],
    }
  ]
}
  • Multiple metrics and dimensions
{
  "reportRequests":
  [
    {
      "viewId": "100425068",
      "dateRanges": [{"startDate": "2020-10-01", "endDate": "2020-10-30"}],
     "metrics": [
        {"expression": "ga:users"},
        {"expression": "ga:sessions"}
      ],
       "dimensions": [
        {"name": "ga:browser"}, 
        {"name": "ga:country"}
      ],
    }
  ]
}
  • With filters
{
  "reportRequests":
  [
    {
      "viewId": "100425068",
      "dateRanges": [{"startDate": "2020-10-01", "endDate": "2020-10-30"}],
     "metrics": [
        {"expression": "ga:users"},
        {"expression": "ga:sessions"}
      ],
      "dimensions": [{"name": "ga:browser"}, {"name": "ga:country"}],
      "dimensionFilterClauses": [
        {
          "filters": [
            {
              "dimensionName": "ga:browser",
              "operator": "EXACT",
              "expressions": ["Chrome"]
            }
          ]
        }
      ]
    }
  ]
}

To see more examples, check the reporting documentation .

Leave a Comment