Finding out where analytics should be determined

I am working with Google Analytics api in python and keep getting error

NameError: name 'analytics' is not defined. 

I searched for a few days on the analyticsapi site and on StackOverflow for an answer. If someone could point me in the direction of the correct documentation or help me here, that would be greatly appreciated. The attached code that I still have.

from apiclient.http import MediaFileUpload
from apiclient.errors import HttpError

try:
  media = MediaFileUpload('Bing_Ad_Test.csv',
                          mimetype='application/octet-stream',
                          resumable=False)
  daily_upload = analytics.management().uploads().uploadData(
      accountId='',
      webPropertyId='',
      customDataSourceId='',
      media_body=media).execute()

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 ('There was an API error : %s : %s' %
         (error.resp.status, error.resp.reason))
+4
source share
2 answers

The error says what is happening: you did not determine the name analyticsthat was used earlier in line 9

daily_upload = analytics.management().uploads().uploadData(

, . , .

https://developers.google.com/analytics/devguides/reporting/core/v3/quickstart/service-py

https://developers.google.com/analytics/devguides/reporting/core/v3/coreDevguide

!

+1

script , . script, . . :

import argparse

from apiclient.discovery import build
from oauth2client.service_account import ServiceAccountCredentials

import httplib2
from oauth2client import client
from oauth2client import file
from oauth2client import tools
from apiclient.http import MediaFileUpload

# SET VARS
SERVICE_ACCOUNT_EMAIL='XXX'
CUSTOM_DATA_SOURCE_ID='XXX'
WEB_PROPERTY_ID='XXX'
ACCOUNT_ID='XXX'
CSV_IMPORT_FILE_LOCATION='XXX'
CREDENTIALS_KEY_FILE_LOCATION='XXX'

def get_service(api_name, api_version, scope, key_file_location,
                service_account_email):

  credentials = ServiceAccountCredentials.from_p12_keyfile(
    service_account_email, key_file_location, scopes=scope)

  http = credentials.authorize(httplib2.Http())

  # Build the service object.
  service = build(api_name, api_version, http=http)

  return service

def uploadCSV(service):
  try:
    media = MediaFileUpload(CSV_IMPORT_FILE_LOCATION,
                          mimetype='application/octet-stream',
                          resumable=False)
    daily_upload = service.management().uploads().uploadData(
        accountId=ACCOUNT_ID,
        webPropertyId=WEB_PROPERTY_ID,
        customDataSourceId=CUSTOM_DATA_SOURCE_ID,
        media_body=media).execute()

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


def main():
  # Define the auth scopes to request.
  scope = ['https://www.googleapis.com/auth/analytics.edit','https://www.googleapis.com/auth/analytics']

  # Authenticate and construct service.
  service = get_service('analytics', 'v3', scope, CREDENTIALS_KEY_FILE_LOCATION,
    SERVICE_ACCOUNT_EMAIL)

  # Upload CSV Data
  uploadCSV(service)


if __name__ == '__main__':
  main()
+2

Source: https://habr.com/ru/post/1657623/


All Articles