Skip to content

Commit

Permalink
implement import req for mixpanel (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
jakekeeys authored Feb 1, 2021
1 parent 2ea3d87 commit fc19c3e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 10 deletions.
42 changes: 40 additions & 2 deletions go/tracking/mixpanel.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,28 @@ import (
"fmt"
"net/http"
"net/url"
"strconv"
"strings"
"time"
)

const mixpanelEndpoint = "https://api-eu.mixpanel.com/track"
const (
mixpanelEndpoint = "https://api-eu.mixpanel.com/track"
mixpanelImportEndpoint = "https://api-eu.mixpanel.com/import"
)

func NewMixpanelBackend(token string, client *http.Client) *MixpanelBackend {
func NewMixpanelBackend(token string, client *http.Client, secret string) *MixpanelBackend {
return &MixpanelBackend{
token: token,
client: client,
secret: secret, // Required for import implementation
}
}

type MixpanelBackend struct {
token string
client *http.Client
secret string
}

type mixpanelRequest struct {
Expand Down Expand Up @@ -55,6 +62,18 @@ func (b *MixpanelBackend) track(ctx context.Context, eventName string, propertie
if err != nil {
return fmt.Errorf("mixpanel backend: %w", err)
}

// If event is older than 4 days https://developer.mixpanel.com/reference/events#import-events
imp := shouldImport(properties)
if imp && b.secret != "" {
req, err = http.NewRequestWithContext(ctx, "POST", mixpanelImportEndpoint, strings.NewReader(body.Encode()))
if err != nil {
return fmt.Errorf("mixpanel backend: %w", err)
}

req.SetBasicAuth(b.secret, "")
}

req.Header.Set("Content-type", "application/x-www-form-urlencoded")

res, err := b.client.Do(req)
Expand Down Expand Up @@ -102,3 +121,22 @@ func (b *MixpanelBackend) Alias(ctx context.Context, currentID string, alias str
func (b *MixpanelBackend) Close() error {
return nil
}

func shouldImport(properties map[string]string) bool {
ets, ok := properties["time"]

if !ok {
return false
}

eti, err := strconv.ParseInt(ets, 10, 64)
if err != nil {
return false
}

if time.Unix(eti, 0).Before(time.Now().AddDate(0,0,-4)) {
return true
}

return false
}
16 changes: 8 additions & 8 deletions go/tracking/mixpanel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestMixpanelBackend_Track(t *testing.T) {
t.Run("sets provided event name as event", func(t *testing.T) {
client, transport := tripper(1, "")

be := tracking.NewMixpanelBackend("apiKey", client)
be := tracking.NewMixpanelBackend("apiKey", client, "")
err := be.Track(ctx, "sample", "id", map[string]string{})

require.NoError(t, err)
Expand All @@ -30,7 +30,7 @@ func TestMixpanelBackend_Track(t *testing.T) {
t.Run("sets distinct_id ", func(t *testing.T) {
client, transport := tripper(1, "")

be := tracking.NewMixpanelBackend("apiKey", client)
be := tracking.NewMixpanelBackend("apiKey", client, "")
err := be.Track(ctx, "sample", "000000", map[string]string{})

require.NoError(t, err)
Expand All @@ -40,7 +40,7 @@ func TestMixpanelBackend_Track(t *testing.T) {
t.Run("sets token", func(t *testing.T) {
client, transport := tripper(1, "")

be := tracking.NewMixpanelBackend("apiKey", client)
be := tracking.NewMixpanelBackend("apiKey", client, "")
err := be.Track(ctx, "sample", "0000000", map[string]string{})

require.NoError(t, err)
Expand All @@ -50,7 +50,7 @@ func TestMixpanelBackend_Track(t *testing.T) {
t.Run("returns an error if call is not successful", func(t *testing.T) {
client, _ := tripper(0, "errorMsg")

be := tracking.NewMixpanelBackend("apiKey", client)
be := tracking.NewMixpanelBackend("apiKey", client, "")
err := be.Track(ctx, "sample", "0000000", map[string]string{})

assert.Errorf(t, err, "mixpanel backend: call was not successful: errorMsg")
Expand All @@ -59,7 +59,7 @@ func TestMixpanelBackend_Track(t *testing.T) {
t.Run("sends all properties and returns nil if successful", func(t *testing.T) {
client, transport := tripper(1, "")

be := tracking.NewMixpanelBackend("apiKey", client)
be := tracking.NewMixpanelBackend("apiKey", client, "")
err := be.Track(ctx, "sample", "0000000", map[string]string{
"prop1": "prop1",
"prop2": "prop2",
Expand All @@ -78,7 +78,7 @@ func TestMixpanelBackend_Alias(t *testing.T) {
t.Run("sends correct properties and returns nil", func(t *testing.T) {
client, transport := tripper(1, "")

be := tracking.NewMixpanelBackend("apiKey", client)
be := tracking.NewMixpanelBackend("apiKey", client, "")
err := be.Alias(ctx, "id", "alias")

require.NoError(t, err)
Expand All @@ -92,7 +92,7 @@ func TestMixpanelBackend_Alias(t *testing.T) {
t.Run("returns an error if call is not successful", func(t *testing.T) {
client, _ := tripper(0, "errMsg")

be := tracking.NewMixpanelBackend("apiKey", client)
be := tracking.NewMixpanelBackend("apiKey", client, "")
err := be.Alias(ctx, "id", "alias")

assert.Errorf(t, err, "mixpanel backend: call was not successful: errMsg")
Expand Down Expand Up @@ -159,7 +159,7 @@ func tripper(code int, msg string) (*http.Client, *testRoundTripper) {

func TestMixpanelBackend_TrackIntegration(t *testing.T) {

mp := tracking.NewMixpanelBackend("asd", http.DefaultClient)
mp := tracking.NewMixpanelBackend("asd", http.DefaultClient, "")

err := mp.Track(context.Background(), "test-event", "test-distinct-id", map[string]string{"param": "value"})

Expand Down

0 comments on commit fc19c3e

Please sign in to comment.