forked from jpillora/go-ogle-analytics
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(event): update ua to ga4 analytics
Signed-off-by: Abhinandan Purkait <[email protected]>
- Loading branch information
1 parent
ecc4269
commit 8c1ad47
Showing
6 changed files
with
142 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,122 @@ | ||
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"` | ||
// Our Application Org, example OpenEBS | ||
Project string `json:"project"` | ||
// K8s Version, example v1.25.15 | ||
K8sVersion string `json:"k8s_version"` | ||
// Name of the engine,example lvm-localpv | ||
EngineName string `json:"engine_name"` | ||
// Version of the engine, example lvm-v1.3.x-e927123:11-08-2023-e927123 | ||
EngineVersion string `json:"engine_version"` | ||
// Uid of the default k8s ns, to uniquely identify, example f5d2a546-19ce-407d-99d4-0655d67e2f76 | ||
K8sDefaultNsUid string `json:"k8s_default_ns_uid"` | ||
// Installer of the app, example lvm-localpv-helm | ||
EngineInstaller string `json:"engine_installer"` | ||
// Machine's os example Ubuntu 20.04.6 LTS, 5.4.0-165-generic, x86_64 | ||
NodeOs string `json:"node_os"` | ||
// Machine's kernel version, example 5.4.0-165-generic | ||
NodeKernelVersion string `json:"node_kernel_version"` | ||
// Machine's arch, example x86_64 | ||
NodeArch string `json:"node_arch"` | ||
// Id of the involved object, example `pvc-b3968e30-9020-4011-943a-7ab338d5f19f` | ||
VolumeName string `json:"vol_name"` | ||
// Extra detail of the involved object, example `lvm-vol-new` | ||
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_category"` | ||
// Value for the event, i.e 4, 2 | ||
Value string `json:"event_action"` | ||
} | ||
|
||
// OpenebsEventBuilder builder pattern code | ||
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 (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 | ||
} | ||
|
||
func NewOpenebsEvent(opts ...OpenebsEventOption) (*OpenebsEvent, error) { | ||
e := &OpenebsEvent{} | ||
func (b *OpenebsEventBuilder) EngineInstaller(engineInstaller string) *OpenebsEventBuilder { | ||
b.openebsEvent.EngineInstaller = engineInstaller | ||
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) NodeOs(nodeOs string) *OpenebsEventBuilder { | ||
b.openebsEvent.NodeOs = nodeOs | ||
return b | ||
} | ||
|
||
return e, nil | ||
func (b *OpenebsEventBuilder) NodeKernelVersion(nodeKernelVersion string) *OpenebsEventBuilder { | ||
b.openebsEvent.NodeKernelVersion = nodeKernelVersion | ||
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) NodeArch(nodeArch string) *OpenebsEventBuilder { | ||
b.openebsEvent.NodeArch = nodeArch | ||
return b | ||
} | ||
|
||
e.Category = category | ||
return nil | ||
} | ||
func (b *OpenebsEventBuilder) ResourceId(resourceId string) *OpenebsEventBuilder { | ||
b.openebsEvent.ResourceId = resourceId | ||
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) Category(category string) *OpenebsEventBuilder { | ||
b.openebsEvent.Category = category | ||
return b | ||
} | ||
|
||
e.Action = action | ||
return nil | ||
} | ||
func (b *OpenebsEventBuilder) Action(action string) *OpenebsEventBuilder { | ||
b.openebsEvent.Action = action | ||
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) 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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,16 @@ | ||
package event | ||
|
||
import "strings" | ||
|
||
func (e *OpenebsEvent) CategoryStr() string { | ||
return e.Category | ||
} | ||
|
||
// Replace the `-` with `_` in the Category | ||
func (e *OpenebsEvent) NormalizedCategoryStr() string { | ||
return strings.ReplaceAll(e.CategoryStr(), "-", "_") | ||
} | ||
|
||
func (e *OpenebsEvent) ActionStr() string { | ||
return e.Action | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,42 @@ | ||
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("x86_64"). | ||
NodeKernelVersion(" 5.4.0-165-generic"). | ||
ResourceId("pvc-b3968e30-9020-4011-943a-7ab338d5f19f"). | ||
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!") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters