Facebook iOS update : integrated the conversions APIs with native php

Viewed 2321

I am using PHP with Laravel to create an E-commerce and integrating Facebook pixel and everything was working properly, then after IOS 14 update, the pixel integration isn't working properly, so I heard that if we integrated the Facebook conversion APIs and add the domain verification code it would solve the problem

so I tried to add this

<meta name="facebook-domain-verification" content="myCode" />

so my question is: is it correct that integration conversion APIs would solve the problem, and if it is correct - I don't know where and how to integrate these conversion APIs

2 Answers

The Facebook Conversion API events are more powerful than the Facebook Web Pixels. The Conversion API event gets fired through backend logic. you can use the ajax call from the front end, and check the event triggers on the Facebook panel

Facebook Pixel Conversion API

https://developers.facebook.com/docs/marketing-api/conversions-api/

you can generate the access token from https://business.facebook.com/event_manager select your site and generate the Facebook pixel

javascript ajax function and you can simply call this ajax function where you want to trigger pixel

Function Call

let event_id = d.getTime(); 
fbq_custom("PageView", window.location.href, {event_id: 'PageView-'+event_id}, {event_id: 'PageView-'+event_id}) }

Function Definition

function fbq_custom(trace = 'PageView', source_url = '', data = {}){

    data.trace = trace;
    data.source_url = source_url
    $.ajax({
        url: "/conversionAPI/",
        data: data, 
        type: "post",  
        success: function (data) {
            console.log(data);
            console.log("server event test");
        }
     });
}

I just use this package

https://github.com/facebook/facebook-php-business-sdk

PHP API

use FacebookAds\Api;
use FacebookAds\Logger\CurlLogger;
use FacebookAds\Object\ServerSide\Content;
use FacebookAds\Object\ServerSide\CustomData;
use FacebookAds\Object\ServerSide\DeliveryCategory;
use FacebookAds\Object\ServerSide\Event;
use FacebookAds\Object\ServerSide\EventRequest;
use FacebookAds\Object\ServerSide\Gender;
use FacebookAds\Object\ServerSide\UserData;

$trace = $_POST['trace'];
$source_url = $_POST['source_url'];
$firstname = $_POST['firstname'] ? $_POST['firstname']: "";
$lastname = $_POST['lastname'] ? $_POST['lastname']: "";
$phone =  $_POST['telephone'] ? $_POST['telephone']: "";
$email =  $_POST['email'] ? $_POST['email']: "";
$event_id =  $_POST['event_id'] ? $_POST['event_id']: "";
$action_source =  $_POST['action_source'] ? $_POST['action_source']: "website";


$access_token = 'here you need to add the access token';
$pixel_id = 'your pixel id';

if (is_null($access_token) || is_null($pixel_id)) {
    throw new Exception(
        'You must set your access token and pixel id before executing'
    );
}


Api::init(null, null, $access_token);
$api = Api::instance();
$api->setLogger(new CurlLogger());
$events = array();

$user_data = (new UserData())
    ->setClientIpAddress($_SERVER['REMOTE_ADDR'])
    ->setClientUserAgent($_SERVER['HTTP_USER_AGENT'])
    ->setEmail($email)
    ->setPhone($phone)
    ->setFirstName($firstname)
    ->setLastName($lastname);


$custom_data = (new CustomData());

$event = (new Event())
    ->setEventName($trace)
    ->setEventTime(time())
    ->setEventId($event_id)
    ->setEventSourceUrl($source_url)
    ->setActionSource($action_source)
    ->setUserData($user_data)
    ->setCustomData($custom_data);

$events = array();
array_push($events, $event);


$request = (new EventRequest($pixel_id))
    ->setEvents($events);
$response = $request->execute();



print_r(array(
    "status" => 200,
    "message" => "pixel code added successfully",
    "source_url" => $source_url,
    'trace' => $trace,
    'event_id' => $event_id,
    "response" => $response,
    "user_data" => $user_data,
    "event" => $event

)) ;

I think the easiest way to integrate FB CAPI is through Google Tag Manager. This integration is much easier to maintain and develop if you don't have your own developers.

Also, Facebook recently launched a new type of integration - via CAPI Gateway. It costs more, but is set up literally in a few clicks in 30 minutes. Here's a video with detailed instructions: https://stape.io/facebook-conversions-api-gateway-the-easiest-way-to-implement-fb-capi/

Related