Skip to content

Commit

Permalink
feat(event): update ua to ga4 analytics
Browse files Browse the repository at this point in the history
Signed-off-by: Abhinandan Purkait <[email protected]>
  • Loading branch information
Abhinandan-Purkait committed Nov 10, 2023
1 parent ecc4269 commit b8c3d30
Show file tree
Hide file tree
Showing 7 changed files with 184 additions and 93 deletions.
66 changes: 38 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,39 +23,49 @@ Create a new `client` and `Send()` an 'event'.
``` go
package main

import (
gaClient "github.com/openebs/go-ogle-analytics/client"
gaEvent "github.com/openebs/go-ogle-analytics/event"
)

func main() {
client, err := gaClient.NewMeasurementClient(
gaClient.WithApiSecret("yourApiSecret"),
gaClient.WithMeasurementId("G-yourMeasurementClient"),
gaClient.WithClientId("uniqueUserId-000000001"),
)
if err != nil {
panic(err)
}
import (
"fmt"

event, err := gaEvent.NewOpenebsEvent(
gaEvent.WithCategory("Foo"),
gaEvent.WithAction("Bar"),
gaEvent.WithLabel("Baz"),
gaEvent.WithValue(19072023),
gaClient "github.com/openebs/go-ogle-analytics/client"
gaEvent "github.com/openebs/go-ogle-analytics/event"
)
if err != nil {
panic(err)
}

err = client.Send(event)
if err != nil {
panic(err)
func main() {
client, err := gaClient.NewMeasurementClient(
gaClient.WithApiSecret("<api-secret>"),
gaClient.WithMeasurementId("<measurement-id>"),
gaClient.WithClientId("<client-id>"),
)
if err != nil {
panic(err)
}

event := gaEvent.NewOpenebsEventBuilder().
Project("OpenEBS").
K8sVersion("v1.25.15").
EngineName("test-engine").
EngineVersion("v1.0.0").
K8sDefaultNsUid("f5d2a546-19ce-407d-99d4-0655d67e2f76").
EngineInstaller("helm").
NodeOs("Ubuntu 20.04.6 LTS").
NodeArch("linux/amd64").
NodeKernelVersion("5.4.0-165-generic").
VolumeName("pvc-b3968e30-9020-4011-943a-7ab338d5f19f").
VolumeClaimName("openebs-lvmpv").
Category("volume-deprovision").
Action("replica:2").
Label("Capacity").
Value("2Gi").
Build()

err = client.Send(event)
if err != nil {
panic(err)
}

fmt.Println("Event fired!")
}

println("Event fired!")
}

```

3. In GA, go to Report > Realtime
Expand Down
4 changes: 4 additions & 0 deletions client/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,7 @@ func WithClientId(clientId string) MeasurementClientOption {
return nil
}
}

func (client *MeasurementClient) SetClientId(clientId string) {
client.clientId = clientId
}
152 changes: 105 additions & 47 deletions event/build.go
Original file line number Diff line number Diff line change
@@ -1,69 +1,127 @@
package event

import (
"github.com/openebs/lib-csi/pkg/common/errors"
)

type OpenebsEventOption func(*OpenebsEvent) error

// OpenebsEvent Hit Type
type OpenebsEvent struct {
Category string `json:"category"`
Action string `json:"action"`
Label string `json:"label"`
Value int64 `json:"value"`
// Specify Project name, ex OpenEBS
Project string `json:"project"`
// K8s Version, ex v1.25.15
K8sVersion string `json:"k8s_version"`
// Name of the engine, ex lvm-localpv
EngineName string `json:"engine_name"`
// Version of the engine, ex lvm-v1.3.0-e927123:11-08-2023-e927123
EngineVersion string `json:"engine_version"`
// Uid of the default k8s ns, ex f5d2a546-19ce-407d-99d4-0655d67e2f76
K8sDefaultNsUid string `json:"k8s_default_ns_uid"`
// Installer of the app, ex lvm-localpv-helm
EngineInstaller string `json:"engine_installer"`
// Machine's os, ex Ubuntu 20.04.6 LTS
NodeOs string `json:"node_os"`
// Machine's kernel version, ex 5.4.0-165-generic
NodeKernelVersion string `json:"node_kernel_version"`
// Machine's arch, ex linux/amd64
NodeArch string `json:"node_arch"`
// Name of the pv object, example `pvc-b3968e30-9020-4011-943a-7ab338d5f19f`
VolumeName string `json:"vol_name"`
// Name of the pvc object, example `openebs-lvmpv`
VolumeClaimName string `json:"vol_claim_name"`
// Category of event, i.e install, volume-provision
Category string `json:"event_category"`
// Action of the event, i.e running, replica:1
Action string `json:"event_action"`
// Label for the event, i.e nodes, capacity
Label string `json:"event_label"`
// Value for the label, i.e 4, 2
Value string `json:"event_value"`
}

// OpenebsEventBuilder is builder for OpenebsEvent
type OpenebsEventBuilder struct {
openebsEvent *OpenebsEvent
}

func NewOpenebsEventBuilder() *OpenebsEventBuilder {
openebsEvent := &OpenebsEvent{}
b := &OpenebsEventBuilder{openebsEvent: openebsEvent}
return b
}

func (b *OpenebsEventBuilder) Project(project string) *OpenebsEventBuilder {
b.openebsEvent.Project = project
return b
}

func (b *OpenebsEventBuilder) K8sVersion(k8sVersion string) *OpenebsEventBuilder {
b.openebsEvent.K8sVersion = k8sVersion
return b
}

func (b *OpenebsEventBuilder) EngineName(engineName string) *OpenebsEventBuilder {
b.openebsEvent.EngineName = engineName
return b
}

