Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8378311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:54:07+00:00 2026-06-09T15:54:07+00:00

I am trying to Query the Google Analytics API using Python. I’ve followed the

  • 0

I am trying to Query the Google Analytics API using Python. I’ve followed the example on the documentation. (I’ve made very minor changes to help me debug the problems I’m having). I keep getting a ‘NoneType’ object has no attribute ‘__getitem__‘ which I don’t seem to be able to explain. I’m just following the Example in the Documentation offered by Google which tries to get ga:visits data for the 03/03/2012.

The code I’m running is:

#!/usr/bin/python
# -*- coding: utf-8 -*-

import sys

# import the Auth Helper class
import hello_analytics_api_v3_auth

from apiclient.errors import HttpError
from oauth2client.client import AccessTokenRefreshError

def main(argv):
  # Step 1. Get an analytics service object.
  print "I got here twice"
  service = hello_analytics_api_v3_auth.initialize_service()

  try:
    # Step 2. Get the user's first profile ID.
    profile_id = get_first_profile_id(service)
    print profile_id

    if profile_id:
      # Step 3. Query the Core Reporting API.
      results = get_results(service, profile_id)
      print "I got the results"

      # Step 4. Output the results.
      print_results(results)
      print "I printed the results"

  except TypeError, error:
    # Handle errors in constructing a query.
    print ('There was an error in constructing your query : %s' % error)

  except HttpError, error:
    # Handle API errors.
    print ('Arg, there was an API error : %s : %s' %
           (error.resp.status, error._get_reason()))

  except AccessTokenRefreshError:
    # Handle Auth errors.
    print ('The credentials have been revoked or expired, please re-run '
           'the application to re-authorize')

def get_first_profile_id(service):
  # Get a list of all Google Analytics accounts for this user
  print "I am trying to get first profile ID"
  accounts = service.management().accounts().list().execute()

  if accounts.get('items'):
    # Get the first Google Analytics account
    firstAccountId = accounts.get('items')[0].get('id')

    # Get a list of all the Web Properties for the first account
    webproperties = service.management().webproperties().list(accountId=firstAccountId).execute()

    if webproperties.get('items'):
      # Get the first Web Property ID
      firstWebpropertyId = webproperties.get('items')[0].get('id')

      # Get a list of all Profiles for the first Web Property of the first Account
      profiles = service.management().profiles().list(
          accountId=firstAccountId,
          webPropertyId=firstWebpropertyId).execute()

      if profiles.get('items'):
        # return the first Profile ID
        return profiles.get('items')[0].get('id')

  return None


def get_results(service, profile_id):
  # Use the Analytics Service Object to query the Core Reporting API
  return service.data().ga().get(
      ids='ga:' + profile_id,
      start_date='2012-03-03',
      ##The start date dates range is hard coded here
      ##We have to change this so it becomes an input parameter
      end_date='2012-03-03',
      ##The end date is also hard coded in
      ##Change this to be an input parameter
      ##If you run out of ideas, read the start date off a txt file
      ##And then have the user change the text file before running
      ##the program
      metrics='ga:visits').execute()

def print_results(results):
  # Print data nicely for the user.
  print results
  if results:
    print 'First Profile: %s' % results.get('profileInfo').get('profileName')
    print 'Total Visits: %s' % results.get('rows')[0][0]

  else:
    print 'No results found'

##if __name__ == '__main__':
main(sys.argv)

Which is returning the following results:

I got here twice
I am trying to get first profile ID
REDACT PROFILE ID
I got the results
{u'kind': u'analytics#gaData', u'containsSampledData': False}, u'itemsPerPage': 1000, u'totalsForAllResults': {u'ga:visits': u'0'}, u'columnHeaders': [{u'dataType': u'INTEGER', u'columnType': u'METRIC', u'name': u'ga:visits'}], u'query': {u'max-results': 1000, u'start-date': u'2012-03-03', u'start-index': 1, u'ids': u'ga:REDACTED', u'metrics': [u'ga:visits'], u'end-date': u'2012-03-03'}, u'totalResults': 0, u'id': u'https://www.googleapis.com/analytics/v3/data/ga?ids=ga:4159539&metrics=ga:visits&start-date=2012-03-03&end-date=2012-03-03&start-index=1&max-results=1000', u'selfLink': u'https://www.googleapis.com/analytics/v3/data/ga?ids=ga:4159539&metrics=ga:visits&start-date=2012-03-03&end-date=2012-03-03&start-index=1&max-results=1000'}
First Profile: H - REDACTED
There was an error in constructing your query : 'NoneType' object has no attribute '__getitem__'

Can anyone help us by explaining what’s going wrong and showing us what we need to fix? 🙂

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T15:54:08+00:00Added an answer on June 9, 2026 at 3:54 pm

    Ensure that objects are not None before calling the get function. Maybe

    service.management().profiles().list(
              accountId=firstAccountId,
              webPropertyId=firstWebpropertyId).execute()
    

    is returning None and profiles.get('items') is failing.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to use Google Analytics API to query internal searches that happen on
I'm trying to access my Google spreadsheets using the GData API. I have followed
I am trying to retrieve the html of a Google search query result using
I'm trying to screenscrape the first result of a Google search using Python and
I am trying to use Google language detection API, Right now I am using
I'm trying to scrape using Google I'm Feeling Lucky button. For small query like
I'm trying to query the goo.gl API from inside a Google Apps Script. The
I'm trying to use Google's AJAX (JSON) Web Search API in Python. I'm stuck
I'm trying to use Google visualization's query language to query for a string using
am currently using google feed api to get feed links dynamically. am trying to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.