# README
PostHog Go
Please see the main PostHog docs.
Specifically, the Go integration details.
Quickstart
Install posthog to your gopath
$ go get github.com/posthog/posthog-go
Go 🦔!
package main
import (
"os"
"github.com/posthog/posthog-go"
)
func main() {
client := posthog.New(os.Getenv("POSTHOG_API_KEY")) // This value must be set to the project API key in PostHog
// alternatively, you can do
// client, _ := posthog.NewWithConfig(
// os.Getenv("POSTHOG_API_KEY"),
// posthog.Config{
// PersonalApiKey: "your personal API key", // Set this to your personal API token you want feature flag evaluation to be more performant. This will incur more costs, though
// Endpoint: "https://us.i.posthog.com",
// },
// )
defer client.Close()
// Capture an event
client.Enqueue(posthog.Capture{
DistinctId: "test-user",
Event: "test-snippet",
Properties: posthog.NewProperties().
Set("plan", "Enterprise").
Set("friends", 42),
})
// Add context for a user
client.Enqueue(posthog.Identify{
DistinctId: "user:123",
Properties: posthog.NewProperties().
Set("email", "[email protected]").
Set("proUser", false),
})
// Link user contexts
client.Enqueue(posthog.Alias{
DistinctId: "user:123",
Alias: "user:12345",
})
// Capture a pageview
client.Enqueue(posthog.Capture{
DistinctId: "test-user",
Event: "$pageview",
Properties: posthog.NewProperties().
Set("$current_url", "https://example.com"),
})
// Capture event with calculated uuid to deduplicate repeated events.
// The library github.com/google/uuid is used
key := myEvent.Id + myEvent.Project
uid := uuid.NewSHA1(uuid.NameSpaceX500, []byte(key)).String()
client.Enqueue(posthog.Capture{
Uuid: uid,
DistinctId: "test-user",
Event: "$pageview",
Properties: posthog.NewProperties().
Set("$current_url", "https://example.com"),
})
// Check if a feature flag is enabled
isMyFlagEnabled, err := client.IsFeatureEnabled(
FeatureFlagPayload{
Key: "flag-key",
DistinctId: "distinct_id_of_your_user",
})
if isMyFlagEnabled == true {
// Do something differently for this user
}
}
Testing Locally
You can run your Go app against a local build of posthog-go
by making the following change to your go.mod
file for whichever your app, e.g.
module example/posthog-go-app
go 1.22.5
require github.com/posthog/posthog-go v0.0.0-20240327112532-87b23fe11103
require github.com/google/uuid v1.3.0 // indirect
replace github.com/posthog/posthog-go => /path-to-your-local/posthog-go
Questions?
Visit the community forum.
# Functions
Creates a backo instance with the following defaults:
base: 100 milliseconds factor: 2 jitter: 0 cap: 10 seconds.
Instantiate a new client that uses the write key passed as first argument to send messages to the backend.
Creates a backo instance with the given parameters.
No description provided by the author
No description provided by the author
Instantiate a new client that uses the write key and configuration passed as arguments to send messages to the backend.
This function instantiate an object that statisfies the posthog.Logger interface and send logs to standard logger passed as argument.
# Constants
This constant sets the default batch size used by client instances if none was explicitly set.
This constant sets the default endpoint to which client instances send messages if none was explictly set.
Specifies the default timeout for fetching feature flags.
Specifies the default interval at which to fetch new feature flags.
This constant sets the default flush interval used by client instances if none was explicitly set.
No description provided by the author
No description provided by the author
No description provided by the author
Version of the client.
# Variables
This error is returned by methods of the `Client` interface when they are called after the client was already closed.
This error is used to notify the client callbacks that a message send failed because the JSON representation of a message exceeded the upper limit.
This error is used to notify the application that too many requests are already being sent and no more messages can be accepted.
# Structs
This type represents object sent in a alias call.
No description provided by the author
No description provided by the author
No description provided by the author
This type represents object sent in a capture call.
No description provided by the author
Instances of this type carry the different configuration options that may be set when instantiating a client.
Returned by the `NewWithConfig` function when the one of the configuration fields was set to an impossible value (like a negative duration).
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
Instances of this type are used to represent errors returned when a field was no initialize properly in a structure passed as argument to one of the functions of this package.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
This type represents object sent in an identify call.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
# Interfaces
No description provided by the author
Values implementing this interface are used by posthog clients to notify the application when a message send succeeded or failed.
This interface is the main API exposed by the posthog package.
Instances of types implementing this interface can be used to define where the posthog client logs are written.
This interface is used to represent posthog objects that can be sent via a client.
# Type aliases
No description provided by the author
This type is used to represent properties in messages that support it.