Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Client #5

Merged
merged 9 commits into from
Jul 18, 2022
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 84 additions & 19 deletions amplitude/client.go
Original file line number Diff line number Diff line change
@@ -1,42 +1,107 @@
package amplitude

type Amplitude struct {
Configuration Config
timeline Timeline
type Client interface {
Track(event Event)
Identify(identify Identify, eventOptions EventOptions, eventProperties map[string]interface{})
GroupIdentify(groupType string, groupName []string, identify Identify,
eventOptions EventOptions, eventProperties, userProperties map[string]interface{},
)
Revenue(revenue Revenue, eventOptions EventOptions)
SetGroup(groupType string, groupName []string, eventOptions EventOptions)
Flush()
Add(plugin Plugin)
Remove(plugin Plugin)
Shutdown()
}

func (a Amplitude) Track(event BaseEvent) {

func NewClient(config Config) Client {
return &client{configuration: config}
}

func (a Amplitude) Identify(identityObj Identity, eventOptions EventOptions, eventProperties map[string]string) {

type client struct {
configuration Config
timeline Timeline
}

func (a Amplitude) GroupIdentify(groupType string, groupName string, identifyObj Identity, eventOptions EventOptions, eventProperties map[string]string) {

// Track processes and sends the given event object.
func (a *client) Track(event Event) {
a.timeline.Process(event)
}

func (a Amplitude) Revenue(revenueObj Revenue, eventOptions EventOptions) {

// Identify sends an identify event to update user Properties.
func (a *client) Identify(identify Identify, eventOptions EventOptions, eventProperties map[string]interface{}) {
if !identify.IsValid() {
a.configuration.Logger.Error("Empty Identify Properties")
} else {
identifyEvent := Event{
EventType: IdentifyEventEventType,
EventOptions: eventOptions,
EventProperties: eventProperties,
UserProperties: identify.Properties,
}

a.Track(identifyEvent)
}
}

func (a Amplitude) SetGroup(groupType string, groupName string, eventOptions EventOptions) {

// GroupIdentify sends a group identify event to update group Properties.
func (a *client) GroupIdentify(groupType string, groupName []string, identify Identify,
eventOptions EventOptions, eventProperties, userProperties map[string]interface{},
) {
if !identify.IsValid() {
a.configuration.Logger.Error("Empty group identify Properties")
} else {
groupIdentifyEvent := Event{
EventType: GroupIdentifyEventEventType,
EventOptions: eventOptions,
EventProperties: eventProperties,
UserProperties: userProperties,
Groups: map[string][]string{groupType: groupName},
GroupProperties: identify.Properties,
}

a.Track(groupIdentifyEvent)
}
}

func (a Amplitude) Flush() {

// Revenue sends a revenue event with revenue info in eventProperties.
func (a *client) Revenue(revenue Revenue, eventOptions EventOptions) {
if !revenue.IsValid() {
a.configuration.Logger.Error("Invalid revenue quantity")
} else {
revenueEvent := revenue.ToRevenueEvent(eventOptions)
a.Track(revenueEvent)
}
}

func (a Amplitude) Add(plugin Plugin) {

// SetGroup sends an identify event to put a user in group(s)
// by setting group type and group name as user property for a user.
func (a *client) SetGroup(groupType string, groupName []string, eventOptions EventOptions) {
identify := Identify{}
identify.Set(groupType, groupName)
a.Identify(identify, eventOptions, map[string]interface{}{})
}

func (a Amplitude) Remove(plugin Plugin) {
// Flush flushes all events waiting to be sent in the buffer.
func (a *client) Flush() {
a.timeline.Flush()
}

// Add adds the plugin object to client instance.
// Events tracked bby this client instance will be processed by instances' plugins.
func (a *client) Add(plugin Plugin) {
a.timeline.Add(plugin)
plugin.Setup(a)
}

func (a Amplitude) Shutdown() {
// Remove removes the plugin object from client instance.
func (a *client) Remove(plugin Plugin) {
a.timeline.Remove(plugin)
}

// Shutdown shuts the client instance down from accepting new events
// flushes all events in the buffer.
func (a *client) Shutdown() {
a.configuration.OptOut = false
falconandy marked this conversation as resolved.
Show resolved Hide resolved
a.timeline.Shutdown()
}
4 changes: 3 additions & 1 deletion amplitude/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"time"
)

type EventCallback = func(event BaseEvent, code int, message ...string)
type EventCallback = func(event Event, code int, message ...string)

type Config struct {
APIKey string
Expand All @@ -23,13 +23,15 @@ type Config struct {
}

func getStorage(c *Config) Storage {

return c.StorageProvider.GetStorage()
}

func (c Config) IsValid() bool {
if c.APIKey == "" || c.FlushQueueSize <= 0 || c.FlushInterval <= 0 || !c.IsMinIDLengthValid() {
return false
}

return true
}

Expand Down
33 changes: 19 additions & 14 deletions amplitude/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ type PluginType int

type ServerZone string

type IdentityOp string

const (
SdkLibrary = "amplitude-go"
SdkVersion = "0.0.0"
Expand All @@ -15,19 +17,23 @@ const (
Batch = "batch"
HTTPV2 = "v2"

LoggerName = "amplitude"
DefaultGroupIdentifyEvent = "$groupidentify"
IdentityOpAdd = "$add"
IdentityOpAppend = "$append"
IdentityOpClearAll = "$clearAll"
IdentityOpPrepend = "$prepend"
IdentityOpSet = "$set"
IdentityOpSetOnce = "$setOnce"
IdentityOpUnset = "$unset"
IdentityOpPreInsert = "$preInsert"
IdentityOpPostInsert = "$postInsert"
IdentityOpRemove = "$remove"
UnsetValue = "-"
LoggerName = "amplitude"

IdentifyEventEventType = "$identify"
falconandy marked this conversation as resolved.
Show resolved Hide resolved
GroupIdentifyEventEventType = "$groupidentify"
RevenueEventEventType = "revenue_amount"

IdentityOpAdd = "$add"
IdentityOpAppend = "$append"
IdentityOpClearAll = "$clearAll"
IdentityOpPrepend = "$prepend"
IdentityOpSet = "$set"
IdentityOpSetOnce = "$setOnce"
IdentityOpUnset = "$unset"
IdentityOpPreInsert = "$preInsert"
IdentityOpPostInsert = "$postInsert"
IdentityOpRemove = "$remove"
UnsetValue = "-"

RevenueProductID = "$productId"
RevenueQuantity = "$quantity"
Expand All @@ -36,7 +42,6 @@ const (
RevenueReceipt = "$receipt"
RevenueReceiptSig = "$receiptSig"
DefaultRevenue = "$revenue"
AmpRevenueEvent = "revenue_amount"

MaxPropertyKeys = 1024
MaxStringLength = 1024
Expand Down
39 changes: 7 additions & 32 deletions amplitude/event.go
Original file line number Diff line number Diff line change
@@ -1,37 +1,12 @@
package amplitude

type Plan struct {
branch string
source string
version string
versionID string
}

func (p *Plan) getPlanBody() {

}
type EventOptions struct{}

type EventOptions struct {
}

type BaseEvent struct {
type Event struct {
EventType string
EventOptions
}

type Identity struct {
}

type GroupIdentifyEvent struct {
BaseEvent
}

type IdentifyEvent struct {
BaseEvent
}

type Revenue struct {
}

type RevenueEvent struct {
Revenue
EventProperties map[string]interface{}
UserProperties map[string]interface{}
Groups map[string][]string
GroupProperties map[string]interface{}
}
16 changes: 16 additions & 0 deletions amplitude/identify.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package amplitude

type Identify struct {
PropertiesSet []interface{}
Properties map[string]interface{}
}

// IsValid checks if to Identify object has Properties
// returns true if Identify object has Properties, otherwise returns false.
func (i Identify) IsValid() bool {
return len(i.Properties) > 0
}

func (i Identify) Set(key string, value interface{}) {

}
4 changes: 4 additions & 0 deletions amplitude/plan.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package amplitude

type Plan struct {
}
4 changes: 4 additions & 0 deletions amplitude/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ package amplitude
type Plugin struct {
}

func (p Plugin) Setup(client *client) {

}

type EventPlugin struct {
Plugin
}
Expand Down
23 changes: 23 additions & 0 deletions amplitude/revenue.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package amplitude

type Revenue struct {
Price float64
Quantity int
}

// IsValid checks if a revenue instance has a positive integer quantity.
func (r Revenue) IsValid() bool {
return r.Quantity > 0
}

func (r Revenue) ToRevenueEvent(eventOptions EventOptions) Event {
return Event{
EventType: RevenueEventEventType,
EventOptions: eventOptions,
EventProperties: r.GetEventProperties(),
}
}

func (r Revenue) GetEventProperties() map[string]interface{} {
return map[string]interface{}{}
}
4 changes: 2 additions & 2 deletions amplitude/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type InMemoryStorage struct {
func (i *InMemoryStorage) setup(configuration Config, workers Worker) {
}

func (i InMemoryStorage) push(event BaseEvent, delay int) {
func (i InMemoryStorage) push(event Event, delay int) {

}

Expand All @@ -31,7 +31,7 @@ func (i InMemoryStorage) pullAll() {

}

func (i InMemoryStorage) insertEvent(totalDelay int, event BaseEvent) {
func (i InMemoryStorage) insertEvent(totalDelay int, event Event) {

}

Expand Down
20 changes: 20 additions & 0 deletions amplitude/timeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,23 @@ package amplitude

type Timeline struct {
}

func (t Timeline) Process(event Event) {

}

func (t *Timeline) Add(plugin Plugin) {

}

func (t Timeline) Flush() {

}

func (t *Timeline) Remove(plugin Plugin) {

}

func (t *Timeline) Shutdown() {

}
4 changes: 2 additions & 2 deletions examples/track_example/track_example.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ func main() {
config := amplitude.Config{APIKey: "your_api_key"}

// Config callback function (optional)
client := amplitude.Amplitude{Configuration: config}
client := amplitude.NewClient(config)

// Create a BaseEvent instance
event := amplitude.BaseEvent{}
event := amplitude.Event{}

// Track an event
client.Track(event)
Expand Down