-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathalias.go
More file actions
111 lines (97 loc) · 3.97 KB
/
Copy pathalias.go
File metadata and controls
111 lines (97 loc) · 3.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package posthog
import (
"time"
)
var _ Message = (*Alias)(nil)
// Alias represents an alias call that links another distinct ID to an existing user.
// Enqueue validates that DistinctId and Alias are both set, fills Type, Uuid,
// Timestamp, and DisableGeoIP, then sends the message as a $create_alias event.
type Alias struct {
// Type is reserved for SDK serialization and is overwritten by Enqueue.
// Deprecated: this field is ignored by PostHog ingestion and is kept for backwards compatibility.
Type string
// Uuid is an optional event UUID. If empty, Enqueue generates a random UUID.
// If set, it must be a valid UUID; invalid values are replaced with a generated UUID.
Uuid string
// Alias is the alternate distinct ID to attach to DistinctId.
Alias string
// DistinctId is the existing user distinct ID that Alias should resolve to.
DistinctId string
// Timestamp is the event timestamp. UTC is preferred; non-UTC values are
// converted to the equivalent UTC instant. If zero, Enqueue uses the current time.
Timestamp time.Time
// DisableGeoIP controls whether this alias event disables GeoIP lookup.
// Enqueue overwrites it from Config.GetDisableGeoIP.
DisableGeoIP bool
// IsServer controls whether the event includes the $is_server property.
// Enqueue overwrites it from Config.GetIsServer.
IsServer bool
}
func (msg Alias) internal() {
panic(unimplementedError)
}
// Validate checks that the alias message has both DistinctId and Alias set.
func (msg Alias) Validate() error {
return validateRequiredStringFields("posthog.Alias", requiredStringField{name: "DistinctId", value: msg.DistinctId}, requiredStringField{name: "Alias", value: msg.Alias})
}
// AliasInApiProperties is the wire-format properties object for an Alias message.
type AliasInApiProperties struct {
sysContext
// DistinctId is the canonical user distinct ID.
DistinctId string `json:"distinct_id"`
// Alias is the alternate distinct ID being linked to DistinctId.
Alias string `json:"alias"`
// Lib is the SDK name sent as $lib.
Lib string `json:"$lib"`
// LibVersion is the SDK version sent as $lib_version.
LibVersion string `json:"$lib_version"`
// IsServer marks the event as originating from a server-side SDK.
// Omitted entirely when nil (Config.IsServer resolved to false).
IsServer *bool `json:"$is_server,omitempty"`
// DisableGeoIP is sent as $geoip_disable when GeoIP lookup is disabled.
DisableGeoIP bool `json:"$geoip_disable,omitempty"`
}
// AliasInApi is the wire-format payload produced from an Alias message.
type AliasInApi struct {
// Type is the legacy message type sent to the batch API.
// Deprecated: this field is ignored by PostHog ingestion and is kept for backwards compatibility.
Type string `json:"type"`
// Uuid is the valid event UUID sent to the batch API.
Uuid string `json:"uuid"`
// Library is the SDK name sent to the batch API.
Library string `json:"library"`
// LibraryVersion is the SDK version sent to the batch API.
LibraryVersion string `json:"library_version"`
// Timestamp is the event timestamp sent to the batch API in UTC.
Timestamp time.Time `json:"timestamp"`
// Properties contains alias-specific event properties.
Properties AliasInApiProperties `json:"properties"`
// Event is always $create_alias for Alias messages.
Event string `json:"event"`
}
// APIfy converts an Alias message into the PostHog batch API representation.
func (msg Alias) APIfy() APIMessage {
libraryVersion := getVersion()
var isServer *bool
if msg.IsServer {
isServer = Ptr(true)
}
apified := AliasInApi{
Type: msg.Type,
Uuid: msg.Uuid,
Event: "$create_alias",
Library: SDKName,
LibraryVersion: libraryVersion,
Timestamp: msg.Timestamp.UTC(),
Properties: AliasInApiProperties{
sysContext: getSystemContext(),
DistinctId: msg.DistinctId,
Alias: msg.Alias,
Lib: SDKName,
LibVersion: libraryVersion,
IsServer: isServer,
DisableGeoIP: msg.DisableGeoIP,
},
}
return apified
}