How to bring facebook leads into your own CRM ?


      Facebook Lead Ads are so much more than just a form because this kind of advertisement can help you drive objectives from the top of the funnel, such as Brand Awareness and Reach, to Customer Acquisition. Here we look into:

  1. Understand the process and set up your facebook app.

  2. Direct facebook ad leads using webhooks to your CRM.

  3. Make your app to collect your page’s lead ads form submissions.

  4. Generate never expiring access_tokens.

  5. Add new code to your CRM to make a facebook webhook listener.

  6. Collect the the leads in CRM and process it, send it via email, sms or any other ways.

        Facebook ads redirect users to a form provided by facebook. These forms are created when the facebook ad is created. We can provide desired fields in the form. When a user submits the ad-form from facebook, there are two ways to collect the data. Either download the leads frequently from facebook or subscribe the page to a webhook. If you’ve a webhook subscription, you’ll get the lead instantly when the customer submits the leads ads form. Let us see how we can do this.

Prerequisites

1. Facebook Developers Account

2. FacebookAds Python SDK

3. Web Server setup for webhook listener.

        Let’s start with the Facebook developers account. We need a facebook app to automate the process of lead submission. Only a developer's account can create the facebook app.

Set up your Facebook App

  1. Create a new App by giving App name and your email address.

  2. Under App Domains in Basic settings, include your webserver’s URL. You can also add localhost for testing purposes. 

  3. Go to the reviews tab and make your App public. Then go ahead and ask for Manage Pages and Ads Management permissions. There is a review process before granting these permissions. You will be notified by Facebook once it is complete or if there is more information required from you during the review.


Setup the webhook

We need to subscribe to the app for a particular event on facebook. Here we need to get access to the app when a leadgen event occurs.

  1. Add a new product ‘webhooks’ from the ‘Add products’ section( didn’t see ? click on the + button on the left panel ) of the dashboard.

  2. Subscribe for leadgen in the listed subscriptions for your page. 
    Give https://<yourcrmdomain.com>/facebookads-webhook (you can modify) as the callback url and verify_token.

  3.  

  4. Set your webhook listener to receive the subscription test and it should return the verify token that we gave in the above step.


  5. As you can see on the above code, If the challenge value sent by graph api is returned to the same url, the webhook subscription is successful.

Subscribe the App to the page

Each facebook page has different permissions. If you are an admin of a facebook page, you’ve to allow this app to access the page.

  1. Go to the Graph API explorer tool page.( Facebook Graph API Explorer )

  2. Get the user access token first with manage_pages and pages_show_list permissions. Then change it to the page that you wanted to get the access token. If you already have an access token, it will be displayed there. If not, click on get the access token.


  3. While checking the validity of the access_token, it may expire soon. Our CRM needs a never expirable access_token. 

    • Copy the user access_token from Graph API explorer.

    • Debug it with Access Token Debugger

    • You’ll get a little more long lived access_tokens. Press "Extend Access Token" and copy the generated long-lived user access token.

    • Go to Graph Api explorer and paste copied token into the "Access Token" field.

    • Make a GET request with "<PAGE_ID>?fields=access_token"

    • Find the permanent page access_token in the response (node "access_token")


  4. Change the request submission type to POST, API version to your SDK version and change the remaining contents with your page_id and the keyword subscribed_apps. Send the request by clicking the submit button. < page_id >/subscribed_apps


  5. If you pass a json data with this request, you can see it there. If you didn’t send any extra parameters with the request, you’ll get a response { "success": true }. You’ll get an error if anything is missing in the request.
    Note: You can do this request using any other ways like `curl`.

Setting up webhook listener for receiving lead

           We’ve already created the webhook listener function. Now we need to change it to receive the payload coming from facebook api. Facebook is not sending the lead form data directly to the webhook callback url. A reference to the lead data in json format is given in the payload. Here is a sample payload:

{"object":"page","entry":[{"id":"9924851200109236","time":1580389437,"changes":[{"value":{"form_id":"939431753010352","leadgen_id":"554662192186140","created_time":1580389436,"page_id":"192002001092369"},"field":"leadgen"}]}]}

We need to get the lead data using the leadgen_id. For this, facebook is given their own API.

  1. Download or clone the facebook ads python sdk from git.

    git clone https://github.com/facebook/facebook-python-ads-sdk.git
  1. Install it to your project environment

    pip install facebookads 
    Note: if you don’t have pip, install - easy_install pip
  2. Add the following code to your webhook listener function. Check the leadgen_id with your existing leads to avoid duplication of data.


Test the app subscription to page and lead delivery

  1. Go to facebook’s lead ads testing tool page - Lead Ads RTU Debug Tool

  2. Select your page and form for lead testing.

  3. Check the App ID and ‘leadgen’ in Subscribed Fields, under the heading  ‘webhook subscription for the selected page’. 

  4. Click on the ‘create lead’ button to create an automated dummy lead data.


  5. Track the status of the leadgen webhook post to your CRM by clicking on the ‘Track status’ button. Check the status and error messages when the status is shown. You can also see the dummy payload sent to the CRM there.


  6. Click on the ‘Delete lead’ button to delete the submitted lead. It will clear the tracking feature and now you can submit another lead.

           Now you’ve the form data on the variable `lead_data` in your CRM. Pass the data to extract the fields and send mail or sms with it. Do it on another thread, you may use more time the main thread.




Subscribe Us


Subscribe our Newsletter

Contact Us


India
Samstha Services Pvt.Ltd,
First Floor, 5/35A3, Near Npol,
NGO Quarters - BMC Road,
Kakkanad, Kochi, Kerala 682021
+91 70122 32420
+91 85474 25888
+91 80892 27170

UK
The Portfolio Innovation Centre,
The University of Northampton,
Avenue Campus, St. Georges Ave,
Northampton NN2 6JD, UK
+44(0) 20 7097 1312
About UK Office

Follow Us