Skip to content

Commit

Permalink
Send track message to segment, not only identify message
Browse files Browse the repository at this point in the history
Identify is for adding/updating user properties. Track is for individual
action.
  • Loading branch information
guillaumerose committed Jan 6, 2021
1 parent 667a416 commit 6401a08
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
18 changes: 12 additions & 6 deletions pkg/crc/segment/segment.go
Expand Up @@ -4,6 +4,7 @@ import (
"io/ioutil"
"os"
"path/filepath"
"runtime"
"strings"

"github.com/code-ready/crc/cmd/crc/cmd/config"
Expand Down Expand Up @@ -64,13 +65,18 @@ func (c *Client) Upload(action string, err error) error {
return uerr
}

t := analytics.NewTraits().
Set("action", action).
Set("error", err.Error())

return c.segmentClient.Enqueue(analytics.Identify{
if err := c.segmentClient.Enqueue(analytics.Identify{
AnonymousId: anonymousID,
Traits: analytics.NewTraits().
Set("os", runtime.GOOS),
}); err != nil {
return err
}
return c.segmentClient.Enqueue(analytics.Track{
AnonymousId: anonymousID,
Traits: t,
Event: action,
Properties: analytics.NewProperties().
Set("error", err.Error()),
})
}

Expand Down
11 changes: 9 additions & 2 deletions pkg/crc/segment/segment_test.go
Expand Up @@ -8,6 +8,7 @@ import (
"net/http/httptest"
"os"
"path/filepath"
"runtime"
"testing"

cmdConfig "github.com/code-ready/crc/cmd/crc/cmd/config"
Expand All @@ -22,8 +23,11 @@ type segmentResponse struct {
AnonymousID string `json:"anonymousId"`
MessageID string `json:"messageId"`
Traits struct {
Error string `json:"error"`
OS string `json:"os"`
} `json:"traits"`
Properties struct {
Error string `json:"error"`
} `json:"properties"`
Type string `json:"type"`
} `json:"batch"`
Context struct {
Expand Down Expand Up @@ -88,7 +92,10 @@ func TestClientUploadWithConsent(t *testing.T) {
case x := <-body:
s := segmentResponse{}
require.NoError(t, json.Unmarshal(x, &s))
require.Equal(t, s.Batch[0].Traits.Error, "an error occurred")
require.Equal(t, s.Batch[0].Type, "identify")
require.Equal(t, s.Batch[0].Traits.OS, runtime.GOOS)
require.Equal(t, s.Batch[1].Type, "track")
require.Equal(t, s.Batch[1].Properties.Error, "an error occurred")
require.Equal(t, s.Context.App.Name, "crc")
require.Equal(t, s.Context.App.Version, version.GetCRCVersion())
default:
Expand Down

0 comments on commit 6401a08

Please sign in to comment.