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

এই টিউটোরিয়ালটি 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. APPLICATION TYPE এর জন্য ওয়েব অ্যাপ্লিকেশন নির্বাচন করুন।
  3. শংসাপত্রের নাম দিন।
  4. অনুমোদিত জাভাস্ক্রিপ্ট অরিজিনগুলি ফাঁকা রাখুন, এই টিউটোরিয়ালের জন্য এটির প্রয়োজন নেই।
  5. http://localhost:8080/oauth2callback.php-অনুমোদিত পুনঃনির্দেশিত URIS সেট করুন
  6. তৈরি করুন ক্লিক করুন।

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

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

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

composer require google/apiclient:^2.0

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

আপনাকে দুটি ফাইল তৈরি করতে হবে:

  1. index.php ব্যবহারকারীর দ্বারা পরিদর্শন করা প্রধান পৃষ্ঠা হবে।
  2. oauth2callback.php OAuth 2.0 প্রতিক্রিয়া পরিচালনা করবে।

index.php

এই ফাইলটিতে গুগল অ্যানালিটিক্স এপিআই অনুসন্ধান এবং ফলাফল প্রদর্শনের জন্য প্রধান যুক্তি রয়েছে। প্রথম নমুনা কোডটি index.php এ কপি বা ডাউনলোড করুন

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

// Start a session to persist credentials.
session_start();

// Create the client object and set the authorization configuration
// from the client_secretes.json you downloaded from the developer console.
$client = new Google_Client();
$client->setAuthConfig(__DIR__ . '/client_secrets.json');
$client->addScope(Google_Service_Analytics::ANALYTICS_READONLY);

// If the user has already authorized this app then get an access token
// else redirect to ask the user to authorize access to Google Analytics.
if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
  // Set the access token on the client.
  $client->setAccessToken($_SESSION['access_token']);

  // Create an authorized analytics service object.
  $analytics = new Google_Service_Analytics($client);

  // Get the first view (profile) id for the authorized user.
  $profile = getFirstProfileId($analytics);

  // Get the results from the Core Reporting API and print the results.
  $results = getResults($analytics, $profile);
  printResults($results);
} else {
  $redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/oauth2callback.php';
  header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}


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 "<p>First view (profile) found: $profileName</p>";
    print "<p>Total sessions: $sessions</p>";
  } else {
    print "<p>No results found.</p>";
  }
}

?>

oauth2callback.php

এই ফাইলটি OAuth 2.0 প্রতিক্রিয়া পরিচালনা করে। oauth2callback.php এ দ্বিতীয় নমুনা কোডটি কপি বা ডাউনলোড করুন

<?php

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

// Start a session to persist credentials.
session_start();

// Create the client object and set the authorization configuration
// from the client_secrets.json you downloaded from the Developers Console.
$client = new Google_Client();
$client->setAuthConfig(__DIR__ . '/client_secrets.json');
$client->setRedirectUri('http://' . $_SERVER['HTTP_HOST'] . '/oauth2callback.php');
$client->addScope(Google_Service_Analytics::ANALYTICS_READONLY);

// Handle authorization flow from the server.
if (! isset($_GET['code'])) {
  $auth_url = $client->createAuthUrl();
  header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));
} else {
  $client->authenticate($_GET['code']);
  $_SESSION['access_token'] = $client->getAccessToken();
  $redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/';
  header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}


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

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

পিএইচপি পরিবেশন করার জন্য কনফিগার করা একটি ওয়েব সার্ভারের সাথে নমুনাটি চালান। আপনি যদি PHP 5.4 বা নতুন ব্যবহার করেন, তাহলে আপনি নিম্নলিখিত কমান্ডটি কার্যকর করে PHP-এর বিল্ট-ইন টেস্ট ওয়েব সার্ভার ব্যবহার করতে পারেন:

php -S localhost:8080 /path/to/sample

তারপর আপনার ব্রাউজারে http://localhost:8080 দেখুন।

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

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