Skip to content

Commit

Permalink
PRODENG-2805 drop segment tokens
Browse files Browse the repository at this point in the history
- token removed from code, can be injected at compile time
- if token is empty, disable analytics
- no need for dev/prod token separation, if it is getting injected

Signed-off-by: James Nesbitt <[email protected]>
  • Loading branch information
james-nesbitt committed Dec 4, 2024
1 parent 79f17f5 commit 4b48a5d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
19 changes: 7 additions & 12 deletions pkg/analytics/analytics.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,8 @@ import (
analytics "gopkg.in/segmentio/analytics-go.v3"
)

const (
// ProdSegmentToken is the API token we use for Segment in production.
ProdSegmentToken = "FlDwKhRvN6ts7GMZEgoCEghffy9HXu8Z" //nolint:gosec // intentionally public
// DevSegmentToken is the API token we use for Segment in development.
DevSegmentToken = "DLJn53HXEhUHZ4fPO45MMUhvbHRcfkLE" //nolint:gosec // intentionally public
)
// SegmentToken is the API token we use for Segment. Set at compile time.
var SegmentToken = ""

// Analytics is the interface used for our analytics client.
type Analytics interface {
Expand All @@ -36,11 +32,12 @@ type Client struct {
var defaultClient Client

func init() {
segmentToken := DevSegmentToken
if version.IsProduction() {
segmentToken = ProdSegmentToken
if SegmentToken == "" {
defaultClient = Client{IsEnabled: false}
return
}
ac, err := NewSegmentClient(segmentToken)

ac, err := NewSegmentClient(SegmentToken)
if err != nil {
log.Warnf("failed to initialize analytics: %v", err)
return
Expand Down Expand Up @@ -68,7 +65,6 @@ func NewSegmentClient(segmentToken string) (Analytics, error) { //nolint:ireturn
// is enabled.
func (c *Client) TrackEvent(event string, properties analytics.Properties) error {
if !c.IsEnabled {
log.Debugf("analytics disabled, not tracking event '%s'", event)
return nil
}

Expand All @@ -95,7 +91,6 @@ func (c *Client) TrackEvent(event string, properties analytics.Properties) error
// is enabled.
func (c *Client) IdentifyUser(userConfig *user.Config) error {
if !c.IsEnabled {
log.Debug("analytics disabled, not identifying user")
return nil
}
msg := analytics.Identify{
Expand Down
4 changes: 4 additions & 0 deletions pkg/analytics/analytics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ func TestIdentifyAnalyticsUser(t *testing.T) {
}

func TestDefaultClient(t *testing.T) {
sc, _ := NewSegmentClient("AAAAAAAAA")
defaultClient.AnalyticsClient = sc

userConfig := user.Config{
Name: "John Doe",
Email: "[email protected]",
Expand All @@ -102,6 +105,7 @@ func TestDefaultClient(t *testing.T) {
t.Run("Analytics enabled", func(t *testing.T) {
old := defaultClient.IsEnabled
Enabled(true)

defer Enabled(old)

TrackEvent("foobar", nil)
Expand Down

0 comments on commit 4b48a5d

Please sign in to comment.