Skip to content

Commit

Permalink
Add historical_migration flag to config (#40)
Browse files Browse the repository at this point in the history
Add historical migration flag
  • Loading branch information
ivanagas committed Sep 16, 2024
1 parent 60d37b0 commit 91cecc3
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 4 deletions.
4 changes: 4 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ type Config struct {
// will override DefaultFeatureFlagsPollingInterval.
NextFeatureFlagsPollingTick func() time.Duration

// Flag to enable historical migration
// See more in our migration docs: https://posthog.com/docs/migrate
HistoricalMigration bool

// The HTTP transport used by the client, this allows an application to
// redefine how requests are being sent at the HTTP level (for example,
// to change the connection pooling policy).
Expand Down
5 changes: 3 additions & 2 deletions message.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ func makeTimestamp(t time.Time, def time.Time) time.Time {
// export this type because it's only meant to be used internally to send groups
// of messages in one API call.
type batch struct {
ApiKey string `json:"api_key"`
Messages []message `json:"batch"`
ApiKey string `json:"api_key"`
HistoricalMigration bool `json:"historical_migration,omitempty"`
Messages []message `json:"batch"`
}

type APIMessage interface{}
Expand Down
5 changes: 3 additions & 2 deletions posthog.go
Original file line number Diff line number Diff line change
Expand Up @@ -430,8 +430,9 @@ func (c *client) send(msgs []message) {
const attempts = 10

b, err := json.Marshal(batch{
ApiKey: c.key,
Messages: msgs,
ApiKey: c.key,
HistoricalMigration: c.HistoricalMigration,
Messages: msgs,
})

if err != nil {
Expand Down
51 changes: 51 additions & 0 deletions posthog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,57 @@ func TestCaptureNoProperties(t *testing.T) {
})
}

func ExampleHistoricalMigrationCapture() {
body, server := mockServer()
defer server.Close()

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

client.Enqueue(Capture{
Event: "Download",
DistinctId: "123456",
Properties: Properties{
"application": "PostHog Go",
"version": "1.0.0",
"platform": "macos", // :)
},
SendFeatureFlags: false,
})

fmt.Printf("%s\n", <-body)
// Output:
// {
// "api_key": "Csyjlnlun3OzyNJAafdlv",
// "batch": [
// {
// "distinct_id": "123456",
// "event": "Download",
// "library": "posthog-go",
// "library_version": "1.0.0",
// "properties": {
// "$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"
// }
// ],
// "historical_migration": true
// }

}

func TestEnqueue(t *testing.T) {
tests := map[string]struct {
ref string
Expand Down

0 comments on commit 91cecc3

Please sign in to comment.