/facebook-pixel

FB pixel extension for nette framework

Primary LanguagePHPMIT LicenseMIT

Nette extension for Facebook Pixel.

TODO

  • support for ajax

Requirements

Installation

The best way to install Eflyax/Facebook-pixel is using Composer:

$   composer require eflyax/facebook-pixel

Usage

Preparation

Extension registration

Add to your neon config:

extensions:
    facebookPixel: Eflyax\FacebookPixel\DI\FacebookPixelExtension

and configuration with you FB pixel ID:

facebookPixel:
    id: '111122223333444'
    productIdPrefix: '42' # optional

Be careful you add FB pixel ID as string, there was issues with integer

Backend

Inject FB pixel factory and service to your module where you want to render FB pixel. FB pixel service will help you to render specific events.

abstract class BaseFrontPresenter extends BasePresenter
{

    /** @var IFacebookPixelFactory @inject */
    public $IFacebookPixelFactory;
    
    /** @var FacebookPixel */
    public $facebookPixel;
    
    
    protected function startup()
    {
        parent::startup();
        $this->facebookPixel = $this['facebookPixel'];
    }

    
    .
    .
    
    protected function createComponentFacebookPixel()
    {
        return $this->IFacebookPixelFactory->create();
    }
    
}

Frontend

Now you are ready to render FB pixel in layout

{control facebookPixel}

Events

AddToCart

In method where you add product to cart call eventStart like this:

    $this->facebookPixel->addToCart(
        $productId,
        $productTitle,
        $productCategory,
        $productPrice,
        $currencyCode
    );

Purchase

  $this->facebookPixel->purchase($totalPrice, $currencyCode);

ViewContent

For one product:

$this->facebookPixel->viewContent(
    $productId,
    $productTitle,
    $productCategory,
    $productPrice,
    $currencyCode
);

For more products (category):

$this->facebookPixel->viewContent(
    $productIds
);

Events validation

If you want to validate events which you send to facebook I can recommend this browser plugin: Facebook Pixel Helper

How to run tests

$   cd tests
$   composer install
$   mkdir temp
$   ./vendor/bin/codecept build
$   ./vendor/bin/codecept run