হ্যালো অ্যানালিটিক্স এপিআই: ইনস্টল করা অ্যাপ্লিকেশনের জন্য পাইথন কুইকস্টার্ট

এই টিউটোরিয়ালটি Google Analytics অ্যাকাউন্ট অ্যাক্সেস করতে, অ্যানালিটিক্স API গুলিকে জিজ্ঞাসা করতে, API প্রতিক্রিয়াগুলি পরিচালনা করতে এবং ফলাফলগুলি আউটপুট করার জন্য প্রয়োজনীয় পদক্ষেপগুলির মধ্য দিয়ে চলে। এই টিউটোরিয়ালে Core Reporting API v3.0 , Management API v3.0 , এবং OAuth2.0 ব্যবহার করা হয়েছে৷

ধাপ 1: Analytics API সক্ষম করুন

Google Analytics API ব্যবহার শুরু করার জন্য, আপনাকে প্রথমে সেটআপ টুল ব্যবহার করতে হবে, যা আপনাকে Google API কনসোলে একটি প্রকল্প তৈরি, API সক্ষম করা এবং শংসাপত্র তৈরি করার মাধ্যমে গাইড করে৷

একটি ক্লায়েন্ট আইডি তৈরি করুন

শংসাপত্র পৃষ্ঠা থেকে:

  1. ক্রিয়েট ক্রেডেনশিয়াল ক্লিক করুন এবং OAuth ক্লায়েন্ট আইডি নির্বাচন করুন।
  2. আবেদন প্রকারের জন্য অন্য নির্বাচন করুন।
  3. শংসাপত্রের নাম দিন।
  4. তৈরি করুন ক্লিক করুন।

আপনি এইমাত্র তৈরি করা শংসাপত্রটি নির্বাচন করুন এবং JSON ডাউনলোড করুন ক্লিক করুন। ডাউনলোড করা ফাইলটিকে client_secrets.json হিসাবে সংরক্ষণ করুন; আপনার টিউটোরিয়াল পরে এটি প্রয়োজন.

ধাপ 2: Google ক্লায়েন্ট লাইব্রেরি ইনস্টল করুন

আপনি হয় একটি প্যাকেজ ম্যানেজার ব্যবহার করতে পারেন বা পাইথন ক্লায়েন্ট লাইব্রেরি ম্যানুয়ালি ডাউনলোড এবং ইনস্টল করতে পারেন:

পিপ

পাইথন প্যাকেজ ইনস্টল করার জন্য প্রস্তাবিত টুল pip ব্যবহার করুন:

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

সেটআপ টুলস

setuptools প্যাকেজে অন্তর্ভুক্ত easy_install টুলটি ব্যবহার করুন:

sudo easy_install --upgrade google-api-python-client

ম্যানুয়াল ইনস্টলেশন

পাইথনের জন্য সর্বশেষ ক্লায়েন্ট লাইব্রেরি ডাউনলোড করুন, কোডটি আনপ্যাক করুন এবং চালান:

sudo python setup.py install

Python সিস্টেমে ইনস্টল করার জন্য আপনাকে সুপার ইউজার ( sudo ) বিশেষাধিকার সহ কমান্ডটি ব্যবহার করতে হতে পারে।

ধাপ 3: নমুনা সেটআপ করুন

আপনাকে HelloAnalytics.py নামে একটি ফাইল তৈরি করতে হবে, যাতে প্রদত্ত নমুনা কোড থাকবে।

  1. HelloAnalytics.py এ নিম্নলিখিত সোর্স কোডটি কপি বা ডাউনলোড করুন
  2. নমুনা কোড হিসাবে একই ডিরেক্টরির মধ্যে পূর্বে ডাউনলোড করা client_secrets.json সরান।
"""A simple example of how to access the Google Analytics API."""

