-
-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathrequest-facebook-report.js
67 lines (51 loc) · 1.91 KB
/
request-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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/**
*
* Export Facebook Ads Data to Google Sheets
*
* Requests a Facebook report asynchronously and caches the report ID
*
* Version: 2.2
*
* Google Apps Script maintained by Frederic Harnois
*
**/
// MODIFY YOUR REPORT HERE //
// ad account ID
const AD_ACCOUNT_ID = 'INSERT_AD_ACCOUNT_ID'
// ad, adset, campaign, account
const LEVEL = 'INSERT_LEVEL'
// https://developers.facebook.com/docs/marketing-api/insights/parameters#fields
const FIELDS = 'INSERT_FIELDS'
// https://developers.facebook.com/docs/marketing-api/insights/parameters#param
const DATE_RANGE = 'INSERT_DATE_RANGE'
// user access token linked to a Facebook app
const TOKEN = 'INSERT_TOKEN'
// number of days from 1 to 90
const TIME_INCREMENT = 'INSERT_TIME_INCREMENT'
// https://developers.facebook.com/docs/marketing-api/insights/parameters#param
const FILTERING = 'INSERT_FILTERS'
// DO NOT MODIFY ANYTHING BELOW //
function requestFacebookReport() {
// Builds the Facebook Ads Insights API URL
const facebookUrl = `https://graph.facebook.com/v7.0/act_${AD_ACCOUNT_ID}/insights?level=${LEVEL}&fields=${FIELDS}&date_preset=${DATE_RANGE}&access_token=${TOKEN}&time_increment=${TIME_INCREMENT}&filtering=${FILTERING}&limit=1000`;
const encodedFacebookUrl = encodeURI(facebookUrl);
const options = {
'method' : 'post'
};
// Fetches & parses the URL
const fetchRequest = UrlFetchApp.fetch(encodedFacebookUrl, options);
const results = JSON.parse(fetchRequest.getContentText());
// Caches the report run ID
const reportId = results.report_run_id;
const cache = CacheService.getScriptCache();
const cached = cache.get('campaign-report-id');
if (cached != null) {
cache.put('campaign-report-id', [], 1);
Utilities.sleep(1001);
cache.put('campaign-report-id', reportId, 21600);
} else {
cache.put('campaign-report-id', reportId, 21600);
};
Logger.log(cache.get('campaign-report-id'));
}