analytics

package module
v0.0.0-...-e76ad97 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 30, 2020 License: MIT Imports: 13 Imported by: 0

README ¶

analytics-go

The Segment analytics client for Go.

Latest version is the v3 branch, which is canonical and documented. (The master branch contains the v2 library code to preserve backward compatibility for folks not using go modules.)

For more documentation see the official Segment Go library documentation or view the godocs.


You can't fix what you can't measure

Analytics helps you measure your users, product, and business. It unlocks insights into your app's funnel, core business metrics, and whether you have product-market fit.

How to get started

  1. Collect analytics data from your app(s).
    • The top 200 Segment companies collect data from 5+ source types (web, mobile, server, CRM, etc.).
  2. Send the data to analytics tools (for example, Google Analytics, Amplitude, Mixpanel).
    • Over 250+ Segment companies send data to eight categories of destinations such as analytics tools, warehouses, email marketing and remarketing systems, session recording, and more.
  3. Explore your data by creating metrics (for example, new signups, retention cohorts, and revenue generation).
    • The best Segment companies use retention cohorts to measure product market fit. Netflix has 70% paid retention after 12 months, 30% after 7 years.

Segment collects analytics data and allows you to send it to more than 250 apps (such as Google Analytics, Mixpanel, Optimizely, Facebook Ads, Slack, Sentry) just by flipping a switch. You only need one Segment code snippet, and you can turn integrations on and off at will, with no additional code. Sign up with Segment today.

Why?
  1. Power all your analytics apps with the same data. Instead of writing code to integrate all of your tools individually, send data to Segment, once.

  2. Install tracking for the last time. We're the last integration you'll ever need to write. You only need to instrument Segment once. Reduce all of your tracking code and advertising tags into a single set of API calls.

  3. Send data from anywhere. Send Segment data from any device, and we'll transform and send it on to any tool.

  4. Query your data in SQL. Slice, dice, and analyze your data in detail with Segment SQL. We'll transform and load your customer behavioral data directly from your apps into Amazon Redshift, Google BigQuery, or Postgres. Save weeks of engineering time by not having to invent your own data warehouse and ETL pipeline.

    For example, you can capture data on any app:

    analytics.track('Order Completed', { price: 99.84 })
    

    Then, query the resulting data in SQL:

    select * from app.order_completed
    order by price desc
    
🚀 Startup Program
If you are part of a new startup (<$5M raised, <2 years since founding), we just launched a new startup program for you. You can get a Segment Team plan (up to $25,000 value in Segment credits) for free up to 2 years — apply here!

License

The library is released under the MIT license.

Documentation ¶

Index ¶

Examples ¶

Constants ¶

View Source
const BatchUploadMaxAttempts = 360

Max attempts for the retry when the batch upload to Segment API fails

View Source
const BatchUploadRetryInterval = 15 * time.Second

Retry interval when the batch upload to Segment API fails

View Source
const Endpoint = "https://api.segment.io"

Endpoint for the Segment API.

View Source
const Version = "2.1.0"

Version of the client.

Variables ¶

View Source
var Backo = backo.DefaultBacko()

Backoff policy.

View Source
var DefaultContext = map[string]interface{}{
	"library": map[string]interface{}{
		"name":    "analytics-go",
		"version": Version,
	},
}

DefaultContext of message batches.

Functions ¶

This section is empty.

Types ¶

type Alias ¶

type Alias struct {
	PreviousId string `json:"previousId"`
	UserId     string `json:"userId"`
	Message
}

Alias message.

type Batch ¶

type Batch struct {
	Context  map[string]interface{} `json:"context,omitempty"`
	Messages []interface{}          `json:"batch"`
	Message
}

Batch message.

type Client ¶

type Client struct {
	Endpoint string
	// Interval represents the duration at which messages are flushed. It may be
	// configured only before any messages are enqueued.
	Interval time.Duration
	Size     int
	Logger   *log.Logger
	Verbose  bool
	Client   http.Client
	// contains filtered or unexported fields
}

Client which batches messages and flushes at the given Interval or when the Size limit is exceeded. Set Verbose to true to enable logging output.

func New ¶

func New(key string) *Client

New client with write key.

func (*Client) Alias ¶

func (c *Client) Alias(msg *Alias) error

Alias buffers an "alias" message.

func (*Client) Close ¶

func (c *Client) Close() error

Close and flush metrics.

func (*Client) Group ¶

func (c *Client) Group(msg *Group) error

Group buffers an "group" message.

func (*Client) Identify ¶

func (c *Client) Identify(msg *Identify) error

Identify buffers an "identify" message.

func (*Client) Page ¶

func (c *Client) Page(msg *Page) error

Page buffers an "page" message.

func (*Client) Track ¶

func (c *Client) Track(msg *Track) error

Track buffers an "track" message.

type Group ¶

type Group struct {
	Context      map[string]interface{} `json:"context,omitempty"`
	Integrations map[string]interface{} `json:"integrations,omitempty"`
	Traits       map[string]interface{} `json:"traits,omitempty"`
	AnonymousId  string                 `json:"anonymousId,omitempty"`
	UserId       string                 `json:"userId,omitempty"`
	GroupId      string                 `json:"groupId"`
	Message
}

Group message.

type Identify ¶

type Identify struct {
	Context      map[string]interface{} `json:"context,omitempty"`
	Integrations map[string]interface{} `json:"integrations,omitempty"`
	Traits       map[string]interface{} `json:"traits,omitempty"`
	AnonymousId  string                 `json:"anonymousId,omitempty"`
	UserId       string                 `json:"userId,omitempty"`
	Message
}

