Flagsmith allows you to manage feature flags and remote config across multiple projects, environments and organisations.
This is the SDK for go for https://flagsmith.com/.
For full documentation visit https://docs.flagsmith.com/clients/server-side?language=go.
Enable events with WithEvents. Its context runs the background sender.
client := flagsmith.NewClient(os.Getenv("FLAGSMITH_SERVER_KEY"), flagsmith.WithEvents(context.Background()))
ec := flagsmith.NewEvaluationContext("user-123", map[string]interface{}{"plan": "premium"})
flag, err := client.GetExperimentFlag(ctx, "checkout_cta", ec)GetExperimentFlag returns the flag and records a $flag_exposure when the identity is enrolled in a running experiment. TrackExposureEvent records one for a flag you evaluated some other way.
Record conversions with TrackEvent. Names starting with $ are reserved.
err = client.TrackEvent("purchase", &flagsmith.EventOptions{Identifier: "user-123", Value: 49.99})Events are sent every 10 seconds (WithEventsFlushInterval) or when 1000 are buffered (WithEventsMaxBufferSize). Failures never reach the code that tracks, flushes or closes.
- Network errors, requests unanswered after 3 seconds (
WithEventsRequestTimeout), and 408, 429, 500, 502, 503 and 504 are retried: three attempts, backoff from 1 second (WithEventsRetryBackoff) doubling to 10 seconds, with full jitter. A batch that still fails goes back to the front of the buffer until the next scheduled send orFlushEvents, unless its last attempt got a 500: then it is dropped. - Any other error status drops the batch.
- A 401 or 403 stops event tracking, drops the buffer and logs one warning, until the client is re-created. Flags keep working.
- One scheduled send is in flight at a time. When the buffer is full meanwhile, the oldest events are dropped.
- Events a 202 lists as rejected are logged by index and not resent.
- Equal exposures are sent once until the batch carrying them succeeds.
- Traits and metadata are captured when tracked. Values that cannot be encoded as JSON drop the event.
GetExperimentFlagsends the identity's traits, transient ones included. It andTrackExposureEventreturn an error for a blank identifier;TrackEventsends one as none.- The events request carries the SDK's own environment key and user agent.
WithCustomHeadersis not applied to it. - Logs never include identifiers, trait values or response content.
DroppedEvents returns a running count of events lost in any of these ways, including at shutdown.
Events cannot be used with WithOfflineMode.
Close the client before the application exits, so that buffered events are not lost. Close sends them, retries included, and returns by its context's deadline, dropping and counting what is left. It also stops the client's environment polling, realtime updates and analytics.
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
client.Close(ctx)FlushEvents sends buffered events without closing the client. It returns once every event tracked before the call has been sent, kept for retry or dropped, or with its context's error when that ends first.
Cancelling the WithEvents context also closes event sending, within one events request timeout. The same deadline cuts sends already in progress, including FlushEvents.
Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.
If you encounter a bug or feature request we would like to hear about it. Before you submit an issue please search existing issues in order to prevent duplicates.
If you have any questions about our projects you can email support@flagsmith.com.