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

এই টিউটোরিয়ালটি 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. অনুমোদিত জাভাস্ক্রিপ্ট অরিজিনগুলিকে http://localhost:8080 এ সেট করুন
  5. http://localhost:8080/oauth2callback-অনুমোদিত রিডাইরেক্ট URIS সেট করুন
  6. তৈরি করুন ক্লিক করুন।

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

আপনাকে HelloAnalytics.html নামে একটি ফাইল তৈরি করতে হবে, যাতে আমাদের উদাহরণের জন্য HTML এবং JavaScript কোড থাকবে।

  1. নিচের সোর্স কোডটি HelloAnalytics.html এ কপি করুন বা ডাউনলোড করুন
  2. আপনার ক্লায়েন্ট আইডি দিয়ে '<YOUR_CLIENT_ID>' প্রতিস্থাপন করুন। উপরে তৈরি করা হয়েছে।
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>Hello Analytics - A quickstart guide for JavaScript</title>
</head>
<body>

<button id="auth-button" hidden>Authorize</button>

<h1>Hello Analytics</h1>

<textarea cols="80" rows="20" id="query-output"></textarea>

<script>

  // Replace with your client ID from the developer console.
  var CLIENT_ID = '<YOUR_CLIENT_ID>';

  // Set authorized scope.
  var SCOPES = ['https://www.googleapis.com/auth/analytics.readonly'];


  function authorize(event) {
    // Handles the authorization flow.
    // `immediate` should be false when invoked from the button click.
    var useImmdiate = event ? false : true;
    var authData = {
      client_id: CLIENT_ID,
      scope: SCOPES,
      immediate: useImmdiate
    };

    gapi.auth.authorize(authData, function(response) {
      var authButton = document.getElementById('auth-button');
      if (response.error) {
        authButton.hidden = false;
      }
      else {
        authButton.hidden = true;
        queryAccounts();
      }
    });
  }


function queryAccounts() {
  // Load the Google Analytics client library.
  gapi.client.load('analytics', 'v3').then(function() {

    // Get a list of all Google Analytics accounts for this user
    gapi.client.analytics.management.accounts.list().then(handleAccounts);
  });
}


function handleAccounts(response) {
  // Handles the response from the accounts list method.
  if (response.result.items && response.result.items.length) {
    // Get the first Google Analytics account.
    var firstAccountId = response.result.items[0].id;

    // Query for properties.
    queryProperties(firstAccountId);
  } else {
    console.log('No accounts found for this user.');
  }
}


function queryProperties(accountId) {
  // Get a list of all the properties for the account.
  gapi.client.analytics.management.webproperties.list(
      {'accountId': accountId})
    .then(handleProperties)
    .then(null, function(err) {
      // Log any errors.
      console.log(err);
  });
}


function handleProperties(response) {
  // Handles the response from the webproperties list method.
  if (response.result.items && response.result.items.length) {

    // Get the first Google Analytics account
    var firstAccountId = response.result.items[0].accountId;

    // Get the first property ID
    var firstPropertyId = response.result.items[0].id;

    // Query for Views (Profiles).
    queryProfiles(firstAccountId, firstPropertyId);
  } else {
    console.log('No properties found for this user.');
  }
}


function queryProfiles(accountId, propertyId) {
  // Get a list of all Views (Profiles) for the first property
  // of the first Account.
  gapi.client.analytics.management.profiles.list({
      'accountId': accountId,
      'webPropertyId': propertyId
  })
  .then(handleProfiles)
  .then(null, function(err) {
      // Log any errors.
      console.log(err);
  });
}


function handleProfiles(response) {
  // Handles the response from the profiles list method.
  if (response.result.items && response.result.items.length) {
    // Get the first View (Profile) ID.
    var firstProfileId = response.result.items[0].id;

    // Query the Core Reporting API.
    queryCoreReportingApi(firstProfileId);
  } else {
    console.log('No views (profiles) found for this user.');
  }
}


function queryCoreReportingApi(profileId) {
  // Query the Core Reporting API for the number sessions for
  // the past seven days.
  gapi.client.analytics.data.ga.get({
    'ids': 'ga:' + profileId,
    'start-date': '7daysAgo',
    'end-date': 'today',
    'metrics': 'ga:sessions'
  })
  .then(function(response) {
    var formattedJson = JSON.stringify(response.result, null, 2);
    document.getElementById('query-output').value = formattedJson;
  })
  .then(null, function(err) {
      // Log any errors.
      console.log(err);
  });
}

  // Add an event listener to the 'auth-button'.
  document.getElementById('auth-button').addEventListener('click', authorize);
</script>

<script src="https://apis.google.com/js/client.js?onload=authorize"></script>

</body>
</html>

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

আপনি Analytics API সক্ষম করার পরে এবং নমুনা উত্স কোড সেট আপ করার পরে নমুনাটি চালানোর জন্য প্রস্তুত।

  1. আপনার ওয়েব সার্ভারে HelloAnalytics.html প্রকাশ করুন এবং আপনার ব্রাউজারে পৃষ্ঠাটি লোড করুন৷
  2. ক্লিক করুন অনুমোদন বোতাম, এবং Google Analytics অ্যাক্সেস অনুমোদন করুন.

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

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