Identify message.

type Message ¶

type Message struct {
	Type      string `json:"type,omitempty"`
	MessageId string `json:"messageId,omitempty"`
	Timestamp string `json:"timestamp,omitempty"`
	SentAt    string `json:"sentAt,omitempty"`
}

Message fields common to all.

type Page ¶

type Page struct {
	Context      map[string]interface{} `json:"context,omitempty"`
	Integrations map[string]interface{} `json:"integrations,omitempty"`
	Traits       map[string]interface{} `json:"properties,omitempty"`
	AnonymousId  string                 `json:"anonymousId,omitempty"`
	UserId       string                 `json:"userId,omitempty"`
	Category     string                 `json:"category,omitempty"`
	Name         string                 `json:"name,omitempty"`
	Message
}

Page message.

type Track ¶

type Track struct {
	Context      map[string]interface{} `json:"context,omitempty"`
	Integrations map[string]interface{} `json:"integrations,omitempty"`
	Properties   map[string]interface{} `json:"properties,omitempty"`
	AnonymousId  string                 `json:"anonymousId,omitempty"`
	UserId       string                 `json:"userId,omitempty"`
	Event        string                 `json:"event"`
	Message
}

Track message.

Example ¶
body, server := mockServer()
defer server.Close()

client := New("h97jamjwbh")
client.Endpoint = server.URL
client.now = mockTime
client.uid = mockId
client.Size = 1

client.Track(&Track{
	Event:  "Download",
	UserId: "123456",
	Properties: map[string]interface{}{
		"application": "Segment Desktop",
		"version":     "1.1.0",
		"platform":    "osx",
	},
})

fmt.Printf("%s\n", <-body)
Output:

{
  "batch": [
    {
      "event": "Download",
      "messageId": "I'm unique",
      "properties": {
        "application": "Segment Desktop",
        "platform": "osx",
        "version": "1.1.0"
      },
      "timestamp": "2009-11-10T23:00:00+0000",
      "type": "track",
      "userId": "123456"
    }
  ],
  "context": {
    "library": {
      "name": "analytics-go",
      "version": "2.1.0"
    }
  },
  "messageId": "I'm unique",
  "sentAt": "2009-11-10T23:00:00+0000"
}
Example (Context) ¶
body, server := mockServer()
defer server.Close()

client := New("h97jamjwbh")
client.Endpoint = server.URL
client.now = mockTime
client.uid = mockId
client.Size = 1

client.Track(&Track{
	Event:  "Download",
	UserId: "123456",
	Properties: map[string]interface{}{
		"application": "Segment Desktop",
		"version":     "1.1.0",
		"platform":    "osx",
	},
	Context: map[string]interface{}{
		"whatever": "here",
	},
})

fmt.Printf("%s\n", <-body)
Output:

{
  "batch": [
    {
      "context": {
        "whatever": "here"
      },
      "event": "Download",
      "messageId": "I'm unique",
      "properties": {
        "application": "Segment Desktop",
        "platform": "osx",
        "version": "1.1.0"
      },
      "timestamp": "2009-11-10T23:00:00+0000",
      "type": "track",
      "userId": "123456"
    }
  ],
  "context": {
    "library": {
      "name": "analytics-go",
      "version": "2.1.0"
    }
  },
  "messageId": "I'm unique",
  "sentAt": "2009-11-10T23:00:00+0000"
}
Example (Many) ¶
body, server := mockServer()
defer server.Close()

client := New("h97jamjwbh")
client.Endpoint = server.URL
client.now = mockTime
client.uid = mockId
client.Size = 3

for i := 0; i < 5; i++ {
	client.Track(&Track{
		Event:  "Download",
		UserId: "123456",
		Properties: map[string]interface{}{
			"application": "Segment Desktop",
			"version":     i,
		},
	})
}

fmt.Printf("%s\n", <-body)
Output:

{
  "batch": [
    {
      "event": "Download",
      "messageId": "I'm unique",
      "properties": {
        "application": "Segment Desktop",
        "version": 0
      },
      "timestamp": "2009-11-10T23:00:00+0000",
      "type": "track",
      "userId": "123456"
    },
    {
      "event": "Download",
      "messageId": "I'm unique",
      "properties": {
        "application": "Segment Desktop",
        "version": 1
      },
      "timestamp": "2009-11-10T23:00:00+0000",
      "type": "track",
      "userId": "123456"
    },
    {
      "event": "Download",
      "messageId": "I'm unique",
      "properties": {
        "application": "Segment Desktop",
        "version": 2
      },
      "timestamp": "2009-11-10T23:00:00+0000",
      "type": "track",
      "userId": "123456"
    }
  ],
  "context": {
    "library": {
      "name": "analytics-go",
      "version": "2.1.0"
    }
  },
  "messageId": "I'm unique",
  "sentAt": "2009-11-10T23:00:00+0000"
}

Directories ¶

Path Synopsis
Godeps
_workspace/src/github.com/jehiah/go-strftime
go implementation of strftime
go implementation of strftime
_workspace/src/github.com/xtgo/uuid
Package uuid can be used to generate and parse universally unique identifiers, a standardized format in the form of a 128 bit number.
Package uuid can be used to generate and parse universally unique identifiers, a standardized format in the form of a 128 bit number.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL