{"id":43368820,"url":"https://github.com/slashequip/laravel-segment","last_synced_at":"2026-02-02T05:09:31.927Z","repository":{"id":40298245,"uuid":"351140376","full_name":"slashequip/laravel-segment","owner":"slashequip","description":"Laravel Segment is an opinionated, approach to integrating Segment into your Laravel application.","archived":false,"fork":false,"pushed_at":"2025-05-18T21:28:17.000Z","size":130,"stargazers_count":26,"open_issues_count":2,"forks_count":16,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-10-06T19:49:10.069Z","etag":null,"topics":["laravel","segmentio"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/slashequip.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":".github/CONTRIBUTING.md","funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":".github/SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2021-03-24T15:58:41.000Z","updated_at":"2025-07-08T09:07:57.000Z","dependencies_parsed_at":"2024-05-20T21:10:57.982Z","dependency_job_id":"2f006197-2404-408c-979b-de74570dee5f","html_url":"https://github.com/slashequip/laravel-segment","commit_stats":{"total_commits":30,"total_committers":4,"mean_commits":7.5,"dds":0.1333333333333333,"last_synced_commit":"4a93fad869ac37006dd84ffbc628448ab5a5c537"},"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"purl":"pkg:github/slashequip/laravel-segment","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/slashequip%2Flaravel-segment","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/slashequip%2Flaravel-segment/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/slashequip%2Flaravel-segment/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/slashequip%2Flaravel-segment/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/slashequip","download_url":"https://codeload.github.com/slashequip/laravel-segment/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/slashequip%2Flaravel-segment/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29006081,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-02T04:25:24.522Z","status":"ssl_error","status_checked_at":"2026-02-02T04:24:51.069Z","response_time":58,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["laravel","segmentio"],"created_at":"2026-02-02T05:09:26.838Z","updated_at":"2026-02-02T05:09:31.921Z","avatar_url":"https://github.com/slashequip.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Laravel Segment\n\n[![Latest Version on Packagist](https://img.shields.io/packagist/v/slashequip/laravel-segment.svg?style=flat-square)](https://packagist.org/packages/slashequip/laravel-segment)\n[![tests](https://github.com/slashequip/laravel-segment/actions/workflows/run-tests.yml/badge.svg?branch=main)](https://github.com/slashequip/laravel-segment/actions/workflows/run-tests.yml)\n[![code style](https://github.com/slashequip/laravel-segment/actions/workflows/code-style.yml/badge.svg?branch=main)](https://github.com/slashequip/laravel-segment/actions/workflows/php-cs-fixer.yml)\n[![psalm](https://github.com/slashequip/laravel-segment/actions/workflows/static-analysis.yml/badge.svg?branch=main)](https://github.com/slashequip/laravel-segment/actions/workflows/psalm.yml)\n[![Total Downloads](https://img.shields.io/packagist/dt/slashequip/laravel-segment.svg?style=flat-square)](https://packagist.org/packages/slashequip/laravel-segment)\n\n![Laravel Segment Logo Banner](https://github.com/slashequip/laravel-segment/blob/main/laravel-segment-banner.svg?raw=true)\n\nLaravel Segment is an opinionated, approach to integrating Segment into your Laravel application.\n\n\n## Installation\n\nYou can install the package via composer:\n\n```bash\ncomposer require slashequip/laravel-segment\n```\n\n\nYou can publish the config file with:\n```bash\nphp artisan vendor:publish --provider=\"SlashEquip\\LaravelSegment\\LaravelSegmentServiceProvider\"\n```\n\nThis is the contents of the published config file, which should be located at `config/segment.php`:\n\n```php\nreturn [\n    'enabled' =\u003e env('SEGMENT_ENABLED', true),\n\n    /**\n     * This is your Segment API write key. It can be\n     * found under Source \u003e Settings \u003e Api Keys\n     */\n    'write_key' =\u003e env('SEGMENT_WRITE_KEY', null),\n\n    /**\n     * Should the Segment service defer all tracking\n     * api calls until after the response, sending\n     * everything using the bulk/batch api?\n     */\n    'defer' =\u003e env('SEGMENT_DEFER', false),\n\n    /**\n     * Should the Segment service be run in safe mode.\n     * Safe mode will only report errors in sending\n     * when safe mode is off exceptions are thrown\n     */\n    'safe_mode' =\u003e env('SEGMENT_SAFE_MODE', true),\n];\n```\n\n## Setting your write key\n\nYour write key is the API key given to you by Segment which can be found under your PHP source settings; \n`https://app.segment.com/{your-workspace-name}/sources/{your-source-name}/settings/keys` in the Segment UI.\n\n## What is a Segment User\n\nWhen we talk about a 'user' in the context of this package we mean any object that\nimplements the `SlashEquip\\LaravelSegment\\Contracts\\CanBeIdentifiedForSegment` contract\nthe package comes with a trait (and the interface) you can attach to your default\nUser model;\n\n```php\nuse Illuminate\\Database\\Eloquent\\Model;\nuse SlashEquip\\LaravelSegment\\Traits\\HasSegmentIdentityByKey;\nuse SlashEquip\\LaravelSegment\\Contracts\\CanBeIdentifiedForSegment;\n\nclass User extends Model implements CanBeIdentifiedForSegment\n{\n    use HasSegmentIdentityByKey;\n}\n```\n\nUsing this trait will automagically use your users' primary key as the identifier\nthat is sent to Segment. Alternatively, you can implement your own instance of the\n`public function getSegmentIdentifier(): string;` method on your User model and not\nuse the trait.\n\n### Globally identifying users\n\nIf you are sending Segment events in multiple places through your application and\nthrough-out a request it might make sense to globally identify a user to make it\nmore convenient when making tracking calls.\n\n```php\nuse SlashEquip\\LaravelSegment\\Facades\\Segment;\n\nSegment::setGlobalUser($user);\n```\n\n### Globally setting context\n\nSegment allows you to send [context](https://segment.com/docs/connections/spec/common/#context)\nwith your tracking events too, you can set a global context that applies to all tracking events.\n\n```php\nuse SlashEquip\\LaravelSegment\\Facades\\Segment;\n\nSegment::setGlobalContext([\n    'ip' =\u003e '127.0.0.1',\n    'locale' =\u003e 'en-US',\n    'screen' =\u003e [\n        'height' =\u003e 1080,\n        'width' =\u003e 1920,\n    ],\n]);\n```\n\n### Here have some convenience\n\nLaravel Segment ships with a middleware that you can apply in your HTTP Kernal that will handle\nthe setting of the global user and some sensible global context too. It should be simple to extend\nthis middleware and adjust for your needs if you want to add to the default context provided.\n\n```php\n    'api' =\u003e [\n        // ... other middleware\n        SlashEquip\\LaravelSegment\\Middleware\\ApplySegmentGlobals::class\n    ],\n```\n\n## Usage\n\n### For tracking events\n```php\nuse SlashEquip\\LaravelSegment\\Facades\\Segment;\n\nSegment::forUser($user)-\u003etrack('User Signed Up', [\n    'source' =\u003e 'Product Hunt',\n]);\n\n// If you have set a global user you can\n// use the simpler provided syntax.\nSegment::track('User Signed Up', [\n    'source' =\u003e 'Product Hunt',\n]);\n\n// If you have defer enabled in the config\n// you can still track an event immediately using trackNow.\nSegment::trackNow('User Signed Up', [\n    'source' =\u003e 'Product Hunt',\n]);\n```\n\n### For identifying users\n```php\nuse SlashEquip\\LaravelSegment\\Facades\\Segment;\n\nSegment::forUser($user)-\u003eidentify([\n    'last_logged_in' =\u003e '2021-03-24 20:05:30',\n    'latest_subscription_amount' =\u003e '$24.60',\n]);\n\n// If you have set a global user you can\n// use the simpler provided syntax.\nSegment::identify([\n    'last_logged_in' =\u003e '2021-03-24 20:05:30',\n    'latest_subscription_amount' =\u003e '$24.60',\n]);\n\n// If you have defer enabled in the config\n// you can still identify a user immediately using identifyNow.\nSegment::identifyNow([\n    'source' =\u003e 'Product Hunt',\n]);\n```\n\n### Anonymous users\n\nSegment allows you to track events for users that are not yet users in your system, they call these anonymous users.\n\nTo track events for anonymous users you can use apply the `ShouldBeAnonymouslyIdentified` interface to any `CanBeIdentifiedForSegment` implementation.\n\n```php\nuse SlashEquip\\LaravelSegment\\Contracts\\CanBeIdentifiedForSegment;\nuse SlashEquip\\LaravelSegment\\Contracts\\ShouldBeAnonymouslyIdentified;\n\nclass SegmentAnonymousTestUser implements CanBeIdentifiedForSegment, ShouldBeAnonymouslyIdentified\n{\n}\n```\n\nFor convenience, the package comes with a `SimpleSegmentAnonymousUser` class that implements the `ShouldBeAnonymouslyIdentified` interface.\n\n```php\nuse SlashEquip\\LaravelSegment\\SimpleSegmentAnonymousUser;\n\nSegment::forUser(new SimpleSegmentAnonymousUser('123'))-\u003etrack('Kitchen sink used');\n```\n\n### Laravel Notifications\nThis package includes an out-of-the-box notification channel, to allow you to use Laravel's built-in notification\nfeature. To send Segment events to users as notifications, generate your notification as normal;\n\n```\nphp artisan make:notification UserSubscribed\n```\n\nYou must ensure your notification implements the `CanNotifyViaSegment` interface, and add the required `toSegment`\nmethod. Then you can configure the `via` method to include the `SegmentChannel` class.\n\nYou can then adjust the `toSegment` method to return the event you'd like. \n\n```php\nuse Illuminate\\Notifications\\Notification;\nuse SlashEquip\\LaravelSegment\\Contracts\\CanBeIdentifiedForSegment;\nuse SlashEquip\\LaravelSegment\\Contracts\\CanBeSentToSegment;\nuse SlashEquip\\LaravelSegment\\Contracts\\CanNotifyViaSegment;\nuse SlashEquip\\LaravelSegment\\Notifications\\SegmentChannel;\nuse SlashEquip\\LaravelSegment\\SimpleSegmentEvent;\n\nclass UserSubscribed extends Notification implements CanNotifyViaSegment\n{\n    public function __construct(\n    ) {\n    }\n\n    public function via(object $notifiable): array\n    {\n        return [SegmentChannel::class];\n    }\n\n    public function toSegment(CanBeIdentifiedForSegment $notifiable): CanBeSentToSegment\n    {\n        return new SimpleSegmentEvent(\n            $notifiable,\n            'User Subscribed',\n            [\n                'plan' =\u003e 'basic',\n                'team_name' =\u003e 'Funky chickens',\n            ],\n        );\n    }\n}\n```\n\n## Misc\n\n### Deferring\nWhen you start to fire many events in your application, even 2-3 per request it can be hyper-beneficial to\nturn on deferring (see config). When deferring is enabled, the service will store all of your tracking events triggered\nthrough-out the request or process and then send them in batch after your application has responded to your user. This\nhappens during the Laravel termination.\n\n### Safe mode\nBy default, safe-mode is turned on. When safe-mode is active it will swallow any exceptions thrown when making the HTTP\nrequest to Segment and report them automatically to the exception handler, allow your app to continue running. When\ndisabled then the exception will be thrown.\n\n## Testing\n\n```bash\n./vendor/bin/pest\n```\n\n### Using `Segment::fake()` in tests\n\nTo prevent real HTTP requests when running your own test suite, call\n`Segment::fake()` before executing the code under test. This swaps the\nSegment service with a fake that records events and identity calls. You can\nthen use assertion helpers to verify what was sent:\n\n```php\nuse SlashEquip\\LaravelSegment\\Facades\\Segment;\n\nSegment::fake();\n\n// Code that should trigger Segment events\n\nSegment::assertTracked('User Signed Up');\nSegment::assertIdentified();\n```\n\n## Changelog\n\nPlease see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.\n\n## Contributing\n\nPlease see [CONTRIBUTING](.github/CONTRIBUTING.md) for details.\n\n## Security Vulnerabilities\n\nPlease review [our security policy](../../security/policy) on how to report security vulnerabilities.\n\n## Credits\n\n- [SlashEquip](https://github.com/slashequip)\n- [All Contributors](../../contributors)\n\n## License\n\nThe MIT License (MIT). Please see [License File](LICENSE.md) for more information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fslashequip%2Flaravel-segment","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fslashequip%2Flaravel-segment","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fslashequip%2Flaravel-segment/lists"}