PHPackages                             bkfdev/laravel-facebook-pixel - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. bkfdev/laravel-facebook-pixel

ActiveLibrary

bkfdev/laravel-facebook-pixel
=============================

Facebook Pixel integration for Laravel

v1.0.8(2y ago)012MITPHPPHP ^8.0

Since Jan 28Pushed 2y ago1 watchersCompare

[ Source](https://github.com/aeq-dev/laravel-facebook-pixel)[ Packagist](https://packagist.org/packages/bkfdev/laravel-facebook-pixel)[ Docs](https://github.com/aeq-dev/laravel-facebook-pixel)[ RSS](/packages/bkfdev-laravel-facebook-pixel/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (9)Dependencies (13)Versions (10)Used By (0)

Facebook Pixel integration for Laravel
======================================

[](#facebook-pixel-integration-for-laravel)

[![Latest Version on Packagist](https://camo.githubusercontent.com/3a01429c34151b29a64435ce2cec2b28e23dcc8e3f41c8cf29a44935044c4b8b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6165712d6465762f6c61726176656c2d66616365626f6f6b2d706978656c2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/aeq-dev/laravel-facebook-pixel)[![GitHub Tests Action Status](https://camo.githubusercontent.com/edd0bc1e45d36ab9a0cf48b2ad3302753f868b1aa616f78022a4bba6374a959c/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6165712d6465762f6c61726176656c2d66616365626f6f6b2d706978656c2f72756e2d74657374732e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://github.com/aeq-dev/laravel-facebook-pixel/actions?query=workflow%3ATests+branch%3Amaster)[![GitHub Code Style Action Status](https://camo.githubusercontent.com/ee4b47f20bf934f141e7e14385d098a8948689883fb4cea8cf42ca5161a19981/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6165712d6465762f6c61726176656c2d66616365626f6f6b2d706978656c2f6669782d7068702d636f64652d7374796c652d6973737565732e796d6c3f6272616e63683d6d61696e266c6162656c3d636f64652532307374796c65267374796c653d666c61742d737175617265)](https://github.com/aeq-dev/laravel-facebook-pixel/actions?query=workflow%3A%22Check+%26+fix+styling%22+branch%3Amaster)[![Total Downloads](https://camo.githubusercontent.com/1991879ef6d3c94a4acbf1b79214f25a289b25f5abbfda8273582e269f2113fd/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6165712d6465762f6c61726176656c2d66616365626f6f6b2d706978656c2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/aeq-dev/laravel-facebook-pixel)

A Complete Facebook Pixel implementation for your Laravel application.

Introduction
------------

[](#introduction)

This package provides a smooth integration of Meta Pixel, along with a straightforward implementation of the latest Conversions API, enhancing your overall experience.

Pre-requisites
--------------

[](#pre-requisites)

### Register a Meta Pixel

[](#register-a-meta-pixel)

To get started with the pixel Meta, you must have a Meta pixel registered: [Read this guide](https://web.facebook.com/business/help/952192354843755).

### Conversions API

[](#conversions-api)

If you plan to use Conversions API then you need to:

#### Obtain An Access Token

[](#obtain-an-access-token)

To use the Conversions API, you need to generate an access token, which will be passed as a parameter in every API call.

Refer to [Conversions API Guide](https://developers.facebook.com/docs/marketing-api/conversions-api/get-started) to learn more.

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require bkfdev/laravel-facebook-pixel
```

You can publish the config file with:

```
php artisan vendor:publish --tag="facebook-pixel-config"
```

This is the contents of the published config file:

```
return [
   /*
     * The Facebook Pixel id, should be a code that looks something like "XXXXXXXXXXXXXXXX".
     */
    'facebook_pixel_id' => env('FACEBOOK_PIXEL_ID', ''),

    /*
     * The key under which data is saved to the session with flash.
     */
    'sessionKey' => env('FACEBOOK_PIXEL_SESSION_KEY', config('app.name').'_facebookPixel'),

    /*
     * To use the Conversions API, you need an access token. For Documentation please see: https://developers.facebook.com/docs/marketing-api/conversions-api/get-started
     */
    'token' => env('FACEBOOK_PIXEL_TOKEN', ''), //Only if you plan using Conversions API for server events

    /*
     * Enable or disable script rendering. Useful for local development.
     */
    'enabled' => env('FACEBOOK_PIXEL_ENABLED', false),

    /*
     * This is used to test server events
     */
    'test_event_code' => env('FACEBOOK_TEST_EVENT_CODE')
];
```

If you plan on using the [flash-functionality](#flashing-data-for-the-next-request) you must install the FacebookPixelMiddleware, after the StartSession middleware:

```
// app/Http/Kernel.php
protected $middleware = [
    ...
    \Illuminate\Session\Middleware\StartSession::class,
    \Bkfdev\FacebookPixel\FacebookPixelMiddleware::class,
    ...
];
```

Usage - Meta Pixel
------------------

[](#usage---meta-pixel)

### Include scripts in Blade

[](#include-scripts-in-blade)

Insert head view after opening head tag, and body view after opening body tag

```
>

        @include('facebookpixel::head')

        @include('facebookpixel::body')

```

Your events will also be rendered here. To add an event, use the `track()` function.

```
// CheckoutController.php
use Bkfdev\FacebookPixel\Facades\FacebookPixel;

public function index()
{
    FacebookPixel::track('Purchase', ['currency' => 'USD', 'value' => 30.00]);
    return view('thank-you');
}
```

This renders:

```

    /* Facebook Pixel's base script */

  fbq('track', 'Purchase', {"currency":"USD","value":30});

```

You can also specify a unique event ID for any of your events so that, if you plan using the conversions API you avoid duplications.

```
//For example your order id
FacebookPixel::track('Purchase', ['currency' => 'USD', 'value' => 30.00], '123456');
```

#### Flashing data for the next request

[](#flashing-data-for-the-next-request)

The package can also set event to render on the next request. This is useful for setting data after an internal redirect.

```
// ContactController.php
use Bkfdev\FacebookPixel\Facades\FacebookPixel;

public function postContact()
{
    // Do contact form stuff...
    FacebookPixel::flashEvent('Lead', [
        'content_name' => 'Auto Insurance',
        'content_category' => 'Quote',
        'value' => 400.00,
        'currency' => 'USD'
    ]);
    return redirect()->action('ContactController@getContact');
}
```

After a form submit, the following event will be parsed on the contact page:

```

    /* Facebook Pixel's base script */

    fbq(
        'track', 'Lead', {
            'content_name': 'Auto Insurance',
            'content_category': 'Quote',
            'value': 40.00,
            'currency': 'USD'
        }
    );

```

### Available Methods

[](#available-methods)

```
use Bkfdev\FacebookPixel\Facades\FacebookPixel;

// Retrieve your Pixel id
$id = FacebookPixel::pixelId();
// Set Pixel id on the fly
FacebookPixel::setPixelId('XXXXXXXX');
// Check whether script rendering is enabled
$enabled = FacebookPixel::isEnabled(); // true|false
// Enable and disable script rendering on the fly
FacebookPixel::enable();
FacebookPixel::disable();
// Add event to the event layer (automatically renders right before the pixel script). Setting new values merges them with the previous ones.
FacebookPixel::track('eventName', ['attribute' => 'value']);
FacebookPixel::track('eventName', ['attribute' => 'value'], 'event_id'); //with an event id
FacebookPixel::track('eventName'); //without properties
FacebookPixel::track('eventName', [], 'event_id'); //with an event id but without properties
// Flash event for the next request. Setting new values merges them with the previous ones.
FacebookPixel::flashEvent('eventName', ['attribute' => 'value']);
FacebookPixel::flashEvent('eventName', ['attribute' => 'value'], 'event_id'); //with an event id
FacebookPixel::flashEvent('eventName'); //without properties
//Clear the event layer.
FacebookPixel::clear();
```

### Custom Events

[](#custom-events)

You can also track a specific custom event on your website. This feature is not available for flashed events.

```
use Bkfdev\FacebookPixel\Facades\FacebookPixel;

// In your controller
FacebookPixel::trackCustom('CUSTOM-EVENT-NAME', ['custom_parameter' => 'ABC', 'value' => 10.00, 'currency' => 'USD']);
//With an event ID
FacebookPixel::trackCustom('CUSTOM-EVENT-NAME', ['custom_parameter' => 'ABC', 'value' => 10.00, 'currency' => 'USD'], 'EVENT_ID');
```

This renders:

```

    /* Facebook Pixel's base script */

      fbq('trackCustom', 'CUSTOM-EVENT-NAME', {'custom_parameter': 'ABC', 'value': 10.00, 'currency': 'USD'});
      /* If you specify the event ID */
      fbq('trackCustom', 'CUSTOM-EVENT-NAME', {'custom_parameter': 'ABC', 'value': 10.00, 'currency': 'USD'}, { eventID : 'EVENT_ID' });

```

### Advanced matching

[](#advanced-matching)

This package provides by default advanced matching. We retrieve the email and the user id from authenticated user and include it in the Pixel base code fbq('init') function call as a third parameter.

```

        /* Facebook Pixel's base script */

        fbq('init', '{PixelID}', {
            em: 'email@email.com', //Email provided with Auth::user()->email
            external_id: 12345, //User id provided with Auth::id()
        });

```

### Macroable

[](#macroable)

Adding events to pages can become a repetitive process. Since this package isn't supposed to be opinionated on what your events should look like, the FacebookPixel is macroable.

```
use Bkfdev\FacebookPixel\Facades\FacebookPixel;

//include this in your macrobale file
FacebookPixel::macro('purchase', function ($product) {
    FacebookPixel::track('Purchase', [
        'currency' => 'EUR',
        'value' => $product->price
    ]);
});

//in your controller
FacebookPixel::purchase($product);
```

Usage - Conversions API
-----------------------

[](#usage---conversions-api)

If you plan on using [Conversions API](https://developers.facebook.com/docs/marketing-api/conversions-api/get-started) functionalities. Yous should specify the token in your .env file first.

For every request yous should specify a unique event id for handling Pixel Duplicate Events and Conversions API.

This is how you can start:

```
use Bkfdev\FacebookPixel\Facades\FacebookPixel;
use FacebookAds\Object\ServerSide\Content;
use FacebookAds\Object\ServerSide\CustomData;
use FacebookAds\Object\ServerSide\DeliveryCategory;
use FacebookAds\Object\ServerSide\UserData;

//Prepare User Data first.
// By default, the IP Address, User Agent and fbc/fbp cookies are added.
$user_data = FacebookPixel::userData()->setEmail('joe@eg.com')->setPhone('12345678901');

$content = (new Content())
    ->setProductId('product123')
    ->setQuantity(1)
    ->setDeliveryCategory(DeliveryCategory::HOME_DELIVERY);

$custom_data = (new CustomData())
    ->setContents(array($content))
    ->setCurrency('usd')
    ->setValue(123.45);

$eventId = uniqid('prefix_');

//send request
FacebookPixel::send('Purchase', $eventId ,$custom_data, $user_data);
```

If you don't specify the $user\_data parameter, by default we retrieve the email &amp; the id from Auth::user() if the user is authenticated. We use the user id as a same external\_id in Meta Pixel and conversions API

```
FacebookPixel::send('Purchase', $eventId, $custom_data);
```

If you want to test server events, you need to specify the FACEBOOK\_TEST\_EVENT\_CODE in your .env file. By default, this test code will be sent in all API request.

So Don't forget to delete after you finish your server tests.

You can use the [Playload Helper](https://developers.facebook.com/docs/marketing-api/conversions-api/payload-helper) to learn more about the requests to send.

Testing
-------

[](#testing)

```
composer test
```

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

Security Vulnerabilities
------------------------

[](#security-vulnerabilities)

Please review [our security policy](../../security/policy) on how to report security vulnerabilities.

Credits
-------

[](#credits)

- [Bkfdev](https://github.com/aeq-dev)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

23

—

LowBetter than 27% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity5

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity52

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~0 days

Total

9

Last Release

832d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/6d2469c45f2a6b752194eb41838268b9d199232d364f18be8597cd3c1da9c5e7?d=identicon)[aeq-dev](/maintainers/aeq-dev)

---

Top Contributors

[![aeq-dev](https://avatars.githubusercontent.com/u/81385994?v=4)](https://github.com/aeq-dev "aeq-dev (26 commits)")

---

Tags

laravelfacebook pixelbkfdev

###  Code Quality

TestsPest

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/bkfdev-laravel-facebook-pixel/health.svg)

```
[![Health](https://phpackages.com/badges/bkfdev-laravel-facebook-pixel/health.svg)](https://phpackages.com/packages/bkfdev-laravel-facebook-pixel)
```

###  Alternatives

[vormkracht10/laravel-mails

Laravel Mails can collect everything you might want to track about the mails that has been sent by your Laravel app.

24149.7k](/packages/vormkracht10-laravel-mails)[spatie/laravel-prometheus

Export Laravel metrics to Prometheus

2651.3M6](/packages/spatie-laravel-prometheus)[combindma/laravel-facebook-pixel

Meta pixel integration for Laravel

4956.9k](/packages/combindma-laravel-facebook-pixel)[hydrat/filament-table-layout-toggle

Filament plugin adding a toggle button to tables, allowing user to switch between Grid and Table layouts.

6292.3k1](/packages/hydrat-filament-table-layout-toggle)[scalar/laravel

Render your OpenAPI-based API reference

6183.9k2](/packages/scalar-laravel)[ralphjsmit/laravel-helpers

A package containing handy helpers for your Laravel-application.

13704.6k2](/packages/ralphjsmit-laravel-helpers)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
