How to access accountUserLinks in the Google Analtyics managment api with python?

102 Views Asked by At

I'm looking for the library for this method analytics.management().accountUserLinks().insert

everytime I try to run it the error is always the same, the method management() doesn't exists into the analytics library. I've got this from the documentation so I think it should works.

I've tried to download different python libraries without success.

1

There are 1 best solutions below

0
On

The Google analytics management api. Is part of the Google apis library.

Which means you can use the google-api-python-client

sudo pip install --upgrade google-api-python-client

sample

"""A simple example of how to access the Google Analytics API."""

import argparse

from googleapiclient.discovery import build
import httplib2
from oauth2client import client
from oauth2client import file
from oauth2client import tools

# If modifying these scopes, delete the file token.json.
SCOPES = ['https://www.googleapis.com/auth/analytics.readonly', 'https://www.googleapis.com/auth/analytics.manage.users.readonly']

# Service account key file
CREDENTIALS = 'C:\YouTube\dev\credentials.json'

VIEW_ID = '78110423'

def get_service(api_name, api_version, scope, client_secrets_path):
  """Get a service that communicates to a Google API.

  Args:
    api_name: string The name of the api to connect to.
    api_version: string The api version to connect to.
    scope: A list of strings representing the auth scopes to authorize for the
      connection.
    client_secrets_path: string A path to a valid client secrets file.

  Returns:
    A service that is connected to the specified API.
  """
  # Parse command-line arguments.
  parser = argparse.ArgumentParser(
      formatter_class=argparse.RawDescriptionHelpFormatter,
      parents=[tools.argparser])
  flags = parser.parse_args([])

  # Set up a Flow object to be used if we need to authenticate.
  flow = client.flow_from_clientsecrets(
      client_secrets_path, scope=scope,
      message=tools.message_if_missing(client_secrets_path))

  # Prepare credentials, and authorize HTTP object with them.
  # If the credentials don't exist or are invalid run through the native client
  # flow. The Storage object will ensure that if successful the good
  # credentials will get written back to a file.
  storage = file.Storage(api_name + '.dat')
  credentials = storage.get()
  if credentials is None or credentials.invalid:
    credentials = tools.run_flow(flow, storage, flags)
  http = credentials.authorize(http=httplib2.Http())

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

  return service


def get_first_profile_id(service):
  # Use the Analytics service object to get the first profile id.

  # Get a list of all Google Analytics accounts for the authorized user.
  accounts = service.management().accounts().list().execute()

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

    account_links = service.management().accountUserLinks().list(
        accountId=account
    ).execute()

    if account_links.get('items', []):
        # return the first view (profile) id.
        return account_links.get('items', [])

  return None

def print_results(results):
    print(results)


def main():

  # Authenticate and construct service.
  service = get_service('analytics', 'v3', SCOPES, CREDENTIALS)
  profile = get_first_profile_id(service)
  print_results(profile)


if __name__ == '__main__':
  main()