PHPackages                             daanra/laravel-segment - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. daanra/laravel-segment

ActiveLibrary[API Development](/categories/api)

daanra/laravel-segment
======================

Laravel Segment is an opinionated, approach to integrating Segment into your Laravel application.

v1.0.9(10mo ago)05.5k—10%MITPHPPHP ^8.0

Since Mar 28Pushed 10mo agoCompare

[ Source](https://github.com/Daanra/laravel-segment)[ Packagist](https://packagist.org/packages/daanra/laravel-segment)[ Docs](https://github.com/Daanra/laravel-segment)[ RSS](/packages/daanra-laravel-segment/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (8)Versions (15)Used By (0)

Laravel Segment
===============

[](#laravel-segment)

[![Latest Version on Packagist](https://camo.githubusercontent.com/5fa6c1a2fc8075263c8eaa5dd69ffe94d23171d84c9cddd2943b49567795b30d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f736c61736865717569702f6c61726176656c2d7365676d656e742e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/slashequip/laravel-segment)[![GitHub Tests Action Status](https://camo.githubusercontent.com/f5f3d84203d2657e42a68c54ef27e2a50cde49c782a1fb472083d3ae05180082/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f736c61736865717569702f6c61726176656c2d7365676d656e742f72756e2d74657374733f6c6162656c3d7465737473)](https://github.com/slashequip/laravel-segment/actions?query=workflow%3ATests+branch%3Amaster)[![GitHub Code Style Action Status](https://camo.githubusercontent.com/1d07407d6844a3e3d88e34fd691efe01cfe5e2e928aec02ce83de62d194b2e42/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f736c61736865717569702f6c61726176656c2d7365676d656e742f436865636b253230262532306669782532307374796c696e673f6c6162656c3d636f64652532307374796c65)](https://github.com/slashequip/laravel-segment/actions?query=workflow%3A%22Check+%26+fix+styling%22+branch%3Amaster)[![Total Downloads](https://camo.githubusercontent.com/1eb904e9a6708c6f45a63c5467b458329fca2ee7c109c549ab8e926b13c61d00/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f736c61736865717569702f6c61726176656c2d7365676d656e742e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/slashequip/laravel-segment)

[![Laravel Segment Logo Banner](https://github.com/slashequip/laravel-segment/raw/main/laravel-segment-banner.svg?raw=true)](https://github.com/slashequip/laravel-segment/blob/main/laravel-segment-banner.svg?raw=true)

Laravel Segment is an opinionated, approach to integrating Segment into your Laravel application.

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require daanra/laravel-segment
```

You can publish the config file with:

```
php artisan vendor:publish --provider="SlashEquip\LaravelSegment\LaravelSegmentServiceProvider"
```

This is the contents of the published config file, which should be located at `config/segment.php`:

```
return [
    'enabled' => env('SEGMENT_ENABLED', true),

    /**
     * This is your Segment API write key. It can be
     * found under Source > Settings > Api Keys
     */
    'write_key' => env('SEGMENT_WRITE_KEY', null),

    /**
     * Should the Segment service defer all tracking
     * api calls until after the response, sending
     * everything using the bulk/batch api?
     */
    'defer' => env('SEGMENT_DEFER', false),

    /**
     * Should the Segment service be run in safe mode.
     * Safe mode will only report errors in sending
     * when safe mode is off exceptions are thrown
     */
    'safe_mode' => env('SEGMENT_SAFE_MODE', true),
];
```

Setting your write key
----------------------

[](#setting-your-write-key)

Your write key is the API key given to you by Segment which can be found under your PHP source settings; `https://app.segment.com/{your-workspace-name}/sources/{your-source-name}/settings/keys` in the Segment UI.

What is a Segment User
----------------------

[](#what-is-a-segment-user)

When we talk about a 'user' in the context of this package we mean any object that implements the `SlashEquip\LaravelSegment\Contracts\CanBeIdentifiedForSegment` contract the package comes with a trait (and the interface) you can attach to your default User model;

```
use Illuminate\Database\Eloquent\Model;
use SlashEquip\LaravelSegment\Traits\HasSegmentIdentityByKey;
use SlashEquip\LaravelSegment\Contracts\CanBeIdentifiedForSegment;

class User extends Model implements CanBeIdentifiedForSegment
{
    use HasSegmentIdentityByKey;
}
```

Using this trait will automagically use your users' primary key as the identifier that is sent to Segment. Alternatively, you can implement your own instance of the `public function getSegmentIdentifier(): string;` method on your User model and not use the trait.

### Globally identifying users

[](#globally-identifying-users)

If you are sending Segment events in multiple places through your application and through-out a request it might make sense to globally identify a user to make it more convenient when making tracking calls.

```
use SlashEquip\LaravelSegment\Facades\Segment;

Segment::setGlobalUser($user);
```

### Globally setting context

[](#globally-setting-context)

Segment allows you to send (context)\[\] with your tracking events too, you can set a global context that applies to all tracking events.

```
use SlashEquip\LaravelSegment\Facades\Segment;

Segment::setGlobalContext([
    'ip' => '127.0.0.1',
    'locale' => 'en-US',
    'screen' => [
        'height' => 1080,
        'width' => 1920,
    ],
]);
```

### Here have some convenience

[](#here-have-some-convenience)

Laravel Segment ships with a middleware that you can apply in your HTTP Kernal that will handle the setting of the global user and some sensible global context too. It should be simple to extend this middleware and adjust for your needs if you want to add to the default context provided.

```
    'api' => [
        // ... other middleware
        SlashEquip\LaravelSegment\Middleware\ApplySegmentGlobals::class
    ],
```

Usage
-----

[](#usage)

### For tracking events

[](#for-tracking-events)

```
use SlashEquip\LaravelSegment\Facades\Segment;

Segment::forUser($user)->track('User Signed Up', [
    'source' => 'Product Hunt',
]);

// If you have set a global user you can
// use the simpler provided syntax.
Segment::track('User Signed Up', [
    'source' => 'Product Hunt',
]);
```

### For identifying users

[](#for-identifying-users)

```
use SlashEquip\LaravelSegment\Facades\Segment;

Segment::forUser($user)->identify([
    'last_logged_in' => '2021-03-24 20:05:30',
    'latest_subscription_amount' => '$24.60',
]);

// If you have set a global user you can
// use the simpler provided syntax.
Segment::identify([
    'last_logged_in' => '2021-03-24 20:05:30',
    'latest_subscription_amount' => '$24.60',
]);
```

Misc
----

[](#misc)

### Deferring

[](#deferring)

When you start to fire many events in your application, even 2-3 per request it can be hyper-beneficial to turn on deferring (see config). When deferring is enabled, the service will store all of your tracking events triggered through-out the request or process and then send them in batch after your application has responded to your user. This happens during the Laravel termination.

### Safe mode

[](#safe-mode)

By default safe-mode is turned on. When safe-mode is active it will swallow any exceptions thrown when making the HTTP request to Segmenta and report them automatically to the exception handler, allow your app to continue running. When disabled then the exception will be thrown.

Testing
-------

[](#testing)

```
./vendor/bin/pest
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](.github/CONTRIBUTING.md) for details.

Security Vulnerabilities
------------------------

[](#security-vulnerabilities)

Please review [our security policy](../../security/policy) on how to report security vulnerabilities.

Credits
-------

[](#credits)

- [SlashEquip](https://github.com/slashequip)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

41

—

FairBetter than 89% of packages

Maintenance54

Moderate activity, may be stable

Popularity22

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity65

Established project with proven stability

 Bus Factor1

Top contributor holds 54.9% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~142 days

Recently: every ~260 days

Total

12

Last Release

317d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/7329241f78d1dc1731c0c669a93c7084a1423474a7c1526a660c46c844e6ff98?d=identicon)[daanraatjes](/maintainers/daanraatjes)

---

Top Contributors

[![slashequip](https://avatars.githubusercontent.com/u/2316916?v=4)](https://github.com/slashequip "slashequip (28 commits)")[![Daanra](https://avatars.githubusercontent.com/u/6588838?v=4)](https://github.com/Daanra "Daanra (23 commits)")

---

Tags

laravel-segment

###  Code Quality

TestsPest

Static AnalysisPsalm

Type Coverage Yes

### Embed Badge

![Health badge](/badges/daanra-laravel-segment/health.svg)

```
[![Health](https://phpackages.com/badges/daanra-laravel-segment/health.svg)](https://phpackages.com/packages/daanra-laravel-segment)
```

###  Alternatives

[netflie/whatsapp-cloud-api

The first PHP SDK to send and receive messages using a cloud-hosted version of the WhatsApp Business Platform

640431.7k4](/packages/netflie-whatsapp-cloud-api)[tencentcloud/tencentcloud-sdk-php

TencentCloudApi php sdk

3731.2M42](/packages/tencentcloud-tencentcloud-sdk-php)[slashequip/laravel-segment

Laravel Segment is an opinionated, approach to integrating Segment into your Laravel application.

2670.5k](/packages/slashequip-laravel-segment)[simplestats-io/laravel-client

Client for SimpleStats!

4515.5k](/packages/simplestats-io-laravel-client)[njoguamos/laravel-plausible

A laravel package for interacting with plausible analytics api.

208.8k](/packages/njoguamos-laravel-plausible)[spatie/spatie-price-api

The Price API used at promotional sites for our own products

1515.1k1](/packages/spatie-spatie-price-api)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
