Uncategorized

Retrieving Facebook Leads into SFMC via CloudPages in Real-Time

When the Lead Capture application is not flexible enough to retrieve the Facebook Leads into SFMC Account, you can use the webhooks to get them in real-time, and here is the recipe for it.

Facebook has highly configurable and complex tools, APIs, and technologies for running campaigns on the platform. Surfing into the terms such as Facebook PagesAppToken TypesPermissionsBusiness IntegrationLead RetrievalWebhooksCampaigns/Ad Set/Ad FormTracking Parameters and all the official documentation (there is a world out there, and it’s calling my your name) is sometimes intimidating for getting started with Facebook Leads integration. Therefore, I will just walk the walk with the noticeably short introduction for each term. You can refer to the links for the details.

Facebook updates its API frequently, please have in mind that the article might be obsolete at the time when you read it. (you might want to refer this page for any upcoming updates about permissions here: unfortunately, the implementation example there is neither the latest nor the greatest).

Let’s start!!!

1.      Facebook Application and Settings:

2.      Connecting the Page with the Facebook Application

  • Complete “Verify Your Business on Facebook”
  • Get Started > Select the Facebook Page which should be related to your app.
  • Afterward, you need to select the related pages for the application when generating page token via Graph API Explorer (as mentioned later).

3.      App Review and Permissions Request

You need to request ads_managementleads_retrieval and pages_manage_ads. You can request them all together. Once your request is approved, you will able to add these permission scopes to the token in live mode (production).

Request Tips: https://github.com/ediccio/sfmc-facebook-integration/blob/master/fb-app-review.txt

4.      Facebook Ads, Ad Forms:

You can coordinate the creation of Lead Ads with your Marketing Team which can be done by Ads Manager in Business Manager or Facebook Page, directly.

Fields Naming is important in order to map the fields correctly in our webhook implementation. Check our example CloudPage that demonstrates how to read by field name.

Important Topics:

  • Tracking Parameters (hidden fields):
  • Test Ad Leads Tool: This works only in Live mode. Your App Review needs to be approved for testing the webhook properly. Once everything is done, you can use this tool for running end-to-end integration testing.

5. Webhook Page by SFMC CloudPages

You can use the implementation example for creating your CloudPages that will receive webhook calls: https://github.com/ediccio/sfmc-facebook-integration/blob/master/sfmc-facebook.html.

if (requestMethod == 'GET') { // Subscription

var verifyToken = Platform.Request.GetQueryStringParameter("hub.verify_token");

var challenge = Platform.Request.GetQueryStringParameter("hub.challenge");



if (verifyToken !== VERIFY_TOKEN) {

    Write(Stringify({ success: false, reason: 'Verify token does not match' }));

} else {

    // We echo the received challenge back to Facebook to finish the verification process.

    Write(challenge);
  }

}

GET handling is used for subscribing the facebook webhook as shown below with screenshots

  • Copy the APP ID.
No alt text provided for this image
  • GET APD-ID/subscriptions via Graph Explorer. Check whether it is empty or not.
No alt text provided for this image
  • POST to set your webhook as shown below.
No alt text provided for this image
  • GET Subscription to verify your POST
No alt text provided for this image
  • You can now see the Webhooks in the products list. You can start triggering dummy test to your subscribed webhook (only work in development mode).
No alt text provided for this image

6. Set LeadGen for the Facebook Page:

  • Go to Graph Explorer, Select Facebook App (the first drop-down list in Access Token section), then click to Get Page Access Token, follow the steps below.

a.      Check the Facebook Page which is related.

No alt text provided for this image

b.      Set the permissions

No alt text provided for this image

c.      Pay attention to the Business Integrations Settings link which might be helpful to troubleshoot permission issues, later.

No alt text provided for this image
  • GET Page-ID/subscribed_apps (or me/subscribed_apps, ensure that the Page is selected for the second drop-down list in access token section)
No alt text provided for this image
  • POST Page-ID/subscribed_apps
No alt text provided for this image
  • GET subscribed_app to verify your POST.
No alt text provided for this image

7. Token that never expires (Long-lived Access Token):

  • Select Facebook App and Select Facebook Page
  • GET /me?fields=access_token
  • Click blue info icon
  • Click to “Open in Access Token Tool” button
No alt text provided for this image
  •  Click to “Extend Access Token” button.
  • Click to “Debug” button.
No alt text provided for this image

7.      Verify the token does not expire and its permission for lead retrieval.

No alt text provided for this image

You can now use the token for your webhook, line 20 https://github.com/ediccio/sfmc-facebook-integration/blob/master/sfmc-facebook.html.

IMPORTANT: Do not forget that tokens are not interchangeable between Development and Live mode.

Leave a Reply

Your email address will not be published. Required fields are marked *