## Getting Started
### Minimal Setup
1. **Installation**:
```bash
composer require dubture/customerio-bundle
Register the bundle in config/bundles.php:
return [
// ...
Dubture\CustomerIOBundle\DubtureCustomerIOBundle::class => ['all' => true],
];
Configuration:
Add your site_id and api_key in config/packages/dubture_customer_io.yaml:
dubture_customer_io:
site_id: "%env(CUSTOMERIO_SITE_ID)%"
api_key: "%env(CUSTOMERIO_API_KEY)%"
First Use Case:
Customer entity implementing Dubture\CustomerIOBundle\Model\CustomerInterface.TrackingEvent or ActionEvent via the event dispatcher to send data to Customer.io.Implement CustomerInterface:
use Dubture\CustomerIOBundle\Model\CustomerInterface;
class AppCustomer implements CustomerInterface
{
public function getId(): string
{
return $this->id;
}
public function getEmail(): ?string
{
return $this->email;
}
// Other required methods (e.g., getName(), getCustomAttributes())
}
Use in Controllers/Commands:
use Dubture\CustomerIOBundle\Event\TrackingEvent;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
public function trackEvent(EventDispatcherInterface $dispatcher, AppCustomer $customer)
{
$event = new TrackingEvent($customer, 'purchase', ['amount' => 99.99]);
$dispatcher->dispatch($event);
}
Track User Actions:
// Example: Track a "view_product" event
$event = new TrackingEvent($customer, 'view_product', [
'product_id' => $product->id,
'category' => $product->category,
]);
$dispatcher->dispatch($event);
Identify Customers:
// Example: Identify a user via email
$event = new ActionEvent($customer, 'identify');
$dispatcher->dispatch($event);
Batch Processing:
Use Symfony’s EventSubscriber to automate tracking:
use Dubture\CustomerIOBundle\Event\TrackingEvents;
class CustomerIOSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return [
'app.customer.purchased' => 'onCustomerPurchased',
];
}
public function onCustomerPurchased(TrackingEvent $event)
{
$event->setName('purchase');
$event->setData(['amount' => $event->getCustomer()->getPurchaseAmount()]);
}
}
Dubture\CustomerIOBundle\CustomerIO service directly for low-level API calls.site_id and api_key in .env for security.CustomerIO service in unit tests to avoid real API calls.Deprecated Bundle:
Event Dispatching:
TrackingEvent/ActionEvent are dispatched after the customer object is fully hydrated (e.g., lazy-loaded relations).// ❌ Avoid: Dispatching before loading customer data
$dispatcher->dispatch(new TrackingEvent($customer, 'view')); // $customer->getData() may return null
Configuration Overrides:
config.yml (legacy). Use config/packages/ for Symfony 4+:
# config/packages/dubture_customer_io.yaml
dubture_customer_io:
site_id: "%env(CUSTOMERIO_SITE_ID)%"
API Errors:
GuzzleHttp exceptions (if used internally).site_id/api_key in Customer.io dashboard.Event Not Firing:
debug:event-dispatcher to list active subscribers:
php bin/console debug:event-dispatcher
Custom Attributes:
CustomerInterface to add domain-specific methods (e.g., getLifetimeValue()).Event Modifiers:
TrackingEvent to modify payloads before sending:
$event->setData(array_merge($event->getData(), ['custom_flag' => true]));
API Wrapper:
Dubture\CustomerIOBundle\CustomerIO to add retries or logging:
class CustomCustomerIO extends CustomerIO
{
public function sendEvent($event)
{
\Log::info('Sending to Customer.io:', $event);
parent::sendEvent($event);
}
}
tags: ['customerio.client'].Service Providers:
$this->app->bind('customerio', function ($app) {
return new \Dubture\CustomerIOBundle\CustomerIO(
$app['config']['dubture_customer_io.site_id'],
$app['config']['dubture_customer_io.api_key']
);
});
Queue Events:
$dispatcher->dispatch(new TrackingEvent($customer, 'signup'));
// Later, process via a worker
Laravel Facade:
use Dubture\CustomerIOBundle\CustomerIO;
class CustomerIOFacade extends \Illuminate\Support\Facades\Facade
{
protected static function getFacadeAccessor() { return 'customerio'; }
}
CustomerIO::track($customer, 'event_name', ['data']);
---
How can I help you explore Laravel packages today?