diff --git a/config.go b/config.go index 4f6294f..79fe765 100644 --- a/config.go +++ b/config.go @@ -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). diff --git a/message.go b/message.go index 2edd4ed..58c7225 100644 --- a/message.go +++ b/message.go @@ -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{} diff --git a/posthog.go b/posthog.go index d6ffcfd..ff47f70 100644 --- a/posthog.go +++ b/posthog.go @@ -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 { diff --git a/posthog_test.go b/posthog_test.go index e36e619..d58184b 100644 --- a/posthog_test.go +++ b/posthog_test.go @@ -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