How to retrieve data from Facebook Ads to Google sheets

How to retrieve data from Facebook Ads to Google sheets

If you are running in-app advertisements, organic campaigns, or paid advertisements on Facebook, then you might be wondering how you can export data from Facebook Ads to Google Sheets for a detailed and in-depth analysis. 

Of course, you can always use the CSV data copy and paste method to get data onto the Google Spreadsheet, the old-fashioned way. However, this process is full of errors and also time-consuming. The worst thing is that you might have to go over the whole process whenever there is a need for updating the data once again. 

Nobody wants so much hassle!

In this article, we will show you how you can retrieve data from Facebook ads to Google Spreadsheets using Google Apps Script code. 

Retrieving data from Facebook ads to Google Sheets is very similar to retrieving data from Google Analytics to Google Sheets. 

Both Facebook marketers and Data analysts can benefit by using this Google Apps Script Code. They won’t have to spend hours preparing reports and strategizing who to advertise. The manual work of retrieving data is incredibly reduced with this method. Presenting a huge amount of data could not look simpler than this!

You can use Google Apps Script to retrieve data from Facebook Ads to Google Sheets:

function retrieveFacebookAdData() {
  var sheet = SpreadsheetApp.getActiveSheet();
  
  // Replace 'ACCOUNT_ID' with the actual ID of your Facebook ad account
  var accountId = 'ACCOUNT_ID';
  
  // Replace 'ACCESS_TOKEN' with the actual access token for your Facebook ad account
  var accessToken = 'ACCESS_TOKEN';
  
  // Replace 'START_DATE' and 'END_DATE' with the actual start and end dates for the data you want to retrieve
  var startDate = 'START_DATE';
  var endDate = 'END_DATE';
  
  // Replace 'FIELDS' with the actual fields you want to retrieve
  var fields = 'FIELDS';
  
  var url = 'https://graph.facebook.com/v12.0/' + accountId + '/insights?fields=' + fields + '&time_range={"since":"' + startDate + '","until":"' + endDate + '"}&access_token=' + accessToken;
  
  var response = UrlFetchApp.fetch(url);
  var data = JSON.parse(response.getContentText());
  
  // Write the data to the Google Sheet
  sheet.getRange(1, 1, data.length, data[0].length).setValues(data);
}