jeffersongoncalves / laravel-segment
Laravel client for the Segment HTTP API: track events, identify users, record page views, send batches, and read Personas profile traits and events.
Package info
github.com/jeffersongoncalves/laravel-segment
pkg:composer/jeffersongoncalves/laravel-segment
Fund package maintenance!
Requires
- php: ^8.2
- illuminate/http: ^12.0|^13.0
- illuminate/support: ^12.0|^13.0
- spatie/laravel-package-tools: ^1.16
Requires (Dev)
- larastan/larastan: ^3.0
- laravel/pint: ^1.21
- orchestra/testbench: ^10.0|^11.0
- pestphp/pest: ^3.0|^4.0
- pestphp/pest-plugin-laravel: ^3.0|^4.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Laravel Segment
A Laravel client for Segment's HTTP API: track events, identify users, record page views, send batches, and read Personas profile traits and events through a simple, typed API built on Laravel's Http client.
This is a server-side API client only — no JS SDK, no Blade snippet.
Features
- Tracking API:
track,identify,page,batch - Profile API (Personas):
profileTraits,profileEvents - Throws
InvalidArgumentExceptionwhen the credentials a call requires are missing
Installation
You can install the package via composer:
composer require jeffersongoncalves/laravel-segment
Publish the config file:
php artisan vendor:publish --tag=laravel-segment-config
Set your Segment credentials in .env:
SEGMENT_WRITE_KEY=your-source-write-key SEGMENT_ACCESS_TOKEN=your-profile-api-access-token SEGMENT_SPACE_ID=your-personas-space-id
The write key authenticates the Tracking API (track, identify, page, batch) — find it under your source's Settings > API Keys. The access token and space ID authenticate the Profile API (profileTraits, profileEvents) — create one under Unify/Personas > Settings > API Access.
Configuration
// config/segment.php return [ 'write_key' => env('SEGMENT_WRITE_KEY', ''), 'access_token' => env('SEGMENT_ACCESS_TOKEN', ''), 'space_id' => env('SEGMENT_SPACE_ID', ''), ];
Usage
The package is resolved via the Segment facade or by injecting JeffersonGoncalves\Segment\Segment.
Track an event
use JeffersonGoncalves\Segment\Facades\Segment; Segment::track('Order Completed', $user->id, [ 'revenue' => 99.90, 'plan' => 'pro', ]);
Identify a user
Segment::identify($user->id, [ 'email' => $user->email, 'name' => $user->name, ]);
Record a page view
Segment::page($user->id, 'Pricing', [ 'referrer' => 'google', ]);
Send a batch
Each message carries its own type alongside its payload. Segment caps a batch request at 500KB.
Segment::batch([ ['type' => 'track', 'userId' => $user->id, 'event' => 'Signed Up'], ['type' => 'identify', 'userId' => $user->id, 'traits' => ['plan' => 'pro']], ]);
Read profile traits and events
$traits = Segment::profileTraits($user->id); $events = Segment::profileEvents($user->id);
The space ID falls back to segment.space_id, and can be overridden per call:
$traits = Segment::profileTraits($user->id, 'spa_other_space');
Error handling
Calling a method without the credentials it requires throws InvalidArgumentException:
try { Segment::track('Order Completed', $user->id); } catch (InvalidArgumentException $e) { logger()->error($e->getMessage()); }
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Contributions are welcome. Please open an issue or pull request on GitHub.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.
