Skip to content

Commit b8c3d30

Browse files
feat(event): update ua to ga4 analytics
Signed-off-by: Abhinandan Purkait <[email protected]>
1 parent ecc4269 commit b8c3d30

File tree

7 files changed

+184
-93
lines changed

7 files changed

+184
-93
lines changed

README.md

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -23,39 +23,49 @@ Create a new `client` and `Send()` an 'event'.
2323
``` go
2424
package main
2525

26-
import (
27-
gaClient "github.com/openebs/go-ogle-analytics/client"
28-
gaEvent "github.com/openebs/go-ogle-analytics/event"
29-
)
30-
31-
func main() {
32-
client, err := gaClient.NewMeasurementClient(
33-
gaClient.WithApiSecret("yourApiSecret"),
34-
gaClient.WithMeasurementId("G-yourMeasurementClient"),
35-
gaClient.WithClientId("uniqueUserId-000000001"),
36-
)
37-
if err != nil {
38-
panic(err)
39-
}
26+
import (
27+
"fmt"
4028

41-
event, err := gaEvent.NewOpenebsEvent(
42-
gaEvent.WithCategory("Foo"),
43-
gaEvent.WithAction("Bar"),
44-
gaEvent.WithLabel("Baz"),
45-
gaEvent.WithValue(19072023),
29+
gaClient "github.com/openebs/go-ogle-analytics/client"
30+
gaEvent "github.com/openebs/go-ogle-analytics/event"
4631
)
47-
if err != nil {
48-
panic(err)
49-
}
5032

51-
err = client.Send(event)
52-
if err != nil {
53-
panic(err)
33+
func main() {
34+
client, err := gaClient.NewMeasurementClient(
35+
gaClient.WithApiSecret("<api-secret>"),
36+
gaClient.WithMeasurementId("<measurement-id>"),
37+
gaClient.WithClientId("<client-id>"),
38+
)
39+
if err != nil {
40+
panic(err)
41+
}
42+
43+
event := gaEvent.NewOpenebsEventBuilder().
44+
Project("OpenEBS").
45+
K8sVersion("v1.25.15").
46+
EngineName("test-engine").
47+
EngineVersion("v1.0.0").
48+
K8sDefaultNsUid("f5d2a546-19ce-407d-99d4-0655d67e2f76").
49+
EngineInstaller("helm").
50+
NodeOs("Ubuntu 20.04.6 LTS").
51+
NodeArch("linux/amd64").
52+
NodeKernelVersion("5.4.0-165-generic").
53+
VolumeName("pvc-b3968e30-9020-4011-943a-7ab338d5f19f").
54+
VolumeClaimName("openebs-lvmpv").
55+
Category("volume-deprovision").
56+
Action("replica:2").
57+
Label("Capacity").
58+
Value("2Gi").
59+
Build()
60+
61+
err = client.Send(event)
62+
if err != nil {
63+
panic(err)
64+
}
65+
66+
fmt.Println("Event fired!")
5467
}
5568

56-
println("Event fired!")
57-
}
58-
5969
```
6070

6171
3. In GA, go to Report > Realtime

client/build.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,7 @@ func WithClientId(clientId string) MeasurementClientOption {
8585
return nil
8686
}
8787
}
88+
89+
func (client *MeasurementClient) SetClientId(clientId string) {
90+
client.clientId = clientId
91+
}

event/build.go

Lines changed: 105 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,127 @@
11
package event
22

3-
import (
4-
"github.com/openebs/lib-csi/pkg/common/errors"
5-
)
6-
73
type OpenebsEventOption func(*OpenebsEvent) error
84

