Skip to content

Commit 6bc1738

Browse files
author
Jonathan Manuzak
committed
Merge pull request #1 from codeguard/import_endpoint
Add import capability
2 parents 9dae0fc + 24f3c5f commit 6bc1738

File tree

2 files changed

+34
-9
lines changed

2 files changed

+34
-9
lines changed

mixpanel.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ type Mixpanel struct {
5555
const events_endpoint string = "https://api.mixpanel.com/track"
5656
const people_endpoint string = "https://api.mixpanel.com/engage"
5757

58+
var import_endpoint string = "https://api.mixpanel.com/import"
59+
5860
func b64(payload []byte) []byte {
5961
var b bytes.Buffer
6062
encoder := base64.NewEncoder(base64.URLEncoding, &b)
@@ -108,6 +110,20 @@ mp.Track("12345", "Welcome Email Sent", &P{
108110
})
109111
*/
110112
func (mp *Mixpanel) Track(distinct_id, event string, prop *P) error {
113+
import_endpoint += "?api_key=" + mp.Token
114+
return mp.sendEvent(distinct_id, event, prop, "events")
115+
}
116+
117+
/*
118+
Imports events that occurred more than 5 days in the past. Takes the
119+
same arguments as Track and behaves in the same way.
120+
*/
121+
func (mp *Mixpanel) Import(distinct_id, event string, prop *P) error {
122+
return mp.sendEvent(distinct_id, event, prop, "import")
123+
}
124+
125+
/* Internal implementation of event sending. Can be used with Track or Import. */
126+
func (mp *Mixpanel) sendEvent(distinct_id, event string, prop *P, endpoint string) error {
111127
properties := &P{
112128
"token": mp.Token,
113129
"distinct_id": distinct_id,
@@ -129,7 +145,7 @@ func (mp *Mixpanel) Track(distinct_id, event string, prop *P) error {
129145
return err
130146
}
131147

132-
return mp.c.Send("events", data)
148+
return mp.c.Send(endpoint, data)
133149
}
134150

135151
/*
@@ -337,6 +353,7 @@ func NewStdConsumer() *StdConsumer {
337353
c.endpoints = make(map[string]string)
338354
c.endpoints["events"] = events_endpoint
339355
c.endpoints["people"] = people_endpoint
356+
c.endpoints["import"] = import_endpoint
340357
return c
341358
}
342359

@@ -383,6 +400,7 @@ func NewBuffConsumer(maxSize int64) *BuffConsumer {
383400
bc.buffers = make(map[string][][]byte)
384401
bc.buffers["people"] = make([][]byte, 0, maxSize)
385402
bc.buffers["events"] = make([][]byte, 0, maxSize)
403+
bc.buffers["import"] = make([][]byte, 0, maxSize)
386404
return bc
387405
}
388406

mixpanel_test.go

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,26 +31,25 @@ func TestTrack(t *testing.T) {
3131
}
3232
}
3333

34-
func TestJsonArray(t *testing.T){
35-
result := jsonArray( [][]byte{ []byte("{'a':'b'}")} )
34+
func TestJsonArray(t *testing.T) {
35+
result := jsonArray([][]byte{[]byte("{'a':'b'}")})
3636
if string(result) != "[{'a':'b'}]" {
3737
t.Error(string(result))
3838
}
39-
result = jsonArray( [][]byte{ []byte("{'a':'b'}"), []byte("{'c':'d'}")} )
39+
result = jsonArray([][]byte{[]byte("{'a':'b'}"), []byte("{'c':'d'}")})
4040
if string(result) != "[{'a':'b'},{'c':'d'}]" {
4141
t.Error(string(result))
4242
}
4343
}
4444

45-
func TestSmoke(t *testing.T){
45+
func TestSmoke(t *testing.T) {
4646
Smoke(t, NewMixpanel(token))
4747
Smoke(t, NewMixpanelWithConsumer(token, NewBuffConsumer(1)))
4848
mp := NewBuffConsumer(2)
4949
Smoke(t, NewMixpanelWithConsumer(token, mp))
5050
mp.Flush()
5151
}
5252

53-
5453
func Smoke(t *testing.T, mp *Mixpanel) {
5554

5655
err := mp.PeopleSet("12345", &P{"Address": "1313 Mockingbird Lane",
@@ -64,6 +63,14 @@ func Smoke(t *testing.T, mp *Mixpanel) {
6463
t.Error(err)
6564
}
6665

66+
// Import an older event
67+
err = mp.Import("12345", "Welcome Email Sent", &P{
68+
"time": 1392646952,
69+
})
70+
if err != nil {
71+
t.Error(err)
72+
}
73+
6774
// Track that user "12345"'s credit card was declined
6875
err = mp.Track("12345", "Credit Card Declined", nil)
6976
if err != nil {
@@ -73,9 +80,9 @@ func Smoke(t *testing.T, mp *Mixpanel) {
7380
// Properties describe the circumstances of the event,
7481
// or aspects of the source or user associated with the event
7582
err = mp.Track("12345", "Welcome Email Sent", &P{
76-
"Email Template" : "Pretty Pink Welcome",
77-
"User Sign-up Cohort" : "July 2013",
78-
})
83+
"Email Template": "Pretty Pink Welcome",
84+
"User Sign-up Cohort": "July 2013",
85+
})
7986
if err != nil {
8087
t.Error(err)
8188
}

0 commit comments

Comments
 (0)