Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to get Facebook's lead ads sent over to our subscribed endpoint

I'm currently attempting to get Facebook to send over all leads as they're registered in real time to a subscribed endpoint. Facebook mentions in their documentation that this is possible by setting up a realtime update from the new lead ads service.

https://developers.facebook.com/docs/marketing-api/guides/lead-ads/v2.5#setting-up-realtime-updates

However after several hours trying to integrate this this, I've failed to get it to work. Below is the process I've followed so far to no avail.

Setting up the application

I've set up a Facebook application (423332064458136) which is currently tied to my personal account. This application is live, but isn't operating on any platform, as such doesn't require approval through review (as far as I understand).

I've registered a leadgen callback for this application using both the application ID and the application secret. This was achieved using the following curl request..

curl \
-F "object=page" \
-F "callback_url=https://leadr.co.uk/external/handle-fb.php" \
-F "fields=leadgen" \
-F "verify_token=abc123" \
-F "access_token=423332064458136|<APP_SECRET>" \
"https://graph.facebook.com/v2.5/423332064458136/subscriptions"

I received a {success:true} response after registering this. To confirm this is in place, using an application token from the GraphAPI explorer I ran a request at the following path:

/423332064458136/subscriptions

And received back a response of:

{
  "data": [
    {
      "object": "page",
      "callback_url": "https://leadr.co.uk/external/handle-fb.php",
      "fields": [
        "leadgen"
      ],
      "active": true
    }
  ],
}

At the location https://leadr.co.uk/external/handle-fb.php I'm responding with a hub_challenge if required, and additionally logging everything that is sent through php://input to a text file.

Setting up the lead ad

I've setup a lead ad on the VoucherSelector page (https://www.facebook.com/permalink.php?story_fbid=1009785315719107&id=140148862682761) using the instruction from the Facebook documentation: https://developers.facebook.com/docs/marketing-api/guides/lead-ads/v2.5.

This is currently set to inactive as we don't want to spend any budget on it during this testing phase. However it's understood that accounts can still register using the given URL (above).

We've then subscribed this page to our application using a page access token. Using the graphAPI Explorer I've selected our application, and then got a page access token for the voucherselector page. I've then run the following request to subscribe the page to our application.

curl \
-F "access_token=<PAGE_ACCESS_TOKEN>" \
"https://graph.facebook.com/v2.5/140148862682761/subscribed_apps"

I received a {success:true} response from doing this. By running a GET request at the same endpoint I receive a response as follows:

{
  "data": [
    {
      "category": "Business",
      "link": "https://www.facebook.com/games/?app_id=423332064458136",
      "name": "leadR - Lead Ads Collection",
      "id": "423332064458136"
    },
    {
      "category": "Utilities",
      "link": "https://www.facebook.com/games/?app_id=2373072738",
      "name": "Discussion Boards",
      "id": "2373072738"
    }
  ],
}

Confirming that the page is subscribed to our app.

Anytime a registration occurs on this lead ad, our end point is NOT being hit with any payloads.