95
// OpenebsEvent Hit Type
106
type OpenebsEvent struct {
11-
Category string `json:"category"`
12-
Action string `json:"action"`
13-
Label string `json:"label"`
14-
Value int64 `json:"value"`
7+
// Specify Project name, ex OpenEBS
8+
Project string `json:"project"`
9+
// K8s Version, ex v1.25.15
10+
K8sVersion string `json:"k8s_version"`
11+
// Name of the engine, ex lvm-localpv
12+
EngineName string `json:"engine_name"`
13+
// Version of the engine, ex lvm-v1.3.0-e927123:11-08-2023-e927123
14+
EngineVersion string `json:"engine_version"`
15+
// Uid of the default k8s ns, ex f5d2a546-19ce-407d-99d4-0655d67e2f76
16+
K8sDefaultNsUid string `json:"k8s_default_ns_uid"`
17+
// Installer of the app, ex lvm-localpv-helm
18+
EngineInstaller string `json:"engine_installer"`
19+
// Machine's os, ex Ubuntu 20.04.6 LTS
20+
NodeOs string `json:"node_os"`
21+
// Machine's kernel version, ex 5.4.0-165-generic
22+
NodeKernelVersion string `json:"node_kernel_version"`
23+
// Machine's arch, ex linux/amd64
24+
NodeArch string `json:"node_arch"`
25+
// Name of the pv object, example `pvc-b3968e30-9020-4011-943a-7ab338d5f19f`
26+
VolumeName string `json:"vol_name"`
27+
// Name of the pvc object, example `openebs-lvmpv`
28+
VolumeClaimName string `json:"vol_claim_name"`
29+
// Category of event, i.e install, volume-provision
30+
Category string `json:"event_category"`
31+
// Action of the event, i.e running, replica:1
32+
Action string `json:"event_action"`
33+
// Label for the event, i.e nodes, capacity
34+
Label string `json:"event_label"`
35+
// Value for the label, i.e 4, 2
36+
Value string `json:"event_value"`
37+
}
38+
39+
// OpenebsEventBuilder is builder for OpenebsEvent
40+
type OpenebsEventBuilder struct {
41+
openebsEvent *OpenebsEvent
42+
}
43+
44+
func NewOpenebsEventBuilder() *OpenebsEventBuilder {
45+
openebsEvent := &OpenebsEvent{}
46+
b := &OpenebsEventBuilder{openebsEvent: openebsEvent}
47+
return b
48+
}
49+
50+
func (b *OpenebsEventBuilder) Project(project string) *OpenebsEventBuilder {
51+
b.openebsEvent.Project = project
52+
return b
53+
}
54+
55+
func (b *OpenebsEventBuilder) K8sVersion(k8sVersion string) *OpenebsEventBuilder {
56+
b.openebsEvent.K8sVersion = k8sVersion
57+
return b
58+
}
59+
60+
func (b *OpenebsEventBuilder) EngineName(engineName string) *OpenebsEventBuilder {
61+
b.openebsEvent.EngineName = engineName
62+
return b
1563
}
1664

