facebook-nodejs-business-sdk
Version:
SDK for the Facebook Ads API in Javascript and Node.js
125 lines (115 loc) • 2.96 kB
JavaScript
/**
* Copyright (c) 2017-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the license found in the
* LICENSE file in the root directory of this source tree.
* @flow
*/
const bizSdk = require('facebook-nodejs-business-sdk');
const process = require('process');
const AdAccount = bizSdk.AdAccount;
const Campaign = bizSdk.Campaign;
const AdSet = bizSdk.AdSet;
const AdCreative = bizSdk.AdCreative;
const Ad = bizSdk.Ad;
const AdPreview = bizSdk.AdPreview;
let app_id = '1224202007596125';
let access_token = 'EAARZAZA73LzF0BACZADSZCYwMsLHUTwAAYKm5Tciz5GZCGM8ZAtqoM12q8ybFt6dpElSqbUuXm77dGmZAeK6r1wygAQZCslDqpsVQY6RQA3WhLirP8BcgoKoCTvlyKzkx6xGPZCcRiDXUleDqSbnAQBd5k0ZBhuLf1AB2VsIS5OlYDoTQlOoRuVpmZAiJY9O6d688sZD';
let app_secret = '709348c9665c33a4f988ff3950098131';
let account_id = 'act_134983276956882';
let page_id = '190446001681363';
const api = bizSdk.FacebookAdsApi.init(
access_token
);
const showDebugingInfo = true; // Setting this to true shows more debugging info.
if (showDebugingInfo) {
api.setDebug(true);
}
const logApiCallResult = (apiCallName, data) => {
console.log(apiCallName);
if (showDebugingInfo) {
console.log('Data:' + JSON.stringify(data));
}
};
let fields, params;
void async function() {
try {
// _DOC oncall [business_api]
// _DOC open [PROMOTE_YOUR_PAGE]
// _DOC vars [client_access_token, account_id, page_id]
fields = [
];
params = {
'objective' : 'PAGE_LIKES',
'status' : 'PAUSED',
'buying_type' : 'AUCTION',
'name' : 'My Campaign',
};
let campaign = await (new AdAccount(account_id)).createCampaign(
fields,
params
);
let campaign_id = campaign.id;
fields = [
];
params = {
'status' : 'PAUSED',
'targeting' : {'geo_locations': {'countries': ['US']}},
'daily_budget' : '1000',
'billing_event' : 'IMPRESSIONS',
'bid_amount' : '20',
'campaign_id' : campaign_id,
'optimization_goal' : 'PAGE_LIKES',
'promoted_object' : {'page_id': page_id},
'name' : 'My AdSet',
};
let ad_set = await (new AdAccount(account_id)).createAdSet(
fields,
params
);
let ad_set_id = ad_set.id;
fields = [
];
params = {
'body' : 'Like My Page',
'image_url' : 'https://static.xx.fbcdn.net/rsrc.php/v3/yu/r/66zXtGTxCWr.png',
'name' : 'My Creative',
'object_id' : page_id,
'title' : 'My Page Like Ad',
};
let creative = await (new AdAccount(account_id)).createAdCreative(
fields,
params
);
let creative_id = creative.id;
fields = [
];
params = {
'status' : 'PAUSED',
'adset_id' : ad_set_id,
'name' : 'My Ad',
'creative' : {'creative_id': creative_id},
'ad_format' : 'DESKTOP_FEED_STANDARD',
};
let ad = await (new AdAccount(account_id)).createAd(
fields,
params
);
let ad_id = ad.id;
fields = [
];
params = {
'ad_format' : 'DESKTOP_FEED_STANDARD',
};
let ad_previews = await (new Ad(ad_id)).getPreviews(
fields,
params
);
// _DOC close [PROMOTE_YOUR_PAGE]
}
catch(error) {
console.log(error);
process.exit(1);
}
}();