How to track Facebook MetaPixel event in laravel /php?

Updated: Feb 19, 2025

How to track Facebook MetaPixel event in laravel /php?

To track Facebook MetaPixel events in Laravel/PHP, you can follow these steps:

  1. Install the facebook/graph-api package: First, you need to install the official Facebook PHP SDK using Composer. Run the following command in your terminal:
composer require facebook/graph-api
  1. Configure the Facebook SDK: Create a new file named facebook.php in the app/Providers/ directory and add the following code:
<?php

namespace App\Providers;

use Facebook\Facebook;
use Illuminate\Support\ServiceProvider;

class FacebookServiceProvider extends ServiceProvider
{
    /**
     * Register the service provider.
     *
     * @return void
     */
    public function register()
    {
        $this->app->singleton(Facebook::class, function () {
            return new Facebook([
                'app_id' => env('FACEBOOK_APP_ID'),
                'app_secret' => env('FACEBOOK_APP_SECRET'),
                'default_graph_version' => 'v11.0',
            ]);
        });
    }
}

Make sure to set your FACEBOOK_APP_ID and FACEBOOK_APP_SECRET environment variables.

  1. Create a helper function: Create a new file named helpers.php in the app/Helpers/ directory and add the following code:
<?php

if (!function_exists('facebook')) {
    function facebook()
    {
        return app(Facebook::class);
    }
}
  1. Create a new middleware: Create a new file named TrackFacebookPixel.php in the app/Http/Middleware/ directory and add the following code:
<?php

namespace App\Http\Middleware;

use Closure;
use Facebook\Exceptions\FacebookResponseException;
use Facebook\Exceptions\FacebookSDKException;
use Facebook\Facebook;

class TrackFacebookPixel
{
    /**
     * The Facebook SDK instance.
     *
     * @var Facebook
     */
    protected $facebook;

    /**
     * Create a new middleware instance.
     *
     * @param  Facebook  $facebook
     * @return void
     */
    public function __construct(Facebook $facebook)
    {
        $this->facebook = $facebook;
    }

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        try {
            $pixel = $this->facebook->pixel();
            $pixel->track('PageView');
        } catch (FacebookResponseException $e) {
            // Log any errors
        } catch (FacebookSDKException $e) {
            // Log any errors
        }

        return $next($request);
    }
}
  1. Register the middleware: Add the following line to the app/Http/Kernel.php file in the $routeMiddleware array:
'track.facebook.pixel' => \App\Http\Middleware\TrackFacebookPixel::class,
  1. Apply the middleware: Finally, apply the middleware to the web middleware group in the app/Http/Kernel.php file:
protected $middlewareGroups = [
    'web' => [
        // ...
        \App\Http\Middleware\TrackFacebookPixel::class,
    ],
];

Now, every time a page is loaded in your Laravel application, the Facebook Pixel event 'PageView' will be triggered. You can extend this middleware to track other events as well.