Skip to content

Commit

Permalink
feat: track setup flag toggle event (#222)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sunny Guduru authored Apr 30, 2024
1 parent 68d0bf0 commit 3b408cc
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 3 deletions.
60 changes: 60 additions & 0 deletions internal/analytics/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ type Tracker interface {
optOut bool,
step string,
)
SendSetupFlagToggledEvent(
accessToken,
baseURI string,
optOut,
on bool,
count int,
duration_ms int64,
)
}

type Client struct {
Expand Down Expand Up @@ -151,6 +159,27 @@ func (c *Client) SendSetupStepStartedEvent(
)
}

func (c *Client) SendSetupFlagToggledEvent(
accessToken,
baseURI string,
optOut,
on bool,
count int,
duration_ms int64,
) {
c.sendEvent(
accessToken,
baseURI,
optOut,
"CLI Setup Flag Toggled",
map[string]interface{}{
"on": on,
"count": count,
"duration_ms": duration_ms,
},
)
}

func (a *Client) Wait() {
a.wg.Wait()
}
Expand Down Expand Up @@ -181,6 +210,16 @@ func (c *NoopClient) SendSetupStepStartedEvent(
) {
}

func (c *NoopClient) SendSetupFlagToggledEvent(
accessToken,
baseURI string,
optOut,
on bool,
count int,
duration_ms int64,
) {
}

type MockTracker struct {
mock.Mock
ID string
Expand Down Expand Up @@ -245,3 +284,24 @@ func (m *MockTracker) SendSetupStepStartedEvent(
},
)
}

func (m *MockTracker) SendSetupFlagToggledEvent(
accessToken,
baseURI string,
optOut,
on bool,
count int,
duration_ms int64,
) {
m.sendEvent(
accessToken,
baseURI,
optOut,
"CLI Setup Flag Toggled",
map[string]interface{}{
"on": on,
"count": count,
"duration_ms": duration_ms,
},
)
}
30 changes: 29 additions & 1 deletion internal/quickstart/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package quickstart
import (
"fmt"
"log"
"time"

"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/spinner"
Expand Down Expand Up @@ -53,9 +54,14 @@ type ContainerModel struct {
err error
flagKey string
flagsClient flags.Client
flagStatus bool
flagToggled bool
gettingStarted bool
quitting bool
sdk sdkDetail
startTime time.Time
toggleCount int
toggleTime time.Time
totalSteps int
width int
}
Expand All @@ -78,6 +84,7 @@ func NewContainerModel(
environmentsClient: environmentsClient,
flagsClient: flagsClient,
gettingStarted: true,
startTime: time.Now(),
totalSteps: 4,
}
}
Expand Down Expand Up @@ -165,10 +172,19 @@ func (m ContainerModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case fetchedFlagStatusMsg:
m.currentModel, cmd = m.currentModel.Update(msg)
m.err = nil
case fetchedSDKInstructionsMsg, selectedSDKMsg, toggledFlagMsg, spinner.TickMsg, createdFlagMsg:
case fetchedSDKInstructionsMsg, selectedSDKMsg, spinner.TickMsg, createdFlagMsg:
m.gettingStarted = false
m.currentModel, cmd = m.currentModel.Update(msg)
m.err = nil
case toggledFlagMsg:
m.gettingStarted = false
m.currentModel, cmd = m.currentModel.Update(msg)
m.toggleCount++
m.flagStatus = msg.on
m.toggleTime = msg.time
m.flagToggled = true
m.err = nil
sendEvent = true
case showToggleFlagMsg:
m.currentModel = NewToggleFlagModel(
m.flagsClient,
Expand All @@ -192,6 +208,18 @@ func (m ContainerModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.analyticsOptOut,
m.currentStep.String(),
))

if (m.currentStep == stepToggleFlag) && m.flagToggled {
cmd = tea.Batch(cmd, trackSetupFlagToggledEvent(
m.analyticsTracker,
m.accessToken,
m.baseUri,
m.analyticsOptOut,
m.flagStatus,
m.toggleCount,
m.toggleTime.Sub(m.startTime).Milliseconds(),
))
}
}

return m, cmd
Expand Down
19 changes: 17 additions & 2 deletions internal/quickstart/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"time"

tea "github.com/charmbracelet/bubbletea"

Expand All @@ -26,7 +27,10 @@ func sendErrMsg(err error) tea.Cmd {
}
}

type toggledFlagMsg struct{}
type toggledFlagMsg struct {
time time.Time
on bool
}

func toggleFlag(client flags.Client, accessToken, baseUri, flagKey string, enabled bool) tea.Cmd {
return func() tea.Msg {
Expand All @@ -42,7 +46,10 @@ func toggleFlag(client flags.Client, accessToken, baseUri, flagKey string, enabl
return errMsg{err: err}
}

return toggledFlagMsg{}
return toggledFlagMsg{
time: time.Now(),
on: enabled,
}
}
}

Expand Down Expand Up @@ -251,3 +258,11 @@ func trackSetupStepStartedEvent(tracker analytics.Tracker, accessToken, baseURI
return eventTrackedMsg{}
}
}

func trackSetupFlagToggledEvent(tracker analytics.Tracker, accessToken, baseURI string, optOut, on bool, count int, duration_ms int64) tea.Cmd {
return func() tea.Msg {
tracker.SendSetupFlagToggledEvent(accessToken, baseURI, optOut, on, count, duration_ms)

return eventTrackedMsg{}
}
}

0 comments on commit 3b408cc

Please sign in to comment.