Additional Notes

  • I am an administrator of the voucher selector page, which the lead ads is running on.
  • When I attempt to download leads generated from this ad (of which there are currently 8), I only receive myself in the download file.
  • Any attempt to use the /subscriptions_sample endpoint to sent a test packet to our endpoint fails with the following error (meaning we're limited to only being able to test with live accounts):

    {"error":{"message":"(#3) App must be on whitelist","type":"OAuthException","code":3,"fbtrace_id":"HH5gInxafKP"}}

Kind of stuck on how to proceed from here. Has anyone had any success in having this send over leads ads to a subscribed endpoint?

like image 682
Lee Davis Avatar asked Nov 02 '15 09:11

Lee Davis


People also ask

Why Facebook leads are not working?

This can happen if the lead was sent through the Facebook Lead Ads testing tool, as those leads are not organic and are not associated with an ad campaign.

How do I get email notifications from Facebook from lead ads?

Creating Facebook Lead Ad Email Alerts:Add a Facebook account that has admin access to the Page you're using for you Facebook lead ads. We use your personal account to access Page leads, so you must be a Page Admin. Click Update connection. Now go to the Lead Forms section and click on Add Facebook Lead Form.


1 Answers

I have successfully gotten the real-time Facebook lead ad updates to our endpoint using the PHP method Facebook goes through through in this video.

You'll need three things assuming you have your files on a test server or live server.

1 - Correct Facebook App set up. Looking at the above, you seem to have the callback URL and app set up correctly. Just make sure the app has the correct callback URL, and the fields are for 'leadgen' in the webhook settings. Also we had issues with the app accessing the listener unless it was on an https domain (se make sure you have a valid SSL cert), and you will also want to give the app domain permissions in any protective service like Cloud Flare or other firewall so it won't be blocked from hitting the listener.

2 - PHP listener at your callback URL. Here's how we have ours set up for PHP, I don't see your code above so I'll post ours here as perhaps it may differ:

<?php
$challenge = $_REQUEST['hub_challenge'];
$verify_token = $_REQUEST['hub_verify_token'];
if ($verify_token == 'abc123') {echo $challenge;}

//you can output the below to your error log and tail -f it to see them
//feed in live, once you see them hit your error log you know your
//listener works, then you can change the below code to handle the
//array and grab the leadgen ID for a further GET request of that real-time entry;

$input = json_decode(file_get_contents('php://input'), true);
error_log(print_r($input, true));  ?>

3 - Some kind of platform page in your CRM to subscribe the pages. This is what we used below, it's very basic but works as a good start point for a fuller app integration to your CRM:

 <h2>Lead Gen Platform</h2>
<script>
  window.fbAsyncInit = function() {
    FB.init({
      appId      : 'USEYOUROWNAPPID',
      xfbml      : true,
      version    : 'v2.5'
    });
  };

  (function(d, s, id){
     var js, fjs = d.getElementsByTagName(s)[0];
     if (d.getElementById(id)) {return;}
     js = d.createElement(s); js.id = id;
     js.src = "//connect.facebook.net/en_US/sdk.js";
     fjs.parentNode.insertBefore(js, fjs);
   }(document, 'script', 'facebook-jssdk'));


function subscribeApp(page_id, page_access_token) {
 console.log('Subscribed Page to FB Leads Live Update ' + page_id); 
 FB.api('/' + page_id + '/subscribed_apps', 
 'post',
 {access_token: page_access_token},
 function(response)
 {console.log('Successfully subscribed page', response);
 });

}
   function checkLoginState() {
  FB.getLoginStatus(function(response) {
       console.log('statusChangeCallback');
    console.log(response);
console.log('successfully logged in', response);
  });

 FB.login(function(response) {
   if (response.status == 'connected') {
    // Logged into your app and Facebook.
    FB.api ('me/accounts', function(response) { 
        console.log('successfully retrieved pages', response);

    var pages = response.data;
    var ul = document.getElementById('list');
    for (i = 0, len = pages.length; i < len; i++) 
    {
    var page = pages[i];
    var li = document.createElement('li');
    var a = document.createElement('a');
    a.href = "#";
    a.onclick = subscribeApp.bind(this, page.id, page.access_token);
    li.appendChild(a);
    a.innerHTML = page.name;
    ul.appendChild(li);
    }
});

  } else if (response.status == 'not_authorized') {
    // The person is logged into Facebook, but not your app.
  } else {
    // The person is not logged into Facebook, so we're not sure if
    // they are logged into this app or not.
  }
 }, {scope: 'public_profile,manage_pages'});

   }

</script>


<fb:login-button scope="public_profile,manage_pages" onlogin="checkLoginState();">
</fb:login-button>

<ul id="list"></ul>

Using the above we have successfully had our Facebook lead ads begin to hit our listener at the callback URL and thus allow us to handle the data in real-time. Hope this helps with your question!

like image 148
Dev_At_InboundHorizonsInc Avatar answered Sep 29 '22 06:09

Dev_At_InboundHorizonsInc