After a bunch of iterations, this works to an extent but only returns 50 rows. Using this as my baseline after getting so many errors. How can this be modified to retrieve all links from the account but respect the rate limit…say return 1 request per sec? 🙏🏾
function bitlyStats() {
var accessToken = 'key'; // Please set your access token here.
var fetchUrl = 'https://api-ssl.bitly.com/v4/groups?access_token=' + accessToken; // Get the group information from the api.
var headers = {
'Authorization': 'Bearer '+ accessToken,
};
var params = {
'method' : 'get',
'contentType': 'application/json',
'headers' : headers,
'muteHttpExceptions' : true
};
var response = UrlFetchApp.fetch(fetchUrl, params); // Fetch the response from the api.
var groupInfo = JSON.parse(response.getContentText()); // Parse the response as JSON object.
var groupId = groupInfo.groups[0].guid; // Get the group id from the response.
var fetchUrl2 = 'https://api-ssl.bitly.com/v4/groups/' + groupId + '/bitlinks?access_token=' + accessToken; // Get the bitlinks information from the api using the group id.
var response2 = UrlFetchApp.fetch(fetchUrl2, params); // Fetch the response from the api.
var bitlinksInfo = JSON.parse(response2.getContentText()); // Parse the response as JSON object.
var bitlinks = bitlinksInfo.links; // Get the array of bitlinks from the response.
var activeSheet = SpreadsheetApp.getActiveSheet(); // Get the active Google Sheet.
activeSheet.getRange('A1').setValue('ID'); // Write the header for ID column.
activeSheet.getRange('B1').setValue('Link'); // Write the header for Link column.
activeSheet.getRange('C1').setValue('Custom Bitlinks'); // Write the header for Custom Bitlinks column.
activeSheet.getRange('D1').setValue('Long URL'); // Write the header for Long URL column.
activeSheet.getRange('E1').setValue('Title'); // Write the header for Title column.
activeSheet.getRange('F1').setValue('Created At'); // Write the header for Created At column.
for (var i = 0; i < bitlinks.length; i++) { // Loop through the array of bitlinks.
var bitlink = bitlinks[i]; // Get the current bitlink object.
var id = bitlink.id; // Get the id of the bitlink.
var link = bitlink.link; // Get the link of the bitlink.
var customBitlinks = bitlink.custom_bitlinks; // Get the custom bitlinks of the bitlink.
var longUrl = bitlink.long_url; // Get the long url of the bitlink.
var title = bitlink.title; // Get the title of the bitlink.
var createdAt = bitlink.created_at; // Get the date the bitlink was created.
activeSheet.getRange('A' + (i + 2)).setValue(id); // Write the id to the Google Sheet.
activeSheet.getRange('B' + (i + 2)).setValue(link); // Write the link to the Google Sheet.
activeSheet.getRange('C' + (i + 2)).setValue(customBitlinks); // Write the custom bitlinks to the Google Sheet.
activeSheet.getRange('D' + (i + 2)).setValue(longUrl); // Write the long url to the Google Sheet.
activeSheet.getRange('E' + (i + 2)).setValue(title); // Write the title to the Google Sheet.
activeSheet.getRange('F' + (i + 2)).setValue(createdAt); // Write the date the bitlink was created to the Google Sheet.
}
return bitlinks; // Return the array of bitlinks.
}
2
Answers
This works! Used in production.
Suggestion: Adjust your API call’s parameters
Checking Bitly’s API Documentation, retrieving Bitlinks by group has a default size of 50 items to be returned. I suggest modifying your
fetchUrl2
variable to include the size of the results to be returned.EDIT:
After running further tests, it appears that there’s a size limit of 100. I’m not sure if there’s a higher limit depending on the Bitly subscription but I’ve modified your code to have it run through all of the links returned by the API if it is more than 100.
Try this:
Reference: