From 35a491f468c2421439afedb3158cc0d116feb932 Mon Sep 17 00:00:00 2001 From: Alexis Montagne Date: Tue, 8 Feb 2022 11:59:00 -0800 Subject: [PATCH 1/3] Create a new UpdateUser API and deprecate Update --- example_test.go | 2 +- mixpanel.go | 16 ++++++++++++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/example_test.go b/example_test.go index dafc882..acb2015 100644 --- a/example_test.go +++ b/example_test.go @@ -19,7 +19,7 @@ func ExampleMixpanel() { func ExamplePeople() { client := New("mytoken", "") - client.Update("1", &Update{ + client.UpdateUser("1", &Update{ Operation: "$set", Properties: map[string]interface{}{ "$email": "user@email.com", diff --git a/mixpanel.go b/mixpanel.go index 3b3a599..b30bf07 100644 --- a/mixpanel.go +++ b/mixpanel.go @@ -39,8 +39,15 @@ type Mixpanel interface { Track(distinctId, eventName string, e *Event) error // Set properties for a mixpanel user. + // Deprecated: Use UpdateUser instead Update(distinctId string, u *Update) error + // Set properties for a mixpanel user. + UpdateUser(distinctId string, u *Update) error + + // Set properties for a mixpanel group. + UpdateGroup(groupKey, groupId string, u *Update) error + Alias(distinctId, newId string) error } @@ -124,9 +131,14 @@ func (m *mixpanel) Track(distinctId, eventName string, e *Event) error { return m.send("track", params, autoGeolocate) } -// Updates a user in mixpanel. See -// https://mixpanel.com/help/reference/http#people-analytics-updates +// Deprecated: Use UpdateUser instead func (m *mixpanel) Update(distinctId string, u *Update) error { + return m.UpdateUser(distinctId, u) +} + +// UpdateUser: Updates a user in mixpanel. See +// https://mixpanel.com/help/reference/http#people-analytics-updates +func (m *mixpanel) UpdateUser(distinctId string, u *Update) error { params := map[string]interface{}{ "$token": m.Token, "$distinct_id": distinctId, From 59b07cf4986b4cdd10629f2c9adedd4bbce94bb0 Mon Sep 17 00:00:00 2001 From: Alexis Montagne Date: Tue, 8 Feb 2022 12:07:56 -0800 Subject: [PATCH 2/3] Implement UpdateGroup --- mixpanel.go | 14 ++++++++++++++ mixpanel_test.go | 28 ++++++++++++++++++++++++++++ mock.go | 8 ++++++++ 3 files changed, 50 insertions(+) diff --git a/mixpanel.go b/mixpanel.go index b30bf07..545b132 100644 --- a/mixpanel.go +++ b/mixpanel.go @@ -160,6 +160,20 @@ func (m *mixpanel) UpdateUser(distinctId string, u *Update) error { return m.send("engage", params, autoGeolocate) } +// UpdateUser: Updates a group in mixpanel. See +// https://api.mixpanel.com/groups#group-set +func (m *mixpanel) UpdateGroup(groupKey, groupId string, u *Update) error { + params := map[string]interface{}{ + "$token": m.Token, + "$group_id": groupId, + "$group_key": groupKey, + } + + params[u.Operation] = u.Properties + + return m.send("groups", params, false) +} + func (m *mixpanel) to64(data []byte) string { return base64.StdEncoding.EncodeToString(data) } diff --git a/mixpanel_test.go b/mixpanel_test.go index c92a5c3..ba19653 100644 --- a/mixpanel_test.go +++ b/mixpanel_test.go @@ -91,6 +91,34 @@ func TestPeopleOperations(t *testing.T) { } } +func TestGroupOperations(t *testing.T) { + setup() + defer teardown() + + client.UpdateGroup("company_id", "11", &Update{ + Operation: "$set", + Properties: map[string]interface{}{ + "Address": "1313 Mockingbird Lane", + "Birthday": "1948-01-01", + }, + }) + + want := "{\"$group_id\":\"11\",\"$group_key\":\"company_id\",\"$set\":{\"Address\":\"1313 Mockingbird Lane\",\"Birthday\":\"1948-01-01\"},\"$token\":\"e3bc4100330c35722740fb8c6f5abddc\"}" + + if !reflect.DeepEqual(decodeURL(LastRequest.URL.String()), want) { + t.Errorf("LastRequest.URL returned %+v, want %+v", + decodeURL(LastRequest.URL.String()), want) + } + + want = "/groups" + path := LastRequest.URL.Path + + if !reflect.DeepEqual(path, want) { + t.Errorf("path returned %+v, want %+v", + path, want) + } +} + func TestPeopleTrack(t *testing.T) { setup() defer teardown() diff --git a/mock.go b/mock.go index 4000ce8..e42757e 100644 --- a/mock.go +++ b/mock.go @@ -85,6 +85,10 @@ func (mp *MockPeople) String() string { } func (m *Mock) Update(distinctId string, u *Update) error { + return m.UpdateUser(distinctId, u) +} + +func (m *Mock) UpdateUser(distinctId string, u *Update) error { p := m.people(distinctId) if u.IP != "" { @@ -106,6 +110,10 @@ func (m *Mock) Update(distinctId string, u *Update) error { return nil } +func (m *Mock) UpdateGroup(groupKey, groupUser string, u *Update) error { + return nil +} + func (m *Mock) Alias(distinctId, newId string) error { return nil } From 17bb77b1c96860e4e9ebea572efbb7a835d31e76 Mon Sep 17 00:00:00 2001 From: Duke Date: Sun, 10 Apr 2022 11:04:07 -0300 Subject: [PATCH 3/3] Update mixpanel.go --- mixpanel.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mixpanel.go b/mixpanel.go index 545b132..adfcc18 100644 --- a/mixpanel.go +++ b/mixpanel.go @@ -160,7 +160,7 @@ func (m *mixpanel) UpdateUser(distinctId string, u *Update) error { return m.send("engage", params, autoGeolocate) } -// UpdateUser: Updates a group in mixpanel. See +// UpdateGroup: Updates a group in mixpanel. See // https://api.mixpanel.com/groups#group-set func (m *mixpanel) UpdateGroup(groupKey, groupId string, u *Update) error { params := map[string]interface{}{