facebook-nodejs-business-sdk
Version:
SDK for the Facebook Ads API in Javascript and Node.js
222 lines (203 loc) • 5.62 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 Business = bizSdk.Business;
const ProductCatalog = bizSdk.ProductCatalog;
const ProductSet = bizSdk.ProductSet;
const ProductFeed = bizSdk.ProductFeed;
const ProductFeedUpload = bizSdk.ProductFeedUpload;
const AdAccount = bizSdk.AdAccount;
const CustomAudience = bizSdk.CustomAudience;
const Campaign = bizSdk.Campaign;
const AdSet = bizSdk.AdSet;
const AdCreative = bizSdk.AdCreative;
const Ad = bizSdk.Ad;
const ProductItem = bizSdk.ProductItem;
let app_id = '1224202007596125';
let access_token = 'EAARZAZA73LzF0BACZADSZCYwMsLHUTwAAYKm5Tciz5GZCGM8ZAtqoM12q8ybFt6dpElSqbUuXm77dGmZAeK6r1wygAQZCslDqpsVQY6RQA3WhLirP8BcgoKoCTvlyKzkx6xGPZCcRiDXUleDqSbnAQBd5k0ZBhuLf1AB2VsIS5OlYDoTQlOoRuVpmZAiJY9O6d688sZD';
let app_secret = '709348c9665c33a4f988ff3950098131';
let business_id = '575971192554908';
let ad_account_id = 'act_134983276956882';
let DPApixel_id = '1688112124805706';
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 [DPA]
// create product catalog
fields = [
];
params = {
'name' : 'SDK Test Product Catalog',
};
let catalog = await (new Business(business_id)).createOwnedProductCatalog(
fields,
params
);
let catalog_id = catalog.id;
// Create a product set with product catalog
fields = [
];
params = {
'filter' : {'condition': {'i_contains': 'new'}},
'name' : 'SDK Test Product Set',
};
let product_set = await (new ProductCatalog(catalog_id)).createProductSet(
fields,
params
);
let product_set_id = product_set.id;
// Create a product feed with product catalog
fields = [
];
params = {
'schedule' : {'interval':'DAILY', 'url':'http://www.example.com/sample_feed.tsv', 'hour':2},
'name' : 'SDK Test Product Feed',
};
let feed = await (new ProductCatalog(catalog_id)).createProductFeed(
fields,
params
);
let feed_id = feed.id;
// Upload your feed
fields = [
];
params = {
'url' : 'https://www.facebook.com/images/fb_icon_325x325.png',
};
let upload = await (new ProductFeed(feed_id)).createUpload(
fields,
params
);
// Create a product audience
fields = [
];
params = {
'name' : 'SDK Test Product Audience',
'product_set_id' : product_set_id,
'inclusions' : [{'retention_seconds':86400,'rule':{'event':{'eq':'AddToCart'}}}],
'subtype' : 'WEBSITE',
};
let product_audience = await (new AdAccount(ad_account_id)).createProductAudience(
fields,
params
);
let product_audience_id = product_audience.id;
// create your DPA campaign
fields = [
];
params = {
'name' : 'SDK DPA Test Campaign',
'status' : 'PAUSED',
'objective' : 'PRODUCT_CATALOG_SALES',
'promoted_object' : {'product_catalog_id': catalog_id, 'product_set_id':product_set_id},
};
let dpa_campaign = await (new AdAccount(ad_account_id)).createCampaign(
fields,
params
);
let dpa_campaign_id = dpa_campaign.id;
// Create your ad set.
// You need to create pixel, or use existing pixel
// And accept Pixel Term of Service in your business manager if you haven't do so
fields = [
];
params = {
'campaign_id' : dpa_campaign_id,
'name' : 'SDK DPA Test AdSet',
'billing_event' : 'IMPRESSIONS',
'bid_amount' : '500',
'optimization_goal' : 'OFFSITE_CONVERSIONS',
'daily_budget' : '2500',
'promoted_object' : {'pixel_id':DPApixel_id, 'product_set_id':product_set_id},
'targeting' : {'dynamic_audience_ids': [product_audience_id], 'age_min': '60', 'age_max': '65', 'geo_locations': {'countries': ['US']}},
};
let ad_set = await (new AdAccount(ad_account_id)).createAdSet(
fields,
params
);
let ad_set_id = ad_set.id;
// Create and get your ad creative
fields = [
];
params = {
'object_story_spec' : {'page_id':page_id,'template_data':{'name':'Name','link':'www.pandaparadise.net','description':'Description','message':'Message'}},
'name' : 'SDK DPA Test Creative',
'product_set_id' : product_set_id,
'object_type' : 'page',
'instagram_actor_id' : '1049095995165977',
};
let creative_temp = await (new AdAccount(ad_account_id)).createAdCreative(
fields,
params
);
let creative_temp_id = creative_temp.id;
// Create and get your ad creative
fields = [
'account_id',
'id',
'name',
'object_story_spec',
'product_set_id',
'status',
'title',
];
params = {
};
let creative = await (new AdCreative(creative_temp_id)).get(
fields,
params
);
// Create your ad
fields = [
];
params = {
'adset_id' : ad_set_id,
'status' : 'PAUSED',
'name' : 'SDK Test Ad',
'bid_amount' : '100',
'creative' : creative,
};
let ad = await (new AdAccount(ad_account_id)).createAd(
fields,
params
);
// After feed upload, it takes a few seconds for server to parse the file and actually load the product items
// sleep here for a while if you get null in product_item
// and you can print product_items and preview
fields = [
];
params = {
};
let product_items = await (new ProductFeed(feed_id)).getProducts(
fields,
params
);
// _DOC close [DPA]
}
catch(error) {
console.log(error);
process.exit(1);
}
}();