Skip to content

Commit afad34b

Browse files
evgeekevgeny.iva
and
evgeny.iva
authored
RE: Ability to specify uuid when capturing events (#72)
* specify uuid * tidy * fix * rename package * rollback * rollback all * init --------- Co-authored-by: evgeny.iva <[email protected]>
1 parent e4ff93a commit afad34b

12 files changed

+60
-46
lines changed

README.md

+12
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,18 @@ func main() {
6262
Properties: posthog.NewProperties().
6363
Set("$current_url", "https://example.com"),
6464
})
65+
66+
// Capture event with calculated uuid to deduplicate repeated events.
67+
// The library github.com/google/uuid is used
68+
key := myEvent.Id + myEvent.Project
69+
uid := uuid.NewSHA1(uuid.NameSpaceX500, []byte(key)).String()
70+
client.Enqueue(posthog.Capture{
71+
Uuid: uid,
72+
DistinctId: "test-user",
73+
Event: "$pageview",
74+
Properties: posthog.NewProperties().
75+
Set("$current_url", "https://example.com"),
76+
})
6577

6678
// Check if a feature flag is enabled
6779
isMyFlagEnabled, err := client.IsFeatureEnabled(

capture.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ type Capture struct {
99
// This field is exported for serialization purposes and shouldn't be set by
1010
// the application, its value is always overwritten by the library.
1111
Type string
12-
12+
// You don't usually need to specify this field - Posthog will generate it automatically.
13+
// Use it only when necessary - for example, to prevent duplicate events.
14+
Uuid string
1315
DistinctId string
1416
Event string
1517
Timestamp time.Time
@@ -44,6 +46,7 @@ func (msg Capture) Validate() error {
4446

4547
type CaptureInApi struct {
4648
Type string `json:"type"`
49+
Uuid string `json:"uuid"`
4750
Library string `json:"library"`
4851
LibraryVersion string `json:"library_version"`
4952
Timestamp time.Time `json:"timestamp"`
@@ -72,6 +75,7 @@ func (msg Capture) APIfy() APIMessage {
7275

7376
apified := CaptureInApi{
7477
Type: msg.Type,
78+
Uuid: msg.Uuid,
7579
Library: library,
7680
LibraryVersion: libraryVersion,
7781
Timestamp: msg.Timestamp,

config.go

-19
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ package posthog
33
import (
44
"net/http"
55
"time"
6-
7-
"github.com/google/uuid"
86
)
97

108
// Instances of this type carry the different configuration options that may
@@ -79,12 +77,6 @@ type Config struct {
7977
// If not set the client will fallback to use a default retry policy.
8078
RetryAfter func(int) time.Duration
8179

82-
// A function called by the client to generate unique message identifiers.
83-
// The client uses a UUID generator if none is provided.
84-
// This field is not exported and only exposed internally to let unit tests
85-
// mock the id generation.
86-
uid func() string
87-
8880
// A function called by the client to get the current time, `time.Now` is
8981
// used by default.
9082
// This field is not exported and only exposed internally to let unit tests
@@ -173,10 +165,6 @@ func makeConfig(c Config) Config {
173165
c.RetryAfter = DefaultBacko().Duration
174166
}
175167

176-
if c.uid == nil {
177-
c.uid = uid
178-
}
179-
180168
if c.now == nil {
181169
c.now = time.Now
182170
}
@@ -187,10 +175,3 @@ func makeConfig(c Config) Config {
187175

188176
return c
189177
}
190-
191-
// This function returns a string representation of a UUID, it's the default
192-
// function used for generating unique IDs.
193-
func uid() string {
194-
new_uuid, _ := uuid.NewRandom()
195-
return new_uuid.String()
196-
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"api_key": "Csyjlnlun3OzyNJAafdlv",
3+
"batch": [
4+
{
5+
"distinct_id": "123456",
6+
"event": "Download",
7+
"library": "posthog-go",
8+
"library_version": "1.0.0",
9+
"properties": {
10+
"$lib": "posthog-go",
11+
"$lib_version": "1.0.0",
12+
"application": "PostHog Go",
13+
"platform": "macos",
14+
"version": "1.0.0"
15+
},
16+
"send_feature_flags": false,
17+
"timestamp": "2009-11-10T23:00:00Z",
18+
"type": "capture",
19+
"uuid": "11111111-1111-1111-1111-111111111111"
20+
}
21+
]
22+
}

fixtures/test-enqueue-capture.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
},
1616
"send_feature_flags": false,
1717
"timestamp": "2009-11-10T23:00:00Z",
18-
"type": "capture"
18+
"type": "capture",
19+
"uuid": ""
1920
}
2021
]
2122
}

fixtures/test-interval-capture.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
},
1616
"send_feature_flags": false,
1717
"timestamp": "2009-11-10T23:00:00Z",
18-
"type": "capture"
18+
"type": "capture",
19+
"uuid": ""
1920
}
2021
]
2122
}

fixtures/test-many-capture.json

