mtechraw/social-capi

Laravel package to send CAPI events

v1.0.4 2025-07-01 22:37 UTC

This package is auto-updated.

Last update: 2025-07-02 09:27:14 UTC


README

SocialCAPI is a Laravel 11+ compatible package that provides a wrapper for sending Facebook Conversions API (CAPI) events server-side using the official facebook/php-business-sdk. It is ideal for tracking conversions and interactions directly from your backend with support for test_event_code, dynamic data, and Laravel configuration management.

๐Ÿš€ Features

  • Laravel 11 compatible
  • Built on top of Facebook's official Business SDK
  • Supports test_event_code for development and QA validation
  • Fully dynamic input mapping for user data
  • Easily configurable via Laravel config and .env
  • Composer-installable, auto-discovered Laravel service provider

๐Ÿ“ฆ Installation

Install via Composer:

composer require mtechraw/social-capi

๐Ÿ›  Configuration

Step 1: Publish Configuration File

php artisan vendor:publish --tag=config

This will publish config/fb-capi.php:

return [
    'pixel_id' => env('FB_PIXEL_ID'),
    'access_token' => env('FB_ACCESS_TOKEN'),
];

Step 2: Set Environment Variables

Update your .env file:

FB_PIXEL_ID=your_facebook_pixel_id
FB_ACCESS_TOKEN=your_fb_access_token

โœ… Usage Example

You can use the FacebookChannel class directly using Laravel's container:

use SocialCAPI\Facebook\FacebookChannel;

$data = [
    'email' => 'john@example.com',
    'ip' => '127.0.0.1',
    'city' => 'local',
    'state' => 'Texas',
    'country' => 'US',
    'event_name' => 'Purchase',
    'test_event_code' => 'TEST1234ABC', // Optional: for Facebook Events Manager test tab
];

app(FacebookChannel::class)->send($data);

โš™๏ธ Advanced

Supported Event Types

You can send any standard Facebook CAPI event type including:

  • Purchase
  • InitiateCheckout
  • Lead
  • AddToCart
  • etc.

You only need to specify the event name in the event_name key.

Dynamic Data Input

The input array can contain any of the following fields (optional, only what's provided will be sent):

  • email
  • country
  • city
  • state
  • ip
  • event_name
  • test_event_code

๐Ÿงช Using test_event_code

To send a test event (visible in Facebook Events Manager โ†’ Test Events):

  1. Go to Events Manager โ†’ your Pixel โ†’ Test Events
  2. Copy the test event code (e.g. TEST1234ABC)
  3. Add it in the $data array like:
'test_event_code' => 'TEST1234ABC'

This event will appear in the Test Events tab for validation.

๐Ÿงฉ Folder Structure

social-capi/
โ”œโ”€โ”€ config/
โ”‚   โ””โ”€โ”€ fb-capi.php
โ””โ”€โ”€ src/
    โ””โ”€โ”€ Facebook/
        โ”œโ”€โ”€ FBServiceProvider.php
        โ”œโ”€โ”€ FacebookChannel.php
        โ””โ”€โ”€ FacebookMessage.php

๐Ÿ“˜ License

MIT License โ€” see the LICENSE file for details.

๐Ÿ™‹ Author

Developed with โค๏ธ by Mukesh Rawat