Search code examples
phpfacebookfacebook-graph-apifacebook-php-sdkfacebook-marketing-api

Get the form name of Facebook lead ads using webhook


I have two different Facebook leads ad campaigns (A and B) connected to the same F.B page, I've connected it to a webhook following this guide using Facebook ads PHP sdk, and pass the leads to my C.R.M, everything works fine, the problem is that I can't tell if the lead came from form A or B.

I've tried to pull the from name like this:

$input = json_decode(file_get_contents('php://input'), true);
if($input)
{
    $form_id = $input['entry'][0]['changes'][0]['value']['form_id'];
    $form = AdsWebhookHandler::getFormName($form_id);
}

From the AdsWebhookHandlerclass:

public static function getFormName($form_id)
    {
        $form = new LeadgenForm($form_id);

        if(!$form) return $form_id;

        return $form->read();
    }

But the form always returns empty ({}) for some reason. Does anybody know how can I pull the form name? or even better - is it possible to pass custom hidden fields in the form?

Thank you all for an answer :)


Solution

  • O.K so I figure out how to get the form name, all I needed to do is use the getData() function included in the Facebook PHP SDK, my code looks like this now:

    public function getFormName($form_id)
    {
        $form = new LeadgenForm($form_id,null,$this->fb_instance);
    
        if(!$form) return $form_id;
    
        $data = $form->read()->getData();
        return isset($data['name']) ? $data['name'] : $form_id;
    }
    

    Hope it'll help someone in the future :)