I am trying to set-up the configuration for the Facebook trigger to execute whenever a FB lead ad is filled out, but there is no step-by-step guide so I’m trying to piece things together from posts in the community. I also reviewed Facebook’s Webhooks for Leads info.
I have created a FB app, generated the Access Token and App Secret using these instructions and created Facebook Graph API (App) credentials in n8n with them. I also created a Facebook Trigger node in n8n with Page as the Object and Leadgen as the field and added the Webhook product to my FB app, but that is where I am stuck.
Also is there additional set-up that needs to be done on the FB side after this? Do additional fields need to be added to the Facebook trigger or options enabled?
Thanks in advance for any help anyone can provide.
@John_Parker Yes, but I found it pretty technical and a chore to set-up since I’m an amateur programmer. I primarily followed Facebook documentation although a couple of n8n posts helped somewhat.
I wish I could help further, but it would take a considerable amount of time to type everything out and, to be honest, I don’t recall all I did to get it working.
Graph Api Explorer Subscribe App for leadgen access with the page
Make a POST request using Graph API explorer since youre already on it or postmanm Windows CLI or Linux CLI, you choose. https://graph.facebook.com/{page-id}/subscribed_apps?subscribed_fields=leadgen&access_token={page-access-token}
Permanent Token
Token Tool - Show your permanent Tokens (Same token with permanent validation)
Hi, I’m still not able to make it work. Can you please elaborate on the steps a little more? If you can make a youtube video it would be a great help to many. This seems a little complicated to explain in words. Thank you.
Setting this up is difficult and shouldn’t have to be, so here’s a more in depth tutorial split into 2 parts because I can only upload 5 screenshots at a time. I followed the simplified tutorial by @matheusmaiberg which got me started (thanks) but the only way I could get this to work as of today was through 2 separate workflows set to Active at the same time, and both starting with n8n Webhook nodes. I did not use the Facebook trigger node because I am not sure it has this capability - any admins want to add this capability? I had to set it up this way because I didn’t see a way for n8n to have one URL accept both POST and GET in the same workflow. Facebook will only allow you to enter 1 URL when subscribing using the “leadgen” webhook. Maybe that will change in the future but as of version 17.0 of Facebook’s API you can only enter 1 webhook URL to subscribe to the “leadgen” webhook.
PART1:
Before you start, get a Facebook Page Access Token. I had trouble using the Facebook Graph Explorer to get this token because it would refresh and give me the wrong token for some reason and I had to do it in PHP on my localhost computer with XAMPP by following 2 YouTube videos by Justin Stolpe with his corresponding code on Github (the main video being titled: “Get Page Access Tokens with Facebook Graph API”). Why must it be this hard Facebook?
As previously stated you also need a Facebook Business Account set with all permissions, as well as a Facebook app setup in the developers area with Facebook Login and Webhooks added to it. Facebook changes their API so frequently that you now appear to need more permissions than before. I needed these permissions added to the app with the Page Access Token in Facebook Graph API Explorer: email pages_show_list leads_retrieval pages_read_engagement pages_manage_metadata public_profile pages_manage_ads pages_read_engagement
The previous instructions state to use the Facebook Graph API Explorer to subscribe the app for leadgen access with the page, but the only way I could get it to subscribe properly was by creating a Webhook n8n node first and using that n8n Webhook URL with GET to subscribe using the Webhooks area of Facebook.
Here is my 2 workflow setup first:
Part 2:
Once you have that setup in n8n you can now go back into Facebook and subscribe the Webhook URL (I used the production n8n URL). Select Page in the Facebook Webhooks area and enter your GET Url. Here is the area for Facebook to subscribe the Webhook to “leadgen” and you just need to scroll down and you should be able to hit Subscribe next to “leadgen” once you set this Callback URL and Verify token:
You should now get a confirmation in the first “FB Lead Ads Webhook Verify & set leadgen” workflow. Keep this workflow active and make the second workflow with the same URL to receive the POST lead notification Webhook from Facebook. Here’s what my second flow looks like:
From here we can now use an HTTP Request node to get the full Facebook Lead Ad and send it to a Google Sheet or whatever we want. Inside of my HTTP Request - GET Full Lead Details I have method set to GET and URL set to an expression of https://graph.facebook.com/v17.0/{{$json.leadgenId}} I also have Send Query Parameters set to Using Fields Below with Name of access_token, and Value of EAAJPUTYOURFBPAGEACCESSTOKENHERE as shown here:
Great guide Kenna!
I would like to bring UTM parameters configured in the ads through this integration, I tried to pull the “ad_id” field instead of “leadgen_id”. However, it only brings me the numbering and not the configured parameters… Do you have any suggestions?
Thanks @Erick_de_Jesus
you definitely need leadgen_id in your first Set node so that you can use it to make the subsequent HTTP Request node in my tutorial.
try putting a second Set node right after the last HTTP Request node and then you can write JSON expressions similar to this:
I am using the test lead to send this so those expressions are not exactly correct but you get the idea and you can see what gets passed as UTM parameters here. They will come in JSON format. If you have trouble writing the expressions, try Claude ai- you can put the full JSON in and ask for an expression for n8n.
My setup is with “Instant Forms” manual setup so the way adding UTM parameters actually works in this scenerio is you have to add them when you setup the Instant Form in Facebook in the tab labeled here:
you can see how I am passing landingpage and number2 there. If you use the tokens like {ad.id} like you would in UTM parameter URL but because its an instant form hosted on Facebook, you can’t really adjust them in the URL.
Here’s a list of the UTM:
ad_id={{ad.id}}
adset_id={{adset.id}}
campaign_id={{campaign.id}}
ad_name={{ad.name}}
adset_name={{adset.name}}
campaign_name={{campaign.name}}
placement={{placement}}
site_source_name={{site.source.name}}
I think you can pass them in the final URL after a person fills out the form though as well. It’s a little hard to do this using live traffic though, and the lead testing tool doesn’t pass them when testing. I’m pretty sure thats the only way to add them. And since I don’t use them anymore, I can’t really help you test with live traffic - so sorry I can’t help more than this.
Awesome - Good luck and let us know how it works out. You’ll see it come in the JSON I think when you add them only with live paid traffic. So if you add ad.id you’ll be able to sort in Facebook’s reports easier to find out which ads are doing well and which to cut. Something like this during setup:
Resuming… I managed to get the UTM data doing it that way, but apparently it’s not possible to pass dynamic parameters in this form model. I got this return:
So cool, I just tested your method and it was much easier because it eliminated the token and subscription part.
Basically, you don’t need the GET workflow anymore, because what you did was register the link for future use.
Once you made the GET from webhook/gotleads and clicked the ‘subscribe to lead ads’ button, you could simply change to POST to start retrieving the leads and remove the ‘Respond to webhook’ part.
The first flow is just a one-time verification process.
Again, you need to verify if the ‘Lead Access’ in Business Settings is allowing your CRM Facebook Developer app to retrieve the lead data.
I’ve changed my agency’s process based on your insights. Thanks a lot.