Add Event.referrer_url and auto-populate from CAPI ParamBuilder
Summary:
Problem
capi-param-builder-nodejs1.3.1 exposesParamBuilder.getReferrerUrl(), and the Node.js SDK’sPreferencealready has theis_referrer_url_allowedgate. What is still missing is (1) areferrer_urlfield onServerEventto land the value in, and (2) the wiring inServerEvent._applyParamBuilderDefaults()that actually consults the ParamBuilder whensetRequestContext(...)is used. This mirrors the PHP change in D106908330, the Ruby change in D106704554, and the Python change in D106700660.Solution
Add
referrer_urltoServerEvent, following the shape ofevent_source_url:
- New
_referrer_url: stringFlow field.- New constructor parameter
referrer_url: stringappended last (afterattribution_data), so existing positional callers stay backward-compatible, plus a matchingparamdoc line and the assignment in the constructor body.get referrer_url()/set referrer_url()accessors and a fluentsetReferrerUrl(), adjacent to theevent_source_urlaccessors.- Inclusion in
normalize()(emitted only when set, right afterevent_source_url).Extend
ServerEvent._applyParamBuilderDefaults()so that after theevent_source_urlhandling it also reads_param_builder.getReferrerUrl()and assigns it to the event’sreferrer_urlwhen:
_preference.isReferrerUrlAllowed()istrue(default), ANDreferrer_urlis currently empty (caller-supplied values always win, matching the precedence rule for fbc/fbp/event_source_url), AND- the builder returned a non-empty URL.
Differential Revision: D106967695
fbshipit-source-id: 805b52dedf4f69f099aacfe193d83389fbb13815
Facebook Business SDK for NodeJS
Introduction
The Facebook Business SDK is a one-stop shop to help our partners better serve their businesses. Partners are using multiple Facebook API’s to serve the needs of their clients. Adopting all these API’s and keeping them up to date across the various platforms can be time consuming and ultimately prohibitive. For this reason Facebook has developed the Business SDK bundling many of its APIs into one SDK to ease implementation and upkeep. The Business SDK is an upgraded version of the Marketing API SDK that includes the Marketing API as well as many Facebook APIs from different platforms such as Pages, Business Manager, Instagram, etc.
This SDK can be used for both server side as well as client side. It comes with ECMAScript 5 bundled minified distribution with source maps of AMD, CommonJS modules, IIFE, as UMD and as Browser Globals.
Quick Start
Business SDK Getting Started Guide
Pre-requisites
Dependencies
Gulp and Bower should be installed globally. Install dependencies:
Checkout
gulpfile.jsfor all available tasks.Register An App
To get started with the SDK, you must have an app registered on developers.facebook.com.
To manage the Marketing API, please visit your App Dashboard and add the Marketing API product to your app.
IMPORTANT: For security, it is recommended that you turn on ‘App Secret Proof for Server API calls’ in your app’s Settings->Advanced page.
Obtain An Access Token
When someone connects with an app using Facebook Login and approves the request for permissions, the app obtains an access token that provides temporary, secure access to Facebook APIs.
An access token is an opaque string that identifies a User, app, or Page.
For example, to access the Marketing API, you need to generate a User access token for your app and ask for the
ads_managementpermission; to access Pages API, you need to generate a Page access token for your app and ask for themanage_pagepermission.Refer to our Access Token Guide to learn more.
For now, we can use the Graph Explorer to get an access token.
Installation
NPM
npm install --save facebook-nodejs-business-sdkBower
bower install --save facebook-nodejs-business-sdkUsage
Access Token
When someone connects with an app using Facebook Login and approves the request for permissions, the app obtains an access token that provides temporary, secure access to Facebook APIs.
An access token is an opaque string that identifies a User, app, or Page.
For example, to access the Marketing API, you need to generate a User access token for your app and ask for the
ads_managementpermission; to access Pages API, you need to generate a Page access token for your app and ask for themanage_pagepermission. Refer to our Access Token Guide to learn more.For now, we can use the Graph Explorer to get an access token.
Api main class
The
FacebookAdsApiobject is the foundation of the Business SDK which encapsulates the logic to execute requests against the Graph API. Once instantiated, the Api object will allow you to start making requests to the Graph API.Facebook Objects
Facebook Ads entities are defined as classes under the
src/objectsdirectory.Fields
Due to the high number of field names in the existing API objects, in order to facilitate your code maintainability, enum-like field objects are provided within each node class. The fields are stored within node object classes which are stored under the
src/objectsdirectory. You can access object properties like this:Read Objects
Requesting an high number of fields may cause the response time to visibly increase, you should always request only the fields you really need.
Create Objects
Update Objects
Delete Objects
Pagination
Since the release of the Facebook Graph API 2.0, pagination is handled through cursors.
Here cursors are defined as in
src\cursor.js. When fetching nodes related to another (Edges) or a collection in the graph, the results are paginated in aCursorclass. Here theCursoris a superpoweredArray(with all it’s native helpful operations) withnextandpreviousmethods that when resolved fills itself with the new set of objects.If you are using cursor to iterate all of your object under your Ad Account, this practice is recommended.
Debugging
A
FacebookAdsApiobject offers a debugging mode that will log all requests. To enable it just callapi.setDebug(true)on an API instance.Style
This package uses type safe javascript. Flow. Inconsistent code will break builds.
SDK Codegen
Our SDK is autogenerated from SDK Codegen. If you want to learn more about how our SDK code is generated, please check this repository.
Join the Facebook Marketing Developer community
License
Facebook Business SDK for NodeJS is licensed under the LICENSE file in the root directory of this source tree.