Segment PHP Client
This library provides a Web Service Client for the Segment.io HTTP API using Guzzle v5.
Install the latest version with:
$ composer require uecode/segment-io-php ~1.1
use SegmentIO\Client; $client = new Client(['write_key' => $writeKey]); // Identify the user - assuming, below, that you // have a $user object from your database $client->identify([ 'userId' => $user->getId(), 'traits' => [ 'name' => $user->getName(), 'email' => $user->getEmail() ] ]); // Track an event (userId or anonymousId is required for all events!) $client->track([ 'userId' => $user->getId(), 'event' => 'Some Event Happened', 'properties' => [ 'foo' => 'bar' ] ]);
The client accepts an array of configuration options:
|API Write Key||
||The Segment.io API Write Key|
||The API Version. Used to version the API (default:
||A method of batching calls to the API to reduce latency of over the wire requests (supports:
|Request Batching: Max Queue Size||
||When using Request Batching, this is the total amount of Events to queue before flushing|
|Request Batching: Batch Size||
||When using Request Batching, this is the total amount of Events sent in a single Request|
|File Batching: Log File||
||When using File Batching, this determines what file to log Events to|
By default, this client will attempt to queue all calls to the API and send them out over a single batch request. Because of the blocking nature of PHP, this method reduces the amount of time the Client has to wait for requests to the API.
Batching does not apply to the
import() method on the client.
There are two methods of Batching Available:
Note: This is enabled by default.
When making calls to the API, the events will be placed into a queue and will be
flushed under one of two cases: when / if the
max_queue_size is reached or at
the end of the PHP Request.
Changing the Client options for
batch_size will affect
how often the Client attempts to flush events.
The file batching is a more performant method for making requests to Segment.io.
Each time a track or identify call is made, it will record that call to a log file.
The log file is then uploaded “out of band” by running the included
You can change the location of the log file by using the
configuration option. If a log_file is not specified, it will default to:
To upload the Events from the log file to Segment.io, run the included
./parse YOUR_WRITE_KEY --file /tmp/segment-io.log
Tracking HTTP API Documentation
Documentation is available for the Tracking HTTP API at segment.io/docs/tracking-api/.
This software is released under the MIT License. See the license file for details.