Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for disableGeoIP #83

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ type Config struct {
// Information on how to get a personal API key: https://posthog.com/docs/api/overview
PersonalApiKey string

// DisableGeoIP will disable GeoIP lookup for events
// and when fetching feature flags
DisableGeoIP bool

// The flushing interval of the client. Messages will be sent when they've
// been queued up to the maximum batch size or when the flushing interval
// timer triggers.
Expand Down Expand Up @@ -175,5 +179,11 @@ func makeConfig(c Config) Config {
c.maxConcurrentRequests = 1000
}

if c.DisableGeoIP {
if c.DefaultEventProperties == nil {
c.DefaultEventProperties = NewProperties().Set(geoipDisableProperty, true)
}
}

return c
}
5 changes: 5 additions & 0 deletions featureflags.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type FeatureFlagsPoller struct {
mutex sync.RWMutex
nextPollTick func() time.Duration
flagTimeout time.Duration
disableGeoIP bool
}

type FeatureFlag struct {
Expand Down Expand Up @@ -101,6 +102,7 @@ type DecideRequestData struct {
Groups Groups `json:"groups"`
PersonProperties Properties `json:"person_properties"`
GroupProperties map[string]Properties `json:"group_properties"`
DisableGeoIP bool `json:"$geoip_disable"`
}

type DecideResponse struct {
Expand All @@ -125,6 +127,7 @@ func newFeatureFlagsPoller(
pollingInterval time.Duration,
nextPollTick func() time.Duration,
flagTimeout time.Duration,
disableGeoIP bool,
) *FeatureFlagsPoller {

if nextPollTick == nil {
Expand All @@ -143,6 +146,7 @@ func newFeatureFlagsPoller(
mutex: sync.RWMutex{},
nextPollTick: nextPollTick,
flagTimeout: flagTimeout,
disableGeoIP: disableGeoIP,
}

go poller.run()
Expand Down Expand Up @@ -932,6 +936,7 @@ func (poller *FeatureFlagsPoller) getFeatureFlagVariants(distinctId string, grou
Groups: groups,
PersonProperties: personProperties,
GroupProperties: groupProperties,
DisableGeoIP: poller.disableGeoIP,
})
headers := [][2]string{{"Authorization", "Bearer " + poller.personalApiKey + ""}}
if err != nil {
Expand Down
23 changes: 23 additions & 0 deletions fixtures/test-enqueue-capture-with-disable-geoip.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"api_key": "Csyjlnlun3OzyNJAafdlv",
"batch": [
{
"distinct_id": "123456",
"event": "Download",
"library": "posthog-go",
"library_version": "1.0.0",
"properties": {
"$geoip_disable": true,
"$lib": "posthog-go",
"$lib_version": "1.0.0",
"application": "PostHog Go",
"platform": "macos",
"version": "1.0.0"
},
"send_feature_flags": false,
"timestamp": "2009-11-10T23:00:00Z",
"type": "capture",
"uuid": ""
}
]
}
3 changes: 3 additions & 0 deletions posthog.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (
const unimplementedError = "not implemented"
const SIZE_DEFAULT = 50_000

const geoipDisableProperty = "$geoip_disable"

// This interface is the main API exposed by the posthog package.
// Values that satsify this interface are returned by the client constructors
// provided by the package and provide a way to send messages via the HTTP API.
Expand Down Expand Up @@ -134,6 +136,7 @@ func NewWithConfig(apiKey string, config Config) (cli Client, err error) {
c.DefaultFeatureFlagsPollingInterval,
c.NextFeatureFlagsPollingTick,
c.FeatureFlagRequestTimeout,
c.Config.DisableGeoIP,
)
}

Expand Down
52 changes: 39 additions & 13 deletions posthog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,6 @@ func ExampleHistoricalMigrationCapture() {
Endpoint: server.URL,
BatchSize: 1,
now: mockTime,
uid: mockId,
HistoricalMigration: true,
})
defer client.Close()
Expand Down Expand Up @@ -299,7 +298,8 @@ func ExampleHistoricalMigrationCapture() {
// },
// "send_feature_flags": false,
// "timestamp": "2009-11-10T23:00:00Z",
// "type": "capture"
// "type": "capture",
// "uuid": ""
// }
// ],
// "historical_migration": true
Expand All @@ -309,12 +309,14 @@ func ExampleHistoricalMigrationCapture() {

func TestEnqueue(t *testing.T) {
tests := map[string]struct {
ref string
msg Message
ref string
msg Message
disableGeoIP bool
}{
"alias": {
strings.TrimSpace(fixture("test-enqueue-alias.json")),
Alias{Alias: "A", DistinctId: "B"},
false,
},

"identify": {
Expand All @@ -323,6 +325,7 @@ func TestEnqueue(t *testing.T) {
DistinctId: "B",
Properties: Properties{"email": "[email protected]"},
},
false,
},

"groupIdentify": {
Expand All @@ -333,6 +336,7 @@ func TestEnqueue(t *testing.T) {
Key: "id:5",
Properties: Properties{},
},
false,
},

"capture": {
Expand All @@ -347,10 +351,28 @@ func TestEnqueue(t *testing.T) {
},
SendFeatureFlags: false,
},
false,
},

"captureWithDisableGeoIP": {
strings.TrimSpace(fixture("test-enqueue-capture-with-disable-geoip.json")),
Capture{
Event: "Download",
DistinctId: "123456",
Properties: Properties{
"application": "PostHog Go",
"version": "1.0.0",
"platform": "macos", // :)
},
SendFeatureFlags: false,
},
true,
},

"*alias": {
strings.TrimSpace(fixture("test-enqueue-alias.json")),
&Alias{Alias: "A", DistinctId: "B"},
false,
},

"*identify": {
Expand All @@ -359,6 +381,7 @@ func TestEnqueue(t *testing.T) {
DistinctId: "B",
Properties: Properties{"email": "[email protected]"},
},
false,
},

"*groupIdentify": {
Expand All @@ -369,6 +392,7 @@ func TestEnqueue(t *testing.T) {
Key: "id:5",
Properties: Properties{},
},
false,
},

"*capture": {
Expand All @@ -384,22 +408,24 @@ func TestEnqueue(t *testing.T) {
},
SendFeatureFlags: false,
},
false,
},
}

body, server := mockServer()
defer server.Close()

client, _ := NewWithConfig("Csyjlnlun3OzyNJAafdlv", Config{
Endpoint: server.URL,
Verbose: true,
Logger: t,
BatchSize: 1,
now: mockTime,
})
defer client.Close()

for name, test := range tests {
client, _ := NewWithConfig("Csyjlnlun3OzyNJAafdlv", Config{
Endpoint: server.URL,
Verbose: true,
Logger: t,
BatchSize: 1,
now: mockTime,
DisableGeoIP: test.disableGeoIP,
})
defer client.Close()

if err := client.Enqueue(test.msg); err != nil {
t.Error(err)
return
Expand Down