-
-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathget-facebook-report.js
44 lines (34 loc) · 1.17 KB
/
get-facebook-report.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/**
*
* Export Facebook Ads Data to Google Sheets
*
* Pushes the Facebook asynchronous report to Google Sheets
*
* Version: 2.2
*
* Google Apps Script maintained by Frederic Harnois
*
**/
// MODIFY YOUR SETTINGS HERE //
// url of the google sheets where the report will be
const SPREADSHEET_URL = 'INSERT_URL'
// name of the sheet where the report will be
const TAB_NAME = 'INSERT_TAB_NAME'
// DO NOT MODIFY ANYTHING BELOW //
function getFacebookReport() {
// Selects the chosen sheet and tab
const ss = SpreadsheetApp.openByUrl(SPREADSHEET_URL);
const sheet = ss.getSheetByName(TAB_NAME);
// Clears the sheet
sheet.clear();
// Gets the Facebook report run ID
const cache = CacheService.getScriptCache();
const reportId = cache.get('campaign-report-id');
// Fetches the report as a csv file
const url = `https://www.facebook.com/ads/ads_insights/export_report?report_run_id=${reportId}&format=csv&access_token=${TOKEN}`;
const fetchRequest = UrlFetchApp.fetch(url);
const results = Utilities.parseCsv(fetchRequest);
// Pastes the csv file in the sheet
sheet.getRange(1,1, results.length, results[0].length).setValues(results);
}