Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 800
matteorr Avatar asked Jul 13 '16 19:07

matteorr


People also ask

How do I see views in Google Analytics?

Users, sessions and pageviews are among the key metrics Google Analytics provides, and they're very easy to find in your Google Analytics dashboard. Simply go to the Audience - Overview report. At the top is a line graph tracking users over time. You can toggle to see sessions and pageviews, among other metrics.

How do I find my Google Analytics API view ID?

To find the ID of a View/Profile Go to Google Analytics and choose Admin. You will see this Account/Property/View. Choose the right view in the third drop-down, then click on View Settings. ‹ Google Analytics - How to give CDP access to your account?

Why can't I see views in Google Analytics?

If you can't find your view, you may be looking at the wrong Analytics account. This can happen if your Google account has access to multiple Analytics accounts. It's also possible that someone with the Administrator role has only granted you access to specific views.


1 Answers

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()
like image 103
Matt Avatar answered Sep 19 '22 10:09

Matt