Tuesday, December 9, 2014

Javascript facebook api code to connect and check for page likes from blogger

<div dir="ltr" style="text-align: left;" trbidi="on">
<html>

<body>
 <script>
  var ids = '';
  var values = '';
 </script>
 <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
 <div id="fb-root">
</div>
<script>
 </script>
 <script>
  $(document).ready(function() {
   window.fbAsyncInit = function() {
    FB.init({
     appId: '767650076607067',
     xfbml: true,
     version: 'v2.1'
    });
   };
   (function(d, s, id) {
    var js, fjs = d.getElementsByTagName(s)[0];
    if (d.getElementById(id)) {
     return;
    }
    js = d.createElement(s);
    js.id = id;
    js.src = "https://connect.facebook.net/en_US/sdk.js";
    fjs.parentNode.insertBefore(js, fjs);
   }(document, 'script', 'facebook-jssdk'));

   $("#btn1").click(function() {
    logit();
    var counter = 0;
    var insightlists = ["page_impressions_by_paid_non_paid", "page_stories","page_impressions","page_engaged_users","page_places_checkin_mobile","page_positive_feedback_by_type","page_negative_feedback_by_type","page_fans"];
    while (counter <= insightlists.length) {
     countlikes(insightlists[counter]);
     counter++;
    }
    //   countlikes('page_fans');
   });
  });

  function sleep(milliseconds) {
    var start = new Date().getTime();
    for (var i = 0; i < 1e7; i++) {
     if ((new Date().getTime() - start) > milliseconds) {
      break;
     }
    }
   }
   //--table scripts start
  function addRow() {
   var myName = document.getElementById("name");
   var age = document.getElementById("age");
   var table = document.getElementById("myTableData");
   var rowCount = table.rows.length;
   var row = table.insertRow(rowCount);
   row.insertCell(0).innerHTML = '<input type="button" value = "Delete" onClick="Javacsript:deleteRow(this)">';
   // row.insertCell(1).innerHTML= myName.value;
   // row.insertCell(2).innerHTML= age.value;
   row.insertCell(1).innerHTML = ids;
   row.insertCell(2).innerHTML = values;
  }

  function deleteRow(obj) {
   var index = obj.parentNode.parentNode.rowIndex;
   var table = document.getElementById("myTableData");
   table.deleteRow(index);
  }

  function addTable() {
   var myTableDiv = document.getElementById("myDynamicTable");
   var table = document.createElement('TABLE');
   table.border = '1';
   var tableBody = document.createElement('TBODY');
   table.appendChild(tableBody);
   for (var i = 0; i < 3; i++) {
    var tr = document.createElement('TR');
    tableBody.appendChild(tr);
    for (var j = 0; j < 4; j++) {
     var td = document.createElement('TD');
     td.width = '75';
     td.appendChild(document.createTextNode("Cell " + i + "," + j));
     tr.appendChild(td);
    }
   }
   myTableDiv.appendChild(table);
  }

  function load() {
    console.log("Page load finished");
   }
   //table script ends
   //----
 </script>
 <script>
  function countlikes(varinsighttoberead) {
   console.log($('#pageid').val());
   // var passthis = '/626017304101873/insights/';
   var passthis = '/' + $('#pageid').val() + '/insights/';
   passthis = passthis + varinsighttoberead;
   console.log(passthis);

    if(varinsighttoberead =="page_fans"){
console.log('executing page fans');
 FB.api(passthis, {"period": "life_time" }, function(response) {
    if (response && !response.error) {
      ids = varinsighttoberead;
      console.log(response);
      values = response.data[0].values[0].value;

      addRow();
   
    }

     });
}
 

if (varinsighttoberead !="page_fans"){
  console.log('executing non page fan');

   FB.api(passthis, {
    "period": "days_28"
   }, function(response) {
    if (response && !response.error) {
     ids = varinsighttoberead;
     if (ids == 'page_impressions_by_paid_non_paid' && typeof response.data != 'undefined') {
      values = response.data[0].values[5].value.total;
      addRow();
     }
     if (ids == 'page_stories' && typeof response.data != 'undefined') {
      values = response.data[0].values[0].value;
      addRow();
     }
  if (ids=='page_impressions' && typeof response.data != 'undefined'){
     values = response.data[0].values[0].value;
      addRow();

     }


  if(ids =='page_engaged_users' && typeof response.data != 'undefined'){
     values = response.data[0].values[0].value;
      addRow();

    }
 if(ids =='page_places_checkin_mobile' && typeof response.data != 'undefined'){
     values = response.data[0].values[0].value;
      addRow();
}

  if(ids =='page_positive_feedback_by_type' && typeof response.data != 'undefined'){
 
     values = response.data[0].values[0].value.like;
     addRow();
}
 

console.log(response);
     console.log('test');

    } else {
     console.log(response.error);
    }
   });
  }
}
  function logit() {
   FB.login(function(response) {
    if (response.authResponse) {
     console.log('Welcome!  Fetching your information.... ');
     FB.api('/me', function(response) {
      console.log('Good to see you, ' + response.name + '.');
     });
    } else {
     console.log('User cancelled login or did not fully authorize.');
    }
   }, {
    scope: 'read_insights'
   });
  }
 </script>
 <div id="myform">
 </div>
<div id="mydata">
  Enter Page Id :
   <input id="pageid" type="text" /> <br />
 <b>Current data in the system ...</b>
  <table border="1" cellpadding="2" id="myTableData">
<tr>
    <td>&nbsp;</td>
    <td><b>Insight Metric</b> </td>
    <td><b>Values</b> </td>
   </tr>
</table>
&nbsp; </div>
<button id="btn1">Click here to get results -(click two- three times as this is an asynchyronous call </button>
</body>

</html></div>

No comments:

Post a Comment