नमस्ते Analytics API: वेब ऐप्लिकेशन के लिए JavaScript क्विकस्टार्ट

इस ट्यूटोरियल में, Google Analytics खाता ऐक्सेस करने, Analytics एपीआई से क्वेरी करने, एपीआई के रिस्पॉन्स को मैनेज करने, और नतीजे देने के लिए ज़रूरी चरणों के बारे में बताया गया है. इस ट्यूटोरियल में, Core Reporting API v3.0, Management API v3.0, और OAuth2.0 का इस्तेमाल किया गया है.

पहला चरण: Analytics API चालू करना

Google Analytics API का इस्तेमाल शुरू करने से पहले, सेटअप टूल का इस्तेमाल करें. यह आपको Google API कंसोल में प्रोजेक्ट बनाने, एपीआई चालू करने, और क्रेडेंशियल बनाने के बारे में जानकारी देता है.

क्लाइंट आईडी बनाना

क्रेडेंशियल पेज से:

  1. क्रेडेंशियल बनाएं पर क्लिक करें और OAuth क्लाइंट आईडी चुनें.
  2. APPLICATION TYPE के लिए, वेब ऐप्लिकेशन चुनें.
  3. क्रेडेंशियल को नाम दें.
  4. अनुमति वाले JAVAscript OriginS को http://localhost:8080 पर सेट करें
  5. अनुमति वाले रीडायरेक्ट यूआरआई को http://localhost:8080/oauth2callback पर सेट करें
  6. बनाएं पर क्लिक करें.

दूसरा चरण: सैंपल सेटअप करना

आपको HelloAnalytics.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>

तीसरा चरण: सैंपल चलाएं

Analytics API चालू करने के बाद, और सैंपल सोर्स कोड सेट अप करें, ताकि सैंपल चलाने के लिए तैयार हो.

  1. HelloAnalytics.html को अपने वेब सर्वर पर पब्लिश करें और पेज को अपने ब्राउज़र में लोड करें.
  2. 'अनुमति दें' बटन पर क्लिक करें और Google Analytics को ऐक्सेस करने की अनुमति दें.

इन चरणों को पूरा करने के बाद, सैंपल में, अनुमति वाले उपयोगकर्ता के पहले Google Analytics व्यू (प्रोफ़ाइल) का नाम और पिछले सात दिनों के सेशन की संख्या दिखती है.

अनुमति वाले Analytics सेवा ऑब्जेक्ट की मदद से, अब Management API के रेफ़रंस दस्तावेज़ में मिले किसी भी कोड सैंपल को चलाया जा सकता है. उदाहरण के लिए, आपके पास accountSummaries.list तरीके का इस्तेमाल करने के लिए, कोड को बदलने का विकल्प है.