phpfacebook-graph-apifacebook-php-sdkfacebook-ads-apifacebook-marketing-api

Issue with Facebook Graph API 'time_created' Filter Not Comparing Leads in Seconds


I'm encountering an issue with the Facebook Graph API when filtering leads based on the time_created field using UNIX timestamps. It seems that the API's time filtering is not precise down to the second.

For example, if I filter leads created at 06-07-2024 11:58:00 AM (actually in UNIX timestamp) and a lead has a time_created value of 06-07-2024 11:58:12 AM, the lead is still included in the response, resulting in duplicates in my data.

Has anyone else experienced this issue or found a way to ensure the filtering is precise to the second?

My Code:

$currentDateTime = Carbon::now(); //At Very Start of Code.
$lastestDate = Carbon::parse(LeadData::latest()->first()->last_processed_time)->getTimestamp();
.
.
.
.
$newUrl2 = "https://graph.facebook.com/v20.0/{$adId}/leads";

$paramsNew2 = [
    //'since' => $lastestDate,
    'filtering' => json_encode([
        [
            'field' => 'time_created',
            'operator' => 'GREATER_THAN', //_OR_EQUAL
            'value' => $latestDate, // 1720247400, getting it from DB as last_processed_time.
        ]
    ]),
    'fields' => "id,form_id,platform,partner_name,field_data,ad_id,adset_id,campaign_id,ad_name,adset_name,campaign_name,created_time,post",
    'access_token' => $accessToken,
];

$response2 = Http::get($newUrl2, $paramsNew2);
$data3 = $response2->json();

$leadDatas = $data3['data'] ?? null;
foreach ($leadDatas as $leadData) 
{
    try 
    {
        LeadData::create([
                'account_id' => $adAccountId ?? null,
                'account_name' => null, ///$accountName,
                'campaign_id' => $leadData['campaign_id'],
                'campaign_name' => $leadData['campaign_name'],
                'adset_id' => $leadData['adset_id'],
                'adset_name' => $leadData['adset_name'],
                'ad_id' => $leadData['ad_id'],
                'ad_name' => $leadData['ad_name'],
                'ad_status' => $adStatus,
                'lead_id' => $leadData['id'],
                'lead_created_time' => $leadData['created_time'],
                'platform' => $leadData['platform'],
                'form_id' => $leadData['form_id'],
                'field_data' => json_encode($leadData['field_data'], JSON_UNESCAPED_UNICODE),
                'last_processed_time' => $currentDateTime,
            ]);

    } catch (\Exception $e) {
        echo "Data entry in database failed: " . $e->getMessage() ."\n";
        Log::error($e->getMessage());
    }
}

Then I'm saving the leads in the DB.


Solution

  • I got a answer after some research and help from my friend, apparently you can use 2 parameters to compare data in the Graphs API,

    'filtering' => json_encode([
        [
            'field' => 'time_created',
            'operator' => 'GREATER_THAN',
            'value' => $lastestDate,
        ],
        [
            'field' => 'time_created',
            'operator' => 'LESS_THAN',
            'value' => $currentDateTime
        ]
    ]),
    

    And my problem was not actually comparing in seconds, the leads being after the scheduler being run to enter the $currentDateTimein DB. Also currentDateTime should be in timestamp

    $currentDateTime = Carbon::now()->timestamp;