Supermetrics Support Forum

Custom JSON

image



How would I fill that in using lines from this script?


// the name of the sheet within your document

var sheetName = "Sheet1";

// the name of the Instagram account you want to track

var instagramAccountName = "username";

 

function insertFollowerCount() {

  var ss = SpreadsheetApp.getActiveSpreadsheet();

  var sheet = ss.getSheetByName(this.sheetName);

  accountdata = getInstagramData(this.instagramAccountName);

  sheet.appendRow([Utilities.formatDate(new Date(), "CST", "yyyy-MM-dd"), accountdata.followerCount, accountdata.followCount, accountdata.mediaCount,accountdata.totalLikes, accountdata.totalComments, accountdata.EngagementRatio]);

 };

 

function getInstagramData(username) {

  var r = new RegExp('<script type="text\/javascript">' +

                   '([^{]+?({.*profile_pic_url.*})[^}]+?)' +

                   '<\/script>');

  var url = "https://www.instagram.com/" + username, totalComments = 0, totalLikes = 0;

  var source = UrlFetchApp.fetch(url).getContentText();

  var jsonStr = source.match(r)[2];

  var data = JSON.parse(jsonStr);

  console.log('data', data);

  var oldVariantOfData = data['entry_data']['ProfilePage'][0];

  console.log('oldVariantOfData', oldVariantOfData);

    for(var i = 0; i < 12; i++) {

      totalComments += parseInt(oldVariantOfData.graphql.user.edge_owner_to_timeline_media.edges[i].node.edge_media_to_comment.count);

      Logger.log(totalComments);

       };

   for(var l = 0; l < 12; l++) {

          totalLikes += parseInt(oldVariantOfData.graphql.user.edge_owner_to_timeline_media.edges[l].node.edge_liked_by.count);

          Logger.log(totalLikes);

        };

  return {

    followerCount : oldVariantOfData.graphql.user.edge_followed_by.count,

    followCount : oldVariantOfData.graphql.user.edge_follow.count,

    mediaCount : oldVariantOfData.graphql.user.edge_owner_to_timeline_media.count,

    totalComments : totalComments,

    totalLikes : totalLikes,

    EngagementRatio : ((((totalLikes+totalComments))/oldVariantOfData.graphql.user.edge_followed_by.count)/12)*100

  }

}



  • Hi,

    That field saved here in the SupermetricsQueries sheet (if hidden, click Add-ons->Supermetrics->Manage queries). So I think if you can get your script to drop the URL value here, surrounded with [" "], the query should refresh using that value:


    image




    Let us know if that works, or if you have issues with getting it to run properly when doing that. 


    -Thank you

    Supermetrics Team


Login to post a comment