diff --git a/examples/PROMOTE_YOUR_PAGE.js b/examples/PROMOTE_YOUR_PAGE.js new file mode 100644 index 00000000..e6a872a0 --- /dev/null +++ b/examples/PROMOTE_YOUR_PAGE.js @@ -0,0 +1,112 @@ +/** + * Copyright (c) Meta Platforms, Inc. and affiliates. + * 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 + */ + +'use strict'; +const bizSdk = require('facebook-nodejs-business-sdk'); +const AdAccount = bizSdk.AdAccount; +const Campaign = bizSdk.Campaign; +const AdSet = bizSdk.AdSet; +const AdCreative = bizSdk.AdCreative; +const Ad = bizSdk.Ad; + +let access_token = ''; +let app_id = ''; +let account_id = 'act_'; +let page_id = ''; +let ads_image_hash = ''; +let page_link = 'https://facebook.com/'; + +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 { + // Step 1: Create an ad campaign + fields = [ + ]; + params = { + 'name': 'My campaign', + 'objective': 'OUTCOME_TRAFFIC', + 'status': 'PAUSED', + 'special_ad_categories': [], + }; + let campaign = await (new AdAccount(account_id)).createCampaign( + fields, + params + ); + let campaign_id = campaign.id; + + // Step 2: Create an adset with some buying options + fields = [ + ]; + params = { + 'name': 'My adset', + 'optimization_goal': 'REACH', + 'bid_amount': '2', + 'daily_budget': '1000', + 'campaign_id': campaign_id, + 'targeting': {'geo_locations':{'countries':['US']}}, + 'status': 'PAUSED', + 'billing_event': 'IMPRESSIONS', + }; + let ad_set = await (new AdAccount(account_id)).createAdSet( + fields, + params + ); + let ad_set_id = ad_set.id; + + + // Step 3: Create an ad creative with your amazing design + // This ad creative is promoting your page + + fields = [ + ]; + params = { + 'name' : 'Creative', + 'object_story_spec' : {'page_id':page_id,'link_data':{'image_hash':ads_image_hash,'link':page_link,'message':'Creative message','call_to_action':{'type':'LIKE_PAGE','value':{'page':page_id}}}}, + 'degrees_of_freedom_spec' : {'creative_features_spec': {'standard_enhancements': {'enroll_status': 'OPT_IN'}}}, + }; + let creative = await (new AdAccount(account_id)).createAdCreative( + fields, + params + ); + let creative_id = creative.id; + + // Step 4: Create an ad under your ad set with your ad creative + 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; + } catch(error) { + console.log(error); + process.exit(1); + } +}();