17-
func NewOpenebsEvent(opts ...OpenebsEventOption) (*OpenebsEvent, error) {
18-
e := &OpenebsEvent{}
65+
func (b *OpenebsEventBuilder) EngineVersion(engineVersion string) *OpenebsEventBuilder {
66+
b.openebsEvent.EngineVersion = engineVersion
67+
return b
68+
}
69+
70+
func (b *OpenebsEventBuilder) K8sDefaultNsUid(k8sDefaultNsUid string) *OpenebsEventBuilder {
71+
b.openebsEvent.K8sDefaultNsUid = k8sDefaultNsUid
72+
return b
73+
}
1974

20-
var err error
21-
for _, opt := range opts {
22-
err = opt(e)
23-
if err != nil {
24-
return nil, errors.Wrap(err, "failed to build OpenebsEvent")
25-
}
26-
}
75+
func (b *OpenebsEventBuilder) EngineInstaller(engineInstaller string) *OpenebsEventBuilder {
76+
b.openebsEvent.EngineInstaller = engineInstaller
77+
return b
78+
}
2779

28-
return e, nil
80+
func (b *OpenebsEventBuilder) NodeOs(nodeOs string) *OpenebsEventBuilder {
81+
b.openebsEvent.NodeOs = nodeOs
82+
return b
2983
}
3084

31-
func WithCategory(category string) OpenebsEventOption {
32-
return func(e *OpenebsEvent) error {
33-
if len(category) == 0 {
34-
return errors.Errorf("failed to set OpenebsEvent category: category is an empty string")
35-
}
85+
func (b *OpenebsEventBuilder) NodeKernelVersion(nodeKernelVersion string) *OpenebsEventBuilder {
86+
b.openebsEvent.NodeKernelVersion = nodeKernelVersion
87+
return b
88+
}
3689

37-
e.Category = category
38-
return nil
39-
}
90+
func (b *OpenebsEventBuilder) NodeArch(nodeArch string) *OpenebsEventBuilder {
91+
b.openebsEvent.NodeArch = nodeArch
92+
return b
4093
}
4194

42-
func WithAction(action string) OpenebsEventOption {
43-
return func(e *OpenebsEvent) error {
44-
if len(action) == 0 {
45-
return errors.Errorf("failed to set OpenebsEvent action: action is an empty string")
46-
}
95+
func (b *OpenebsEventBuilder) VolumeName(volumeName string) *OpenebsEventBuilder {
96+
b.openebsEvent.VolumeName = volumeName
97+
return b
98+
}
4799

48-
e.Action = action
49-
return nil
50-
}
100+
func (b *OpenebsEventBuilder) VolumeClaimName(volumeClaimName string) *OpenebsEventBuilder {
101+
b.openebsEvent.VolumeClaimName = volumeClaimName
102+
return b
51103
}
52104

53-
func WithLabel(label string) OpenebsEventOption {
54-
return func(e *OpenebsEvent) error {
55-
if len(label) == 0 {
56-
return errors.Errorf("failed to set OpenebsEvent label: label is an empty string")
57-
}
105+
func (b *OpenebsEventBuilder) Category(category string) *OpenebsEventBuilder {
106+
b.openebsEvent.Category = category
107+
return b
108+
}
109+
110+
func (b *OpenebsEventBuilder) Action(action string) *OpenebsEventBuilder {
111+
b.openebsEvent.Action = action
112+
return b
113+
}
114+
115+
func (b *OpenebsEventBuilder) Label(label string) *OpenebsEventBuilder {
116+
b.openebsEvent.Label = label
117+
return b
118+
}
58119

59-
e.Label = label
60-
return nil
61-
}
120+
func (b *OpenebsEventBuilder) Value(value string) *OpenebsEventBuilder {
121+
b.openebsEvent.Value = value
122+
return b
62123
}
63124

64-
func WithValue(value int64) OpenebsEventOption {
65-
return func(e *OpenebsEvent) error {
66-
e.Value = value
67-
return nil
68-
}
125+
func (b *OpenebsEventBuilder) Build() *OpenebsEvent {
126+
return b.openebsEvent
69127
}

event/event.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ func (e *OpenebsEvent) CategoryStr() string {
44
return e.Category
55
}
66

7-
func (e *OpenebsEvent) ActionStr() string {
8-
return e.Action
7+
func (e *OpenebsEvent) EngineNameStr() string {
8+
return e.EngineName
99
}

example/main.go

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,44 @@
11
package main
22

33
import (
4+
"fmt"
5+
46
gaClient "github.com/openebs/go-ogle-analytics/client"
57
gaEvent "github.com/openebs/go-ogle-analytics/event"
68
)
79

810
func main() {
911
client, err := gaClient.NewMeasurementClient(
10-
gaClient.WithApiSecret("NguBiGh6QeOdeG3zJswggQ"),
11-
gaClient.WithMeasurementId("G-TZGP46618W"),
12-
gaClient.WithClientId("uniqueUserId-000000001"),
12+
gaClient.WithApiSecret("<api-secret>"),
13+
gaClient.WithMeasurementId("<measurement-id>"),
14+
gaClient.WithClientId("<client-id>"),
1315
)
1416
if err != nil {
1517
panic(err)
1618
}
1719

18-
event, err := gaEvent.NewOpenebsEvent(
19-
gaEvent.WithCategory("Foo"),
20-
gaEvent.WithAction("Bar"),
21-
gaEvent.WithLabel("Baz"),
22-
gaEvent.WithValue(19072023),
23-
)
24-
if err != nil {
25-
panic(err)
26-
}
20+
event := gaEvent.NewOpenebsEventBuilder().
21+
Project("OpenEBS").
22+
K8sVersion("v1.25.15").
23+
EngineName("test-engine").
24+
EngineVersion("v1.0.0").
25+
K8sDefaultNsUid("f5d2a546-19ce-407d-99d4-0655d67e2f76").
26+
EngineInstaller("helm").
27+
NodeOs("Ubuntu 20.04.6 LTS").
28+
NodeArch("linux/amd64").
29+
NodeKernelVersion("5.4.0-165-generic").
30+
VolumeName("pvc-b3968e30-9020-4011-943a-7ab338d5f19f").
31+
VolumeClaimName("openebs-lvmpv").
32+
Category("volume-deprovision").
33+
Action("replica:2").
34+
Label("Capacity").
35+
Value("2Gi").
36+
Build()
2737

2838
err = client.Send(event)
2939
if err != nil {
3040
panic(err)
3141
}
3242

33-
println("Event fired!")
43+
fmt.Println("Event fired!")
3444
}

payload/build.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
package payload
22

33
import (
4-
"github.com/openebs/lib-csi/pkg/common/errors"
5-
64
"github.com/openebs/go-ogle-analytics/event"
5+
"github.com/openebs/lib-csi/pkg/common/errors"
76
)
87

98
type PayloadOption func(*Payload) error
@@ -46,7 +45,7 @@ func WithClientId(clientId string) PayloadOption {
4645
func WithOpenebsEvent(event *event.OpenebsEvent) PayloadOption {
4746
return func(p *Payload) error {
4847
p.Events = append(p.Events, ApiEvent{
49-
Name: event.CategoryStr() + "_" + event.ActionStr(),
48+
Name: NormalizedEventName(event.EngineNameStr() + "-" + event.CategoryStr()),
5049
Params: *event,
5150
})
5251
return nil

payload/utils.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package payload
2+
3+
import (
4+
"strings"
5+
)
6+
7+
// Replace the `-` with `_` from the name
8+
func NormalizedEventName(name string) string {
9+
return strings.ReplaceAll(name, "-", "_")
10+
}

0 commit comments

Comments
 (0)