func NewOpenebsEvent(opts ...OpenebsEventOption) (*OpenebsEvent, error) {
e := &OpenebsEvent{}
func (b *OpenebsEventBuilder) EngineVersion(engineVersion string) *OpenebsEventBuilder {
b.openebsEvent.EngineVersion = engineVersion
return b
}

func (b *OpenebsEventBuilder) K8sDefaultNsUid(k8sDefaultNsUid string) *OpenebsEventBuilder {
b.openebsEvent.K8sDefaultNsUid = k8sDefaultNsUid
return b
}

var err error
for _, opt := range opts {
err = opt(e)
if err != nil {
return nil, errors.Wrap(err, "failed to build OpenebsEvent")
}
}
func (b *OpenebsEventBuilder) EngineInstaller(engineInstaller string) *OpenebsEventBuilder {
b.openebsEvent.EngineInstaller = engineInstaller
return b
}

return e, nil
func (b *OpenebsEventBuilder) NodeOs(nodeOs string) *OpenebsEventBuilder {
b.openebsEvent.NodeOs = nodeOs
return b
}

func WithCategory(category string) OpenebsEventOption {
return func(e *OpenebsEvent) error {
if len(category) == 0 {
return errors.Errorf("failed to set OpenebsEvent category: category is an empty string")
}
func (b *OpenebsEventBuilder) NodeKernelVersion(nodeKernelVersion string) *OpenebsEventBuilder {
b.openebsEvent.NodeKernelVersion = nodeKernelVersion
return b
}

e.Category = category
return nil
}
func (b *OpenebsEventBuilder) NodeArch(nodeArch string) *OpenebsEventBuilder {
b.openebsEvent.NodeArch = nodeArch
return b
}

func WithAction(action string) OpenebsEventOption {
return func(e *OpenebsEvent) error {
if len(action) == 0 {
return errors.Errorf("failed to set OpenebsEvent action: action is an empty string")
}
func (b *OpenebsEventBuilder) VolumeName(volumeName string) *OpenebsEventBuilder {
b.openebsEvent.VolumeName = volumeName
return b
}

e.Action = action
return nil
}
func (b *OpenebsEventBuilder) VolumeClaimName(volumeClaimName string) *OpenebsEventBuilder {
b.openebsEvent.VolumeClaimName = volumeClaimName
return b
}

func WithLabel(label string) OpenebsEventOption {
return func(e *OpenebsEvent) error {
if len(label) == 0 {
return errors.Errorf("failed to set OpenebsEvent label: label is an empty string")
}
func (b *OpenebsEventBuilder) Category(category string) *OpenebsEventBuilder {
b.openebsEvent.Category = category
return b
}

func (b *OpenebsEventBuilder) Action(action string) *OpenebsEventBuilder {
b.openebsEvent.Action = action
return b
}

func (b *OpenebsEventBuilder) Label(label string) *OpenebsEventBuilder {
b.openebsEvent.Label = label
return b
}

e.Label = label
return nil
}
func (b *OpenebsEventBuilder) Value(value string) *OpenebsEventBuilder {
b.openebsEvent.Value = value
return b
}

func WithValue(value int64) OpenebsEventOption {
return func(e *OpenebsEvent) error {
e.Value = value
return nil
}
func (b *OpenebsEventBuilder) Build() *OpenebsEvent {
return b.openebsEvent
}
4 changes: 2 additions & 2 deletions event/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ func (e *OpenebsEvent) CategoryStr() string {
return e.Category
}

func (e *OpenebsEvent) ActionStr() string {
return e.Action
func (e *OpenebsEvent) EngineNameStr() string {
return e.EngineName
}
36 changes: 23 additions & 13 deletions example/main.go
Original file line number Diff line number Diff line change
@@ -1,34 +1,44 @@
package main

import (
"fmt"

gaClient "github.com/openebs/go-ogle-analytics/client"
gaEvent "github.com/openebs/go-ogle-analytics/event"
)

func main() {
client, err := gaClient.NewMeasurementClient(
gaClient.WithApiSecret("NguBiGh6QeOdeG3zJswggQ"),
gaClient.WithMeasurementId("G-TZGP46618W"),
gaClient.WithClientId("uniqueUserId-000000001"),
gaClient.WithApiSecret("<api-secret>"),
gaClient.WithMeasurementId("<measurement-id>"),
gaClient.WithClientId("<client-id>"),
)
if err != nil {
panic(err)
}

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

err = client.Send(event)
if err != nil {
panic(err)
}

println("Event fired!")
fmt.Println("Event fired!")
}
5 changes: 2 additions & 3 deletions payload/build.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package payload

import (
"github.com/openebs/lib-csi/pkg/common/errors"

"github.com/openebs/go-ogle-analytics/event"
"github.com/openebs/lib-csi/pkg/common/errors"
)

type PayloadOption func(*Payload) error
Expand Down Expand Up @@ -46,7 +45,7 @@ func WithClientId(clientId string) PayloadOption {
func WithOpenebsEvent(event *event.OpenebsEvent) PayloadOption {
return func(p *Payload) error {
p.Events = append(p.Events, ApiEvent{
Name: event.CategoryStr() + "_" + event.ActionStr(),
Name: NormalizedEventName(event.EngineNameStr() + "-" + event.CategoryStr()),
Params: *event,
})
return nil
Expand Down
10 changes: 10 additions & 0 deletions payload/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package payload

import (
"strings"
)

// Replace the `-` with `_` from the name
func NormalizedEventName(name string) string {
return strings.ReplaceAll(name, "-", "_")
}

0 comments on commit b8c3d30

Please sign in to comment.