হ্যালো অ্যানালিটিক্স এপিআই: পরিষেবা অ্যাকাউন্টের জন্য পিএইচপি কুইকস্টার্ট

এই টিউটোরিয়ালটি 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. পরিষেবা অ্যাকাউন্ট পৃষ্ঠা খুলুন। অনুরোধ করা হলে, একটি প্রকল্প নির্বাচন করুন.
  2. পরিষেবা অ্যাকাউন্ট তৈরি করুন ক্লিক করুন, পরিষেবা অ্যাকাউন্টের জন্য একটি নাম এবং বিবরণ লিখুন। আপনি ডিফল্ট পরিষেবা অ্যাকাউন্ট আইডি ব্যবহার করতে পারেন, বা একটি ভিন্ন, অনন্য একটি চয়ন করতে পারেন৷ হয়ে গেলে Create এ ক্লিক করুন।
  3. নিম্নলিখিত পরিষেবা অ্যাকাউন্ট অনুমতি (ঐচ্ছিক) বিভাগ প্রয়োজন নেই। অবিরত ক্লিক করুন.
  4. ব্যবহারকারীদের এই পরিষেবা অ্যাকাউন্টের স্ক্রিনে অ্যাক্সেস মঞ্জুর করুন , কী তৈরি করুন বিভাগে নিচে স্ক্রোল করুন। Create key এ ক্লিক করুন।
  5. প্রদর্শিত পার্শ্ব প্যানেলে, আপনার কীটির বিন্যাস নির্বাচন করুন: JSON প্রস্তাবিত৷
  6. তৈরি করুন ক্লিক করুন। আপনার নতুন পাবলিক/প্রাইভেট কী জোড়া তৈরি এবং আপনার মেশিনে ডাউনলোড করা হয়েছে; এটি এই কীটির একমাত্র অনুলিপি হিসাবে কাজ করে। কীভাবে এটি নিরাপদে সঞ্চয় করা যায় সে সম্পর্কে তথ্যের জন্য, পরিষেবা অ্যাকাউন্ট কীগুলি পরিচালনা করা দেখুন।
  7. আপনার কম্পিউটার ডায়ালগে সংরক্ষিত ব্যক্তিগত কী- তে ক্লোজ ক্লিক করুন, তারপর আপনার পরিষেবা অ্যাকাউন্টের টেবিলে ফিরে যেতে সম্পন্ন ক্লিক করুন।

Google Analytics অ্যাকাউন্টে পরিষেবা অ্যাকাউন্ট যোগ করুন

নতুন তৈরি পরিষেবা অ্যাকাউন্টে একটি ইমেল ঠিকানা থাকবে, <projectId>-<uniqueId>@developer.gserviceaccount.com ; আপনি API-এর মাধ্যমে অ্যাক্সেস করতে চান এমন Google বিশ্লেষণ অ্যাকাউন্টে একজন ব্যবহারকারী যোগ করতে এই ইমেল ঠিকানাটি ব্যবহার করুন। এই টিউটোরিয়ালের জন্য শুধুমাত্র Read & Analyze অনুমতি প্রয়োজন।

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

আপনি পিএইচপি রিলিজ ডাউনলোড বা কম্পোজার ব্যবহার করার জন্য Google APIs ক্লায়েন্ট লাইব্রেরি পেতে পারেন:

composer require google/apiclient:^2.0

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

আপনাকে HelloAnalytics.php নামে একটি একক ফাইল তৈরি করতে হবে, যাতে নীচের নমুনা কোড থাকবে৷

  1. HelloAnalytics.php এ নিম্নলিখিত সোর্স কোডটি কপি বা ডাউনলোড করুন
  2. নমুনা কোড হিসাবে একই ডিরেক্টরির মধ্যে পূর্বে ডাউনলোড করা service-account-credentials.json সরান।

HelloAnalytics.php

<?php

// Load the Google API PHP Client Library.
require_once __DIR__ . '/vendor/autoload.php';

$analytics = initializeAnalytics();
$profile = getFirstProfileId($analytics);
$results = getResults($analytics, $profile);
printResults($results);

function initializeAnalytics()
{
  // Creates and returns the Analytics Reporting service object.

  // Use the developers console and download your service account
  // credentials in JSON format. Place them in this directory or
  // change the key file location if necessary.
  $KEY_FILE_LOCATION = __DIR__ . '/service-account-credentials.json';

  // Create and configure a new client object.
  $client = new Google_Client();
  $client->setApplicationName("Hello Analytics Reporting");
  $client->setAuthConfig($KEY_FILE_LOCATION);
  $client->setScopes(['https://www.googleapis.com/auth/analytics.readonly']);
  $analytics = new Google_Service_Analytics($client);

  return $analytics;
}

function getFirstProfileId($analytics) {
  // Get the user's first view (profile) ID.

  // Get the list of accounts for the authorized user.
  $accounts = $analytics->management_accounts->listManagementAccounts();

  if (count($accounts->getItems()) > 0) {
    $items = $accounts->getItems();
    $firstAccountId = $items[0]->getId();

    // Get the list of properties for the authorized user.
    $properties = $analytics->management_webproperties
        ->listManagementWebproperties($firstAccountId);

    if (count($properties->getItems()) > 0) {
      $items = $properties->getItems();
      $firstPropertyId = $items[0]->getId();

      // Get the list of views (profiles) for the authorized user.
      $profiles = $analytics->management_profiles
          ->listManagementProfiles($firstAccountId, $firstPropertyId);

      if (count($profiles->getItems()) > 0) {
        $items = $profiles->getItems();

        // Return the first view (profile) ID.
        return $items[0]->getId();

      } else {
        throw new Exception('No views (profiles) found for this user.');
      }
    } else {
      throw new Exception('No properties found for this user.');
    }
  } else {
    throw new Exception('No accounts found for this user.');
  }
}

function getResults($analytics, $profileId) {
  // Calls the Core Reporting API and queries for the number of sessions
  // for the last seven days.
   return $analytics->data_ga->get(
       'ga:' . $profileId,
       '7daysAgo',
       'today',
       'ga:sessions');
}

function printResults($results) {
  // Parses the response from the Core Reporting API and prints
  // the profile name and total sessions.
  if (count($results->getRows()) > 0) {

    // Get the profile name.
    $profileName = $results->getProfileInfo()->getProfileName();

    // Get the entry for the first entry in the first row.
    $rows = $results->getRows();
    $sessions = $rows[0][0];

    // Print the results.
    print "First view (profile) found: $profileName\n";
    print "Total sessions: $sessions\n";
  } else {
    print "No results found.\n";
  }
}


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

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

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

php HelloAnalytics.php

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

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