PHPackages                             mina/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. [API Development](/categories/api)
4. /
5. mina/laravel-facebook-pixel

ActiveLibrary[API Development](/categories/api)

mina/laravel-facebook-pixel
===========================

Meta pixel integration for Laravel

00PHP

Since Jul 7Pushed 1y agoCompare

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

READMEChangelogDependenciesVersions (1)Used By (0)

Meta Pixel integration for Laravel
==================================

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

[![Latest Version on Packagist](https://camo.githubusercontent.com/6ef154f7e461185b220105830af2f151e93a28524057088bc9a9a1ff9368a486/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f636f6d62696e646d612f6c61726176656c2d66616365626f6f6b2d706978656c2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/combindma/laravel-facebook-pixel)[![GitHub Tests Action Status](https://camo.githubusercontent.com/1012392f619f52592ef31da7d7b923a10ec66dd255890cffcd0c1bfd76d41854/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f636f6d62696e646d612f6c61726176656c2d66616365626f6f6b2d706978656c2f72756e2d74657374732e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://github.com/combindma/laravel-facebook-pixel/actions?query=workflow%3ATests+branch%3Amaster)[![GitHub Code Style Action Status](https://camo.githubusercontent.com/5883a8aac01c3594c2c7c6b524883fe0e666aaddfe5cbdbdf31f5735c567aab7/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f636f6d62696e646d612f6c61726176656c2d66616365626f6f6b2d706978656c2f6669782d7068702d636f64652d7374796c652d6973737565732e796d6c3f6272616e63683d6d61696e266c6162656c3d636f64652532307374796c65267374796c653d666c61742d737175617265)](https://github.com/combindma/laravel-facebook-pixel/actions?query=workflow%3A%22Check+%26+fix+styling%22+branch%3Amaster)[![Total Downloads](https://camo.githubusercontent.com/b22acd565e55764f7411029e9162015301e7520b21dabfbd290600cdfdd017a4/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f636f6d62696e646d612f6c61726176656c2d66616365626f6f6b2d706978656c2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/combindma/laravel-facebook-pixel)

A Complete Meta pixel implementation for your Laravel application.

About Combind Agency
--------------------

[](#about-combind-agency)

[Combine Agency](https://combind.ma?utm_source=github&utm_medium=banner&utm_campaign=package_name) is a leading web development agency specializing in building innovative and high-performance web applications using modern technologies. Our experienced team of developers, designers, and project managers is dedicated to providing top-notch services tailored to the unique needs of our clients.

If you need assistance with your next project or would like to discuss a custom solution, please feel free to [contact us](mailto:hello@combind.ma) or visit our [website](https://combind.ma?utm_source=github&utm_medium=banner&utm_campaign=package_name) for more information about our services. Let's build something amazing together!

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.

- [Documentation for version 4.0 - older version](README-v4.md)

Upgrading to Version 5
----------------------

[](#upgrading-to-version-5)

If you are upgrading from version 4, please refer to our [Version 5 Migration Guide](upgrade-v4-to-v5.md) for detailed instructions on how to make a smooth transition.

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

[](#pre-requisites)

### Register a Meta Pixel

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

To get started with the Meta pixel, you must have a 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 combindma/laravel-facebook-pixel
```

Optionally, you can publish the views using:

```
php artisan vendor:publish --tag="meta-pixel-views"
```

You can publish the config file with:

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

This is the contents of the published config file:

```
return [
   /*
     * The Meta pixel id, should be a code that looks something like "1202417153106158".
     */
    'pixel_id' => env('META_PIXEL_ID', ''),

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

    /*
     * Only if you plan using Conversions API for server events
     * 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('META_PIXEL_TOKEN', ''),

    /*
     * Enable or disable advanced matching. Useful for adjusting user privacy.
     */
    'advanced_matching_enabled' => env('META_PIXEL_ADVANCED_MATCHING_ENABLED', true),

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

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

If you plan on using the [flash-functionality](#flashing-data-for-the-next-request) you must register the MetaPixelMiddleware:

Global Middleware:

```
->withMiddleware(function (Middleware $middleware) {
     $middleware->append(MetaPixelMiddleware::class);
})
```

Or Assigning Middleware to Routes (recommended if you have admin routes):

```
Route::group(['middleware' => [MetaPixelMiddleware::class]], static function () {

});
```

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

```
>

```

If you use UUID as user id you should add userIdAsString attribute to the head component:

```

```

To add an event, use the `track()` function.

```
// CheckoutController.php
use Combindma\FacebookPixel\Facades\MetaPixel;

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

This renders:

```

    /* Meta 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
MetaPixel::track('Purchase', ['currency' => 'USD', 'value' => 30.00], '123456');

//Or create a unique id using
$eventId = uniqid('ViewContent_', true);
MetaPixel::track('ViewContent', [], $eventId);
```

#### 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 Combindma\FacebookPixel\Facades\MetaPixel;

public function postContact()
{
    // Do contact form stuff...
    MetaPixel::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:

```

    /* Meta pixel's base script */

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

```

### Available Methods

[](#available-methods)

```
use Combindma\FacebookPixel\Facades\MetaPixel;

// Retrieve your Pixel id
$id = MetaPixel::pixelId();
// Set Pixel id on the fly
MetaPixel::setPixelId('XXXXXXXX');
// Check whether script rendering is enabled
$enabled = MetaPixel::isEnabled(); // true|false
// Enable and disable script rendering on the fly
MetaPixel::enable();
MetaPixel::disable();
// Add event to the event layer (automatically renders right before the pixel script). Setting new values merges them with the previous ones.
MetaPixel::track('eventName', ['attribute' => 'value']);
MetaPixel::track('eventName', ['attribute' => 'value'], 'event_id'); //with an event id
MetaPixel::track('eventName'); //without properties
MetaPixel::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.
MetaPixel::flashEvent('eventName', ['attribute' => 'value']);
MetaPixel::flashEvent('eventName', ['attribute' => 'value'], 'event_id'); //with an event id
MetaPixel::flashEvent('eventName'); //without properties
//Clear the event layer.
MetaPixel::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 Combindma\FacebookPixel\Facades\MetaPixel;

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

This renders:

```

    /* Meta 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. You can disable it by adding META\_PIXEL\_ADVANCED\_MATCHING\_ENABLED=false in your .env file

```

        /* Meta 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 MetaPixel is macroable.

```
use Combindma\FacebookPixel\Facades\MetaPixel;

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

//in your controller
MetaPixel::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 Combindma\FacebookPixel\Facades\MetaPixel;
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 = MetaPixel::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
MetaPixel::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

```
MetaPixel::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)

- [Combind](https://github.com/Combindma)
- [All Contributors](../../contributors)

License
-------

[](#license)

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

###  Health Score

13

—

LowBetter than 1% of packages

Maintenance27

Infrequent updates — may be unmaintained

Popularity0

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity18

Early-stage or recently created project

 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.

### Community

Maintainers

![](https://www.gravatar.com/avatar/b1764f4a0d3469602414f5a664d8e69eeae023ed8cee10b3bc2c59b3fd4c2cf4?d=identicon)[Mena489](/maintainers/Mena489)

---

Top Contributors

[![omarherri](https://avatars.githubusercontent.com/u/12627384?v=4)](https://github.com/omarherri "omarherri (12 commits)")

### Embed Badge

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

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

###  Alternatives

[stripe/stripe-php

Stripe PHP Library

4.0k143.3M480](/packages/stripe-stripe-php)[twilio/sdk

A PHP wrapper for Twilio's API

1.6k92.9M272](/packages/twilio-sdk)[knplabs/github-api

GitHub API v3 client

2.2k15.8M187](/packages/knplabs-github-api)[facebook/php-business-sdk

PHP SDK for Facebook Business

90121.9M34](/packages/facebook-php-business-sdk)[meilisearch/meilisearch-php

PHP wrapper for the Meilisearch API

73813.7M114](/packages/meilisearch-meilisearch-php)[google/gax

Google API Core for PHP

263103.1M454](/packages/google-gax)

PHPackages © 2026

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