Skip to content

Commit 91cecc3

Browse files
authored
Add historical_migration flag to config (#40)
Add historical migration flag
1 parent 60d37b0 commit 91cecc3

File tree

4 files changed

+61
-4
lines changed

4 files changed

+61
-4
lines changed

config.go

+4
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ type Config struct {
3838
// will override DefaultFeatureFlagsPollingInterval.
3939
NextFeatureFlagsPollingTick func() time.Duration
4040

41+
// Flag to enable historical migration
42+
// See more in our migration docs: https://posthog.com/docs/migrate
43+
HistoricalMigration bool
44+
4145
// The HTTP transport used by the client, this allows an application to
4246
// redefine how requests are being sent at the HTTP level (for example,
4347
// to change the connection pooling policy).

message.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,9 @@ func makeTimestamp(t time.Time, def time.Time) time.Time {
5555
// export this type because it's only meant to be used internally to send groups
5656
// of messages in one API call.
5757
type batch struct {
58-
ApiKey string `json:"api_key"`
59-
Messages []message `json:"batch"`
58+
ApiKey string `json:"api_key"`
59+
HistoricalMigration bool `json:"historical_migration,omitempty"`
60+
Messages []message `json:"batch"`
6061
}
6162

6263
type APIMessage interface{}

posthog.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -430,8 +430,9 @@ func (c *client) send(msgs []message) {
430430
const attempts = 10
431431

432432
b, err := json.Marshal(batch{
433-
ApiKey: c.key,
434-
Messages: msgs,
433+
ApiKey: c.key,
434+
HistoricalMigration: c.HistoricalMigration,
435+
Messages: msgs,
435436
})
436437

437438
if err != nil {

posthog_test.go

+51
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,57 @@ func TestCaptureNoProperties(t *testing.T) {
256256
})
257257
}
258258

259+
func ExampleHistoricalMigrationCapture() {
260+
body, server := mockServer()
261+
defer server.Close()
262+
263+
client, _ := NewWithConfig("Csyjlnlun3OzyNJAafdlv", Config{
264+
Endpoint: server.URL,
265+
BatchSize: 1,
266+
now: mockTime,
267+
uid: mockId,
268+
HistoricalMigration: true,
269+
})
270+
defer client.Close()
271+
272+
client.Enqueue(Capture{
273+
Event: "Download",
274+
DistinctId: "123456",
275+
Properties: Properties{
276+
"application": "PostHog Go",
277+
"version": "1.0.0",
278+
"platform": "macos", // :)
279+
},
280+
SendFeatureFlags: false,
281+
})
282+
283+
fmt.Printf("%s\n", <-body)
284+
// Output:
285+
// {
286+
// "api_key": "Csyjlnlun3OzyNJAafdlv",
287+
// "batch": [
288+
// {
289+
// "distinct_id": "123456",
290+
// "event": "Download",
291+
// "library": "posthog-go",
292+
// "library_version": "1.0.0",
293+
// "properties": {
294+
// "$lib": "posthog-go",
295+
// "$lib_version": "1.0.0",
296+
// "application": "PostHog Go",
297+
// "platform": "macos",
298+
// "version": "1.0.0"
299+
// },
300+
// "send_feature_flags": false,
301+
// "timestamp": "2009-11-10T23:00:00Z",
302+
// "type": "capture"
303+
// }
304+
// ],
305+
// "historical_migration": true
306+
// }
307+
308+
}
309+
259310
func TestEnqueue(t *testing.T) {
260311
tests := map[string]struct {
261312
ref string

0 commit comments

Comments
 (0)