glorand/laravel-drip
Laravel 5.7+ integration for the Drip PHP API client. Provides a service provider and facade for easy Drip setup via .env config (account ID, API key, user agent) and quick access to Drip features inside Laravel apps.
Installation:
composer require glorand/laravel-drip
Publish the config file:
php artisan vendor:publish --provider="Glorand\LaravelDrip\DripServiceProvider"
Configure .env with your Drip API credentials:
DRIP_API_KEY=your_api_key_here
DRIP_API_SECRET=your_api_secret_here
First Use Case: Trigger a Drip campaign for a user when they register:
use Glorand\LaravelDrip\Facades\Drip;
// After user registration
$user = User::create([...]);
Drip::campaigns()->trigger('welcome_sequence', $user->email);
Key Files:
config/drip.php: API credentials and default settings.app/Providers/DripServiceProvider.php: Service binding (if extending).app/Console/Commands/DripSyncCommand.php: Example for syncing subscribers (if implemented).Triggering Campaigns:
// Trigger a campaign for a single user
Drip::campaigns()->trigger('campaign_slug', $email);
// Trigger for multiple users (batch)
$emails = ['user1@example.com', 'user2@example.com'];
Drip::campaigns()->trigger('campaign_slug', $emails);
Subscribing Users:
// Subscribe with default tags
Drip::subscribers()->subscribe($email);
// Subscribe with custom tags/metadata
Drip::subscribers()->subscribe($email, [
'tags' => ['vip', 'trial_user'],
'custom_attributes' => ['plan' => 'premium']
]);
Unsubscribing Users:
Drip::subscribers()->unsubscribe($email);
Syncing Subscribers:
Use the DripSyncCommand (if extended) or manually sync via:
$subscribers = User::all()->pluck('email');
Drip::subscribers()->sync($subscribers, [
'tags' => ['active_user'],
'custom_attributes' => ['last_login' => now()->toDateTimeString()]
]);
Event Listeners:
Attach Drip actions to Laravel events (e.g., registered, created):
// In EventServiceProvider
protected $listen = [
'registered' => [
'App\Listeners\SubscribeToDrip',
],
];
Middleware for API Routes:
// Ensure Drip is initialized before handling requests
Route::middleware(['drip'])->group(function () {
// Routes requiring Drip
});
Laravel Models:
Extend your User model to include Drip methods:
class User extends Authenticatable {
public function subscribeToDrip(array $options = []) {
return Drip::subscribers()->subscribe($this->email, $options);
}
}
Queue Delayed Actions: Offload Drip calls to queues for performance:
Drip::campaigns()->trigger('campaign_slug', $email)
->onQueue('drip')
->delay(now()->addMinutes(5));
Testing: Use mocks for Drip in tests:
$this->mock(Drip::class)->shouldReceive('campaigns()->trigger')->once();
API Rate Limits:
sync) may hit limits if not throttled.Deprecated Laravel Version:
laravel-package-compatibility).Missing Error Handling:
try {
Drip::campaigns()->trigger('campaign_slug', $email);
} catch (\Exception $e) {
Log::error("Drip trigger failed: " . $e->getMessage());
}
No Webhook Support:
Http client to manually poll Drip’s API or implement a webhook endpoint.Config Overrides:
// In AppServiceProvider
Drip::extend(function ($app) {
$app->singleton('drip', function () {
return new \Glorand\Drip\Client([
'api_key' => config('drip.custom_key'),
'api_secret' => config('drip.custom_secret'),
'endpoint' => config('drip.custom_endpoint'),
]);
});
});
Enable API Logging:
Add this to config/drip.php:
'debug' => env('DRIP_DEBUG', false),
Logs will appear in storage/logs/laravel.log.
Validate API Credentials: Test credentials manually via Drip’s API docs or use:
php artisan drip:test-connection
(If the command doesn’t exist, add it to DripServiceProvider.)
Check HTTP Status Codes:
Drip returns non-200 codes for errors (e.g., 400 for invalid emails). Inspect responses:
$response = Drip::subscribers()->subscribe($email);
if ($response->failed()) {
dd($response->errors());
}
Custom Drip Client: Replace the default client by binding a custom instance:
$this->app->bind('drip', function () {
return new \Glorand\Drip\CustomClient(config('drip'));
});
Add New Methods:
Extend the facade or service provider to add missing Drip features (e.g., suppressions):
// In DripServiceProvider
$this->app->afterResolving('drip', function ($drip) {
$drip->suppressions(function () {
return new \App\Services\Drip\SuppressionManager($drip);
});
});
Queue Observers: Observe Drip queue jobs to log or modify payloads:
// In AppServiceProvider
DripJob::observed(function ($job) {
Log::debug('Drip job triggered', ['job' => $job->payload]);
});
Local Development: Use a local Drip mock for testing:
$this->app->when(Drip::class)
->needs(\Glorand\Drip\Client::class)
->give(function () {
return Mockery::mock(\Glorand\Drip\Client::class);
});
How can I help you explore Laravel packages today?