+6-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
},
1515
"send_feature_flags": false,
1616
"timestamp": "2009-11-10T23:00:00Z",
17-
"type": "capture"
17+
"type": "capture",
18+
"uuid": ""
1819
},
1920
{
2021
"distinct_id": "123456",
@@ -29,7 +30,8 @@
2930
},
3031
"send_feature_flags": false,
3132
"timestamp": "2009-11-10T23:00:00Z",
32-
"type": "capture"
33+
"type": "capture",
34+
"uuid": ""
3335
},
3436
{
3537
"distinct_id": "123456",
@@ -44,7 +46,8 @@
4446
},
4547
"send_feature_flags": false,
4648
"timestamp": "2009-11-10T23:00:00Z",
47-
"type": "capture"
49+
"type": "capture",
50+
"uuid": ""
4851
}
4952
]
5053
}

fixtures/test-merge-capture.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
},
1717
"send_feature_flags": false,
1818
"timestamp": "2015-07-10T23:00:00Z",
19-
"type": "capture"
19+
"type": "capture",
20+
"uuid": ""
2021
}
2122
]
2223
}

fixtures/test-timestamp-capture.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
},
1616
"send_feature_flags": false,
1717
"timestamp": "2015-07-10T23:00:00Z",
18-
"type": "capture"
18+
"type": "capture",
19+
"uuid": ""
1920
}
2021
]
2122
}

go.mod

+1-4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@ module github.com/posthog/posthog-go
22

33
go 1.18
44

5-
require (
6-
github.com/google/uuid v1.3.0
7-
github.com/urfave/cli v1.22.5
8-
)
5+
require github.com/urfave/cli v1.22.5
96

107
require (
118
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d // indirect

go.sum

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
22
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSYgptZMwQh2aRr3LuazLJIa+Pg3Kc1ylSYVY=
33
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
4-
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
5-
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
64
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
75
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
86
github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q=
@@ -12,4 +10,4 @@ github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeV
1210
github.com/urfave/cli v1.22.5 h1:lNq9sAHXK2qfdI8W+GRItjCEkI+2oR4d+MEHy1CKXoU=
1311
github.com/urfave/cli v1.22.5/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
1412
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
15-
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
13+
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

posthog_test.go

+5-12
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,6 @@ func fixture(name string) string {
152152
return string(b)
153153
}
154154

155-
func mockId() string { return "I'm unique" }
156-
157155
func mockTime() time.Time {
158156
// time.Unix(0, 0) fails on Circle
159157
return time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)
@@ -191,11 +189,11 @@ func ExampleCapture() {
191189
Endpoint: server.URL,
192190
BatchSize: 1,
193191
now: mockTime,
194-
uid: mockId,
195192
})
196193
defer client.Close()
197194

198195
client.Enqueue(Capture{
196+
Uuid: "00000000-0000-0000-0000-000000000000",
199197
Event: "Download",
200198
DistinctId: "123456",
201199
Properties: Properties{
@@ -225,7 +223,8 @@ func ExampleCapture() {
225223
// },
226224
// "send_feature_flags": false,
227225
// "timestamp": "2009-11-10T23:00:00Z",
228-
// "type": "capture"
226+
// "type": "capture",
227+
// "uuid": "00000000-0000-0000-0000-000000000000"
229228
// }
230229
// ]
231230
// }
@@ -246,7 +245,6 @@ func TestCaptureNoProperties(t *testing.T) {
246245
Endpoint: server.URL,
247246
BatchSize: 1,
248247
now: mockTime,
249-
uid: mockId,
250248
DefaultEventProperties: NewProperties().Set("service", "api"),
251249
})
252250
defer client.Close()
@@ -323,8 +321,9 @@ func TestEnqueue(t *testing.T) {
323321
},
324322

325323
"*capture": {
326-
strings.TrimSpace(fixture("test-enqueue-capture.json")),
324+
strings.TrimSpace(fixture("test-enqueue-capture-with-uuid.json")),
327325
&Capture{
326+
Uuid: "11111111-1111-1111-1111-111111111111",
328327
Event: "Download",
329328
DistinctId: "123456",
330329
Properties: Properties{
@@ -346,7 +345,6 @@ func TestEnqueue(t *testing.T) {
346345
Logger: t,
347346
BatchSize: 1,
348347
now: mockTime,
349-
uid: mockId,
350348
})
351349
defer client.Close()
352350

@@ -404,7 +402,6 @@ func TestCaptureWithInterval(t *testing.T) {
404402
Verbose: true,
405403
Logger: t,
406404
now: mockTime,
407-
uid: mockId,
408405
})
409406
defer client.Close()
410407

@@ -441,7 +438,6 @@ func TestCaptureWithTimestamp(t *testing.T) {
441438
Logger: t,
442439
BatchSize: 1,
443440
now: mockTime,
444-
uid: mockId,
445441
})
446442
defer client.Close()
447443

@@ -475,7 +471,6 @@ func TestCaptureWithDefaultProperties(t *testing.T) {
475471
Logger: t,
476472
BatchSize: 1,
477473
now: mockTime,
478-
uid: mockId,
479474
})
480475
defer client.Close()
481476

@@ -508,7 +503,6 @@ func TestCaptureMany(t *testing.T) {
508503
Logger: t,
509504
BatchSize: 3,
510505
now: mockTime,
511-
uid: mockId,
512506
})
513507
defer client.Close()
514508

@@ -1711,7 +1705,6 @@ func TestCaptureSendFlags(t *testing.T) {
17111705
Logger: t,
17121706
BatchSize: 1,
17131707
now: mockTime,
1714-
uid: mockId,
17151708

17161709
PersonalApiKey: "some very secret key",
17171710
})

0 commit comments

Comments
 (0)