matteorr
matteorr

Reputation: 143

How to get a list of views (profiles) in google analytics api v4

I'm trying to upgrade my script from using version 3 of the google analytics API to version 4.

In version 3, I could get listings of accounts, properties, and views from the api (see API reference for version 3). However, the API reference for version 4 does not seem to show the same thing.

How do I get those listings now?

Upvotes: 12

Views: 6341

Answers (1)

Matt
Matt

Reputation: 5168

TLDR: You get the view listings the same way you always have.

The Analytics Reporting API V4 is a stand alone API for querying an Analytics View for data. There is not V4 management API, only the Analytics Management API V3. The two APIs are designed to be used together.

To load both the V3 and V4 libraries in Python:

from apiclient.discovery import build;

analytics = build('analytics', 'v3', http=http)
analyticsReporting = build('analyticsreporting','v4', http=http)

The best way to list all the views of a user is to call accountsummaries.list() -- See the method reference docs for details.

account_summaries = analytics.management().accountSummaries().list().execute()

Parse the response to get the viewId of interest, and call the V4 API:

response = analyticsreporting.reports().batchGet(
  body={
    "reportRequests":[
    {
      "viewId": viewId,
      "dateRanges":[
        {
          "startDate":"2015-06-15",
          "endDate":"2015-06-30"
        }],
      "metrics":[
        {
          "expression":"ga:sessions"
        }],
      "dimensions": [
        {
          "name":"ga:browser"
        }]
      }]
  }
).execute()

Upvotes: 17

Related Questions