molasses

package module
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Feb 11, 2021 License: MIT Imports: 14 Imported by: 0

README

Molasses-Go

codecov Build status

A Go SDK for Molasses. It allows you to evaluate a user's status for a feature. It also helps simplify logging events for A/B testing.

The SDK uses SSE to communicate with the Molasses Application. Once initialized, it takes microseconds to evaluate if a user is active. When you update a feature on Molasses, it sends that update to all of your clients through SSE and users would start experiencing that change instantly.

Install

go get github.com/molassesapp/molasses-go

Usage

Initialization

Start by initializing the client with an APIKey. This begins the polling for any feature updates. The updates happen every 15 seconds.

	client, err := molasses.Init(molasses.ClientOptions{
		APIKey: os.Getenv("MOLASSES_API_KEY"),
	})

If you decide to automatically track analytics events (experiment started, experiment success) you can turn them off by setting the AutoSendEvents field to true

	client, err := molasses.Init(molasses.ClientOptions{
		APIKey: 		os.Getenv("MOLASSES_API_KEY"),
		AutoSendEvents: true,
  })

Check if feature is active

You can call isActive with the key name and optionally a user's information. The ID field is used to determine whether a user is part of a percentage of users. If you have other constraints based on user params you can pass those in the Params field.

client.IsActive("TEST_FEATURE_FOR_USER", molasses.User{
		ID: "baz",
		Params: map[string]string{
			"teamId": "12356",
		},
	})

You can check if a feature is active for a user who is anonymous by just calling isActive with the key. You won't be able to do percentage roll outs or track that user's behavior.

client.IsActive("TEST_FEATURE_FOR_USER")

Track Events

If you want to track any event call the Track method. Track takes the event's name, the molasses User and any additional parameters for the event.

client.Track("Checkout Submitted", molasses.User{
		ID: "baz",
		Params: map[string]interface{}{
			"teamId": "12356",
		},
	}, map[string]interface{}{
		"version": "v2.3.0"
	})

Experiments

To track the start of an experiment, you can call ExperimentStarted. ExperimentStarted takes the feature's name, the molasses User and any additional parameters for the event.

client.ExperimentStarted("GOOGLE_SSO", molasses.User{
		ID: "baz",
		Params: map[string]interface{}{
			"teamId": "12356",
		},
	}, map[string]interface{}{
		"version": "v2.3.0"
	})

To track whether an experiment was successful you can call ExperimentSuccess. ExperimentSuccess takes the feature's name, the molasses User and any additional parameters for the event.

client.ExperimentSuccess("GOOGLE_SSO", molasses.User{
		ID: "baz",
		Params: map[string]interface{}{
			"teamId": "12356",
		},
	}, map[string]interface{}{
		"version": "v2.3.0"
	})

Example


import (
	"fmt"
	"os"

	"github.com/molassessapp/molasses-go"
)

func main() {
	client, err := molasses.Init(molasses.ClientOptions{
		APIKey: os.Getenv("MOLASSES_API_KEY"),
	})

	if client.IsActive("New Checkout") {
		fmt.Println("we are a go")
	} else {
		fmt.Println("we are a no go")
	}

	if client.IsActive("Another feature", molasses.User{
		ID: "baz",
		Params: map[string]interface{}{
			"teamId": "12356",
		},
	}) {
		fmt.Println("we are a go")
	} else {
		fmt.Println("we are a no go")
	}
}

Documentation

Overview

Package molasses is a Go SDK for Molasses. It allows you to evaluate user's status for a feature. It also helps simplify logging events for A/B testing.

Molasses uses polling to check if you have updated features. Once initialized, it takes microseconds to evaluate if a user is active.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

func (*Client) ExperimentStarted added in v0.4.0

func (c *Client) ExperimentStarted(key string, user User, additionalDetails map[string]interface{})

func (*Client) ExperimentSuccess added in v0.2.0

func (c *Client) ExperimentSuccess(key string, user User, additionalDetails map[string]interface{})

func (*Client) IsActive

func (c *Client) IsActive(key string, user ...User) bool

IsActive - Check to see if a feature is active for a user. You must pass the key of the feature (ex. SHOW_USER_ONBOARDING) and optionally pass the user who you are evaluating. if you pass more than 1 user value, the first will only be evaluated

func (*Client) IsInitiated added in v0.2.0

func (c *Client) IsInitiated() bool

func (*Client) Stop added in v0.2.0

func (c *Client) Stop()

func (*Client) Track added in v0.5.0

func (c *Client) Track(eventName string, user User, additionalDetails map[string]interface{})

type ClientInterface added in v0.2.0

type ClientInterface interface {
	IsActive(key string, user ...User) bool
	Stop()
	IsInitiated() bool
	Track(eventName string, user User, additionalDetails map[string]interface{})
	ExperimentStarted(key string, user User, additionalDetails map[string]interface{})
	ExperimentSuccess(key string, user User, additionalDetails map[string]interface{})
}

func Init

func Init(options ClientOptions) (ClientInterface, error)

Init - Creates a new client to interface with Molasses. Receives a ClientOptions

type ClientOptions

type ClientOptions struct {
	APIKey         string     // APIKey is the required field.
	URL            string     // URL can be updated if you are using a hosted version of Molasses
	Debug          bool       // Debug - whether to log debug info
	HTTPClient     HttpClient // HTTPClient - Pass in your own http client
	AutoSendEvents bool
	Polling        bool
}

ClientOptions - The options for the Molasses client to start, the APIKey is required

type HttpClient added in v0.3.0

type HttpClient interface {
	Do(req *http.Request) (*http.Response, error)
}

type User

type User struct {
	ID     string
	Params map[string]interface{}
}

User - The representation of your user

Jump to

Keyboard shortcuts

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