optimizely

package module
v0.6.2 Latest Latest
Warning

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

Go to latest
Published: Aug 28, 2019 License: Apache-2.0 Imports: 11 Imported by: 0

README

optimizely-sdk-go

GoDoc Build Status codecov Go Report Card

This project aims to provide an SDK for interacting with Optimizely full stack in Go.

This project is in early development stages and does not implement the full feature-set of the official Optimizely SDKs for other languages nor is it officially endorsed by Optimizely. PRs for feature additions and bug fixes are welcome!

Supported features:

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ActivatedImpression

func ActivatedImpression(i Impression) func(*Events) error

ActivatedImpression adds the variation impression to the set of reported events. Note that while many impressions can be added as events, each impression must have originated from the same Optimizely account or an error will be returned while creating the events.

func AnonymizeIP

func AnonymizeIP(anonymize bool) func(*Events) error

AnonymizeIP sets the anonymize IP flag on the events. Defaults to true.

func ClientName

func ClientName(name string) func(*Events) error

ClientName sets the client name property on the events. By default, the client name will be set to the path of this library, i.e. github.com/spothero/optimizely-sdk-go.

func ClientVersion

func ClientVersion(version string) func(*Events) error

ClientVersion overrides the client version of this library. If using Go 1.12+ and Go modules, the version of this library will be extracted from the build information. Otherwise, unless ClientVersion is set here, no version will be reported to Optimizely.

func EnrichDecisions

func EnrichDecisions(enrich bool) func(*Events) error

EnrichDecisions sets the enrich decisions property on the events. Defaults to true.

func ReportEvents

func ReportEvents(client api.Client, events Events) error

ReportEvents is a convenience wrapper for sending events to the Optimizely reporting API that marshals the events to JSON and calls the api package.

Note: The provided client does not necessarily have to be instantiated with a token as the events endpoint does not require one.

Types

type Datafile added in v0.5.3

type Datafile struct {
	Version     string               `json:"version"`
	Revision    string               `json:"revision"`
	ProjectID   string               `json:"projectId"`
	AccountID   string               `json:"accountId"`
	Experiments []DatafileExperiment `json:"experiments"`
}

Datafile used for loading the JSON datafile from Optimizely

func GetDatafile added in v0.6.0

func GetDatafile(client api.Client, environmentName string, projectID int) (Datafile, error)

GetDatafile is a convenience wrapper around the api package's GetDatafile method that unmarshals the datafile from the Optimizely API.

type DatafileExperiment added in v0.5.3

type DatafileExperiment struct {
	ID                string                      `json:"id"`
	Key               string                      `json:"key"`
	LayerID           string                      `json:"layerId"`
	Status            string                      `json:"status"`
	Variations        []DatafileVariation         `json:"variations"`
	TrafficAllocation []DatafileTrafficAllocation `json:"trafficAllocation"`
	ForcedVariations  map[string]string           `json:"forcedVariations"`
}

DatafileExperiment is the structure of the experiment within a datafile. This type is only used when deserializing the datafile.

type DatafileTrafficAllocation added in v0.5.3

type DatafileTrafficAllocation struct {
	EntityID   string `json:"entityId"`
	EndOfRange int    `json:"endOfRange"`
}

DatafileTrafficAllocation is the structure of the traffic allocation with a datafile. This type is only used when deserializing the datafile.

type DatafileVariation added in v0.5.3

type DatafileVariation struct {
	ID  string `json:"id"`
	Key string `json:"key"`
}

DatafileVariation is an experiment variation within a datafile used for deserialization.

type Events

type Events eventBatch

Events are reportable actions back to the Optimizely API. Currently only impression events are supported.

func EventsFromContext

func EventsFromContext(ctx context.Context, options ...func(*Events) error) *Events

EventsFromContext creates Events from all the impressions that were seen during the lifecycle of the provided context. If no impressions were seen or no project was found in the provided context, nil is returned. The options provided to this function match the options provided to NewEvents with the exception that the ActivatedImpression function should never be provided as an option and may result in a panic if the provided impression was created by a project in a different account from the project stored in the context.

func NewEvents

func NewEvents(options ...func(*Events) error) (Events, error)

NewEvents constructs a set of reportable events from the provided options.

type Experiment

type Experiment struct {
	Key string
	// contains filtered or unexported fields
}

Experiment represents a single Optimizely experiment. It contains metadata as well as the traffic allocation for the experiment and any forced variations.

type Impression

type Impression struct {
	Variation
	UserID    string
	Timestamp time.Time
}

Impression is the outcome of bucketing a user into a specific variation. This type holds the variation that the user was bucketed into, the user ID that generated the outcome, and the timestamp at which the variation was generated.

type Project

type Project struct {
	Version   string
	Revision  string
	ProjectID string
	AccountID string

	RawDataFile json.RawMessage
	// contains filtered or unexported fields
}

Project is an Optimizely project containing a set of experiments. Project also includes the raw JSON datafile which was used to generate the Project.

func NewProjectFromDataFile

func NewProjectFromDataFile(datafileJSON []byte) (Project, error)

NewProjectFromDataFile creates a new Optimizely project given the raw JSON datafile

func (Project) GetVariation

func (p Project) GetVariation(experimentName, userID string) *Impression

GetVariation returns an impression, if applicable, for a given experiment and a given user id. If no variation is applicable, nil is returned. The Impression returned by this method can be used later to generate events for reporting to the Optimizely API.

func (Project) ToContext

func (p Project) ToContext(ctx context.Context, userID string) context.Context

ToContext creates a context with the project as a value in the context for a specific user ID. By using GetVariation with the context returned from this method, not only will each Impression be returned to the caller, but each Impression will be recorded in the context. Once the lifecycle of the context is complete, use EventsFromContext to create a unified Events object containing every impression that occurred during the context's lifecycle. This provides simplified API for bucketing a user across multiple experiments and multiple code-paths.

type Variation

type Variation struct {
	Key string
	// contains filtered or unexported fields
}

Variation represents a variation of an Optimizely experiment.

func GetVariation

func GetVariation(ctx context.Context, experimentName string) Variation

GetVariation returns the variation, if applicable, for the given experiment name from the project and user ID stored in the context. See Project.ToContext for more details.

Directories

Path Synopsis
Package API provides functionality for interacting with the Optimizely REST API.
Package API provides functionality for interacting with the Optimizely REST API.

Jump to

Keyboard shortcuts

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