Skip to content

Latest commit

 

History

History
64 lines (45 loc) · 1.89 KB

README.md

File metadata and controls

64 lines (45 loc) · 1.89 KB

Segment Proxy

This package allows to expose a webhook-ready interface to the Segment.io API. It can be used to accept webhooks from e.g. Zapier, and so to talk to the Segment API from there.

Usage

The root package implements HTTP handlers for identify, group, and track. They can be installed by calling segmentproxy.Register() on a ServeMux (e.g. http.DefaultServeMux).

config := segmentproxy.Config{
    Segment: analytics.New("SegmentWriteKey"),
}

segmentproxy.Register(config, http.DefaultServeMux)

log.Fatal(http.ListenAndServe(":8080", nil))

Endpoints

A server with the handlers installed will accept requests on /identify, /group, and /track (with an optional prefix). Each endpoint takes a request body in JSON format, in the same format that Segment.io accepts.

Email to ID conversion

When the caller doesn't know the user id, it's possible to pass an email attribute instead. When the email field is non-empty, segmentproxy.Config.EmailToID is used to map the email to an ID before sending the payload to Segment.

Simple server

The ./simpleserver package is a minimal runnable server. It accepts a single --segment-write-key command line parameter, and listens on port 8080.

Example usage:

curl localhost:8080/identify -XPOST -d '{"userId": "foo"}'

Multiple environments

It can be convenient to use different segment accounts or projets. This can be achieved by using segmentproxy.Register() multiple times with different segment instances and prefixes.

prodConfig := segmentproxy.Config{
    Segment: analytics.New("ProdKey"),
    Prefix: "/prod"
}
stagingConfig := segmentproxy.Config{
    Segment: analytics.New("StagingKey"),
    Prefix: "/staging"
}

segmentproxy.Register(prodConfig, http.DefaultServeMux)
segmentproxy.Register(stagingConfig, http.DefaultServeMux)

log.Fatal(http.ListenAndServe(":8080", nil))