### Install laravel-segment via Composer Source: https://github.com/jeffersongoncalves/laravel-segment/blob/main/README.md Install the package via Composer. ```bash composer require jeffersongoncalves/laravel-segment ``` -------------------------------- ### Run tests with composer test Source: https://github.com/jeffersongoncalves/laravel-segment/blob/main/README.md Run the package test suite. ```bash composer test ``` -------------------------------- ### Publish laravel-segment config Source: https://github.com/jeffersongoncalves/laravel-segment/blob/main/README.md Publish the package configuration file. ```bash php artisan vendor:publish --tag=laravel-segment-config ``` -------------------------------- ### Segment configuration file Source: https://github.com/jeffersongoncalves/laravel-segment/blob/main/README.md Configuration file for the package, mapping environment variables to configuration keys. ```php // config/segment.php return [ 'write_key' => env('SEGMENT_WRITE_KEY', ''), 'access_token' => env('SEGMENT_ACCESS_TOKEN', ''), 'space_id' => env('SEGMENT_SPACE_ID', ''), ]; ``` -------------------------------- ### Record a page view with Segment Source: https://github.com/jeffersongoncalves/laravel-segment/blob/main/README.md Record a page view. Requires the write key. ```php Segment::page($user->id, 'Pricing', [ 'referrer' => 'google', ]); ``` -------------------------------- ### Track an event with Segment Source: https://github.com/jeffersongoncalves/laravel-segment/blob/main/README.md Track an event using the Segment facade. Requires the write key. ```php use JeffersonGoncalves\Segment\Facades\Segment; Segment::track('Order Completed', $user->id, [ 'revenue' => 99.90, 'plan' => 'pro', ]); ``` -------------------------------- ### Send a batch with Segment Source: https://github.com/jeffersongoncalves/laravel-segment/blob/main/README.md Send a batch of messages. Each message carries its own type. Segment caps a batch request at 500KB. ```php Segment::batch([ ['type' => 'track', 'userId' => $user->id, 'event' => 'Signed Up'], ['type' => 'identify', 'userId' => $user->id, 'traits' => ['plan' => 'pro']], ]); ``` -------------------------------- ### Set Segment credentials in .env Source: https://github.com/jeffersongoncalves/laravel-segment/blob/main/README.md Set the required Segment credentials in the .env file. The write key is for the Tracking API, while the access token and space ID are for the Profile API. ```env SEGMENT_WRITE_KEY=your-source-write-key SEGMENT_ACCESS_TOKEN=your-profile-api-access-token SEGMENT_SPACE_ID=your-personas-space-id ``` -------------------------------- ### Read profile traits and events Source: https://github.com/jeffersongoncalves/laravel-segment/blob/main/README.md Read profile traits and events from the Profile API. Requires access token and space ID. ```php $traits = Segment::profileTraits($user->id); $events = Segment::profileEvents($user->id); ``` -------------------------------- ### Identify a user with Segment Source: https://github.com/jeffersongoncalves/laravel-segment/blob/main/README.md Identify a user with traits. Requires the write key. ```php Segment::identify($user->id, [ 'email' => $user->email, 'name' => $user->name, ]); ``` -------------------------------- ### Handle InvalidArgumentException Source: https://github.com/jeffersongoncalves/laravel-segment/blob/main/README.md Handle missing credentials. Calling a method without required credentials throws InvalidArgumentException. ```php try { Segment::track('Order Completed', $user->id); } catch (InvalidArgumentException $e) { logger()->error($e->getMessage()); } ``` -------------------------------- ### Override space ID for profile traits Source: https://github.com/jeffersongoncalves/laravel-segment/blob/main/README.md Override the space ID per call. The space ID falls back to segment.space_id. ```php $traits = Segment::profileTraits($user->id, 'spa_other_space'); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.