Skip to content

Commit afad34b

Browse files
evgeekevgeny.iva
andauthored
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

Lines changed: 12 additions & 0 deletions
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

Lines changed: 5 additions & 1 deletion
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

Lines changed: 0 additions & 19 deletions
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-
}
Lines changed: 22 additions & 0 deletions
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 6 additions & 3 deletions
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 1 addition & 4 deletions
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

0 commit comments

Comments
 (0)