Search code examples
pythongoogle-analyticsgoogle-analytics-api

Google Analytics API asks for a variable called `analytics`?


I am using the Google Analytics API to retrieve data. In the documentation Google refers to analytics.

https://developers.google.com/analytics/devguides/config/mgmt/v3/mgmtReference/management/filters/update

try:
  analytics.management().filters().update(
      accountId='123456',
      filterId='1223334444',
      body={
          'name': 'My Domain Filter',
          'type': 'EXCLUDE',
          'excludeDetails': {
              'field': 'GEO_DOMAIN',
              'matchType': 'EQUAL',
              'expressionValue': 'example.com',
              'caseSensitive': False
              }
      }
  ).execute()

There is a note at the top:

# Note: This code assumes you have an authorized Analytics service object.
# See the Filters Developer Guide for details.

Is analytics a service object? I grabbed the variable api_client from elsewhere in my code:

# The real code that initialized the client
    credentials = client.GoogleCredentials(access_token=access_token,
                                           refresh_token=refresh_token,
                                           client_id=client_id,
                                           client_secret=client_secret,
                                           token_uri=token_uri,
                                           token_expiry=token_expiry,
                                           user_agent=user_agent)

    # Initialize Http Protocol
    http = lib2.Http()

    # Authorize client
    authorized = credentials.authorize(http)

    # Let's build the client
    api_client = google_build(serviceName=api_name,
                              version=api_version, http=authorized)

Didn't work.


Solution

  • You're using the management API, this is for configuration/account management. If you want data/reports use the reporting API. https://developers.google.com/analytics/devguides/reporting/core/v4/