Giter VIP home page Giter VIP logo

segment's Introduction

A modern Segment client. Uses PSR7, PSR17, and PSR18 for transport.

Installation

Add this repository to your composer repository config.

{
  "repositories": [
    {
      "type": "vcs",
      "url": "[email protected]:weirdly/segment.git"
    }
  ]
}

Then require weirdly/segment.

composer require weirdly/segment

Getting Started

Create a SegmentClient instance.

You'll need to provide your own implementations of the PSR standards, or install your favourite one. Suggestions:

$writeKey = 'abc123';
$httpClient = $requestFactory = $streamFactory = new Symfony\Component\HttpClient\Psr18Client();
$segmentHttpClient = new \Weirdly\Segment\Http\HttpClient($writeKey, $httpClient, $requestFactory, $streamFactory);
$client = new \Weirdly\Segment\SegmentClient($segmentHttpClient);

Usage

The main requests have shortcut methods on the client.

$client->identify('user_id', ['trait' => 'value']);
$client->track('user_id', 'Some Event', ['property' => 'value']);
$client->page('user_id', 'Page Name');
$client->screen('user_id', 'Screen Name');
$client->group('user_id', 'Group Name', ['trait' => 'value']);
$client->alias('user_id', 'previous_id');

If you don't yet know the user id, provide a unique, anonymous ID instead. Set the $anonymous argument to true.

$client->identify('user_id', ['trait' => 'value'], true);

For more fine-grained control, pass a request directly to the $client->send() method. E.g. to identify a previously anonymous user with a known user id, send an identify request with both the user id and anonymous id.

$request = \Weirdly\Segment\Request\Identify::createIdentifiedRequest('user_id')
    ->setAnonymousId('anonymous_id');
$client->send($request);

Some request classes have required arguments. When using the createAnonymousRequest() and createIdentifiedRequest() helper methods, pass required arguments after the user id.

$request = \Weirdly\Segment\Request\Track::createIdentifiedRequest('user_id', 'Event Name')

Or just instantiate the request without the helper method. The following results in an identical request to the above example.

$request = new \Weirdly\Segment\Request\Track('Event Name');
$request->setUserId('user_id');

Send multiple requests in a single batch round-trip.

$requests = [
    \Weirdly\Segment\Request\Page::createIdentifiedRequest('user_id')->setName('Page Name'),
    \Weirdly\Segment\Request\Track::createIdentifiedRequest('user_id', 'Event Name'),
];
$client->batch(...$requests);

segment's People

Contributors

cjunge-work avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.