Giter Site home page Giter Site logo

posthog-go's Introduction

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"))
    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"),
    })
}

Questions?

posthog-go's People

Contributors

alexkim205 avatar edscode avatar fuziontech avatar jamesefhawkins avatar liyiy avatar macobo avatar mariusandra avatar neilkakkar avatar tomoconnor avatar tristanspeakeasy avatar yakkomajuri avatar zaynetro avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

posthog-go's Issues

Data races with feature flag poller

SDK with feature flag poller enabled has data races:

go test -race -v .

There seem two be a couple of offenders:

  1. poller.fetchedFlagsSuccessfullyOnce (this and this)
  2. poller.featureFlags and everywhere where they are read with poller.GetFeatureFlags()

Full log: test.log

Possible improvements?

๐Ÿ‘‹ Hello!

I am trying out PostHog, and I wanted to use PostHog feature flags via OpenFeature, so I wrote a PostHog provider for OpenFeature. (You can find it https://github.com/craigpastro/posthog-openfeature-provider-go if you are interested ๐Ÿ™‚ )

Basically, the provider simply wraps IsFeatureEnabled. During the writing of the provider there were a few things I noticed that I think could be improved and wanted to ask what you thought.

So, these mostly relate to the IsFeatureEnabled method.

  1. The call doesn't error out if the API key is incorrect. It does log "posthog 2023/09/07 15:43:34 ERROR: Error calling /decide/" which is not that useful.
  2. It would be nice if this method returned a boolean.
  3. If the flag doesn't exist we have no way of knowing this and we just get back false. It would be nice to somehow know that the flag doesn't exist.
  4. Every request logs "posthog 2023/09/07 15:04:36 ERROR: Unable to fetch feature flags%!(EXTRA )", but I am not sure why.
  5. Not exactly related to IsFeatureEnabled, but if I simply specify a non-empty personal API key when constructing the client, this request succeeds. So is the personal API key actually required?

Thank you for your time! If there are some things you think we can improve I would be happy to try to create a PR.

Unable to go get package

When I run

go get github.com/posthog/posthog-go

I get

go: github.com/posthog/posthog-go: no matching versions for query "upgrade"

Any ideas as to what might be causing this? I look around online but couldn't find anything too helpful

Why does `IsFeatureEnabled` return `interface{}`?

Is there a reason why IsFeatureEnabled returns interface{} and not bool?

Your docs suggest using the library the following way:

isMyFlagEnabled, err := client.IsFeatureEnabled(
            FeatureFlagPayload{
                Key:        "flag-key",
                DistinctId: "distinct_id_of_your_user",
            })

if isMyFlagEnabled {
    // Do something differently for this user
}

With interface{} return value it is not possible.

-if isMyFlagEnabled {
+if isMyFlagEnabled == true {

Could you update the code or the docs?

GetAllFlags result doesn't match IsFeatureEnabled or GetFeatureFlag

I'm trying to use feature flags with groups since I have many anonymous users within an organization for my use case. I created a flag with key test and tried variations where it was on for all, on for just my desired organization, and off for all. The results of GetAllFlags appears to be always correct, while the results for IsFeatureEnabled and GetFeatureFlag are not consistent with GetAllFlags and sometimes wrong.

I'm using the latest SDK.

Code:

func (f *FeatureFlagAccessor) Test(orgID string) {
	flags, err := f.client.GetAllFlags(posthog.FeatureFlagPayloadNoKey{
		DistinctId: "-",
		Groups:     map[string]interface{}{"organization": orgID},
	})
	utils.PrettyPrintStruct(err)
	utils.PrettyPrintStruct(flags)

	singleFlag, err := f.client.GetFeatureFlag(posthog.FeatureFlagPayload{
		Key:        "test",
		DistinctId: "-",
		Groups:     map[string]interface{}{"organization": orgID},
	})
	utils.PrettyPrintStruct(err)
	utils.PrettyPrintStruct(singleFlag)

	isFlagEnabled, err := f.client.IsFeatureEnabled(posthog.FeatureFlagPayload{
		Key:        "test",
		DistinctId: "-",
		Groups:     map[string]interface{}{"organization": orgID},
	})
	utils.PrettyPrintStruct(err)
	utils.PrettyPrintStruct(isFlagEnabled)
}

Feature flag set to enabled with no release conditions:

null
{
	"test": true
}

null
true

null
true

Feature flag turned on for just my orgID:

posthog 2022/12/19 23:33:33 ERROR: Unable to compute flag locally - Can't determine if feature flag is enabled or not with given properties
null
{
	"test": true
}

posthog 2022/12/19 23:33:33 ERROR: Unable to compute flag locally - Can't determine if feature flag is enabled or not with given properties
null
false

posthog 2022/12/19 23:33:33 ERROR: Unable to compute flag locally - Can't determine if feature flag is enabled or not with given properties
null
false

Feature flag set to disabled with no release conditions:

null
{
	"test": false
}

null
false

null
false

CloudQuery Source Plugin?

Hi Team, hopefully this is right place to ask, if not, I'd appreciate if you can direct me.

I'm the founder of cloudquery.io, a high performance open source ELT framework.

Our users are interested in an PostHog plugin, but as we cannot maintain all the plugins ourselves, I was curious if this would be an interesting collaboration, where we would help implement an initial source plugin, and you will help maintain it.

This will give your users the ability to sync PostHog data to any of their datalakes/data-warehouses/databases easily using any of the growing list of CQ destination plugins.

Best,
Yevgeny

Update UUID generation library to Google/uuid

Hi Team,
I noticed whilst integrating posthog-go into something that the library used in config.go for UUID generation is 10/11 years old, and unmaintained.

I've raised a PR to update this to google's uuid library instead.

Many thanks

Impossible to close the client with feature flag poller

Hi! It is impossible to close posthog.Client when feature flags are enabled and no requests to get all flags were made.

What happens:

  1. FeatureFlagsPoller.run starts
  2. Execution is blocked on poller.loaded <- false in fetchNewFeatureFlags because nobody reads from that channel
  3. Infinite loop never starts so <-poller.shutdown is never consumed hence blocking Client.Close method here.
  4. Things work if we GetFeatureFlags at least once. It starts reading poller.loaded channel here

Minimal reproduction example:

package main

import (
	"fmt"
	"os"

	"github.com/posthog/posthog-go"
)

func main() {
	client, err := posthog.NewWithConfig("123", posthog.Config{
		PersonalApiKey: "123",
	})
	if err != nil {
		fmt.Println("could not initialize posthog client", err)
		os.Exit(1)
	}

	// NOTE: Uncomment for things to start working
	// flags, err := client.GetFeatureFlags()
	// fmt.Println("Flags", flags, err)

	fmt.Println("Trying to close the client...")
	if err := client.Close(); err != nil {
		fmt.Println("Failed to close:", err)
	}

	fmt.Println("Client closed.")
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.