import argparse

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


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')

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

    if properties.get('items'):
      # Get the first property id.
      property = properties.get('items')[0].get('id')

      # Get a list of all views (profiles) for the first property.
      profiles = service.management().profiles().list(
          accountId=account,
          webPropertyId=property).execute()

      if profiles.get('items'):
        # return the first view (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
  # for the number of sessions in the past seven days.
  return service.data().ga().get(
      ids='ga:' + profile_id,
      start_date='7daysAgo',
      end_date='today',
      metrics='ga:sessions').execute()


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

  else:
    print 'No results found'


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

  # Authenticate and construct service.
  service = get_service('analytics', 'v3', scope, 'client_secrets.json')
  profile = get_first_profile_id(service)
  print_results(get_results(service, profile))


if __name__ == '__main__':
  main()

ধাপ 4: নমুনা চালান

আপনি অ্যানালিটিক্স এপিআই সক্ষম করার পরে, পাইথনের জন্য Google API ক্লায়েন্ট লাইব্রেরি ইনস্টল করুন এবং নমুনা উত্স কোড সেট আপ করুন নমুনাটি চালানোর জন্য প্রস্তুত৷

ব্যবহার করে নমুনা চালান:

python HelloAnalytics.py
  1. অ্যাপ্লিকেশনটি একটি ব্রাউজারে অনুমোদন পৃষ্ঠাটি লোড করবে।
  2. আপনি যদি ইতিমধ্যে আপনার Google অ্যাকাউন্টে লগ ইন না করে থাকেন, তাহলে আপনাকে লগ ইন করতে বলা হবে৷ আপনি যদি একাধিক Google অ্যাকাউন্টে লগ ইন করে থাকেন, তাহলে অনুমোদনের জন্য ব্যবহার করার জন্য আপনাকে একটি অ্যাকাউন্ট নির্বাচন করতে বলা হবে৷

আপনি যখন এই ধাপগুলি শেষ করেন, নমুনাটি অনুমোদিত ব্যবহারকারীর প্রথম Google Analytics ভিউ (প্রোফাইল) এর নাম এবং গত সাত দিনের সেশনের সংখ্যা প্রকাশ করে৷

অনুমোদিত অ্যানালিটিক্স সার্ভিস অবজেক্টের সাহায্যে আপনি এখন ম্যানেজমেন্ট এপিআই রেফারেন্স ডক্সে পাওয়া যেকোন কোড নমুনা চালাতে পারেন। উদাহরণস্বরূপ আপনি accountSummaries.list পদ্ধতি ব্যবহার করতে কোড পরিবর্তন করার চেষ্টা করতে পারেন।

সমস্যা সমাধান

অ্যাট্রিবিউট ত্রুটি: 'মডিউল_সিক্স_মোভস_উরলিব_পার্স' অবজেক্টের কোনো অ্যাট্রিবিউট নেই 'urlparse'

এই ত্রুটিটি ম্যাক ওএসএক্সে ঘটতে পারে যেখানে "ছয়" মডিউলের ডিফল্ট ইনস্টলেশন (এই লাইব্রেরির একটি নির্ভরতা) পিপ ইনস্টল করার আগে লোড করা হয়। সমস্যাটি সমাধান করতে, PYTHONPATH সিস্টেম এনভায়রনমেন্ট ভেরিয়েবলে পিপের ইনস্টল অবস্থান যোগ করুন:

  1. নিম্নলিখিত কমান্ডের সাহায্যে পিপের ইনস্টলেশন অবস্থান নির্ধারণ করুন:

    pip show six | grep "Location:" | cut -d " " -f2
    

  2. উপরে নির্ধারিত মান দিয়ে <pip_install_path> প্রতিস্থাপন করে আপনার ~/.bashrc ফাইলে নিম্নলিখিত লাইনটি যোগ করুন:

    export PYTHONPATH=$PYTHONPATH:<pip_install_path>
    
  3. নিম্নলিখিত কমান্ডটি ব্যবহার করে যেকোনো খোলা টার্মিনাল উইন্ডোতে আপনার ~/.bashrc ফাইলটি পুনরায় লোড করুন:

    source ~/.bashrc