Skip to content
This repository was archived by the owner on Jan 27, 2026. It is now read-only.

Commit e822513

Browse files
authored
Merge pull request #10 from AlexisMontagne/am/update-groups
Implement UpdateGroup endpoint
2 parents 75438a0 + 3f6c53b commit e822513

File tree

4 files changed

+65
-1
lines changed

4 files changed

+65
-1
lines changed

example_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func ExampleMixpanel() {
2323
func ExamplePeople() {
2424
client := NewWithSecret("mytoken", "myapisecret", "")
2525

26-
client.Update("1", &Update{
26+
client.UpdateUser("1", &Update{
2727
Operation: "$set",
2828
Properties: map[string]interface{}{
2929
"$email": "user@email.com",

mixpanel.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,15 @@ type Mixpanel interface {
4141
Import(distinctId, eventName string, e *Event) error
4242

4343
// Set properties for a mixpanel user.
44+
// Deprecated: Use UpdateUser instead
4445
Update(distinctId string, u *Update) error
4546

47+
// Set properties for a mixpanel user.
48+
UpdateUser(distinctId string, u *Update) error
49+
50+
// Set properties for a mixpanel group.
51+
UpdateGroup(groupKey, groupId string, u *Update) error
52+
4653
// Create an alias for an existing distinct id
4754
Alias(distinctId, newId string) error
4855
}
@@ -158,7 +165,14 @@ func (m *mixpanel) Import(distinctId, eventName string, e *Event) error {
158165

159166
// Update updates a user in mixpanel. See
160167
// https://mixpanel.com/help/reference/http#people-analytics-updates
168+
// Deprecated: Use UpdateUser instead
161169
func (m *mixpanel) Update(distinctId string, u *Update) error {
170+
return m.UpdateUser(distinctId, u)
171+
}
172+
173+
// UpdateUser: Updates a user in mixpanel. See
174+
// https://mixpanel.com/help/reference/http#people-analytics-updates
175+
func (m *mixpanel) UpdateUser(distinctId string, u *Update) error {
162176
params := map[string]interface{}{
163177
"$token": m.Token,
164178
"$distinct_id": distinctId,
@@ -180,6 +194,20 @@ func (m *mixpanel) Update(distinctId string, u *Update) error {
180194
return m.send("engage", params, autoGeolocate)
181195
}
182196

197+
// UpdateGroup: Updates a group in mixpanel. See
198+
// https://api.mixpanel.com/groups#group-set
199+
func (m *mixpanel) UpdateGroup(groupKey, groupId string, u *Update) error {
200+
params := map[string]interface{}{
201+
"$token": m.Token,
202+
"$group_id": groupId,
203+
"$group_key": groupKey,
204+
}
205+
206+
params[u.Operation] = u.Properties
207+
208+
return m.send("groups", params, false)
209+
}
210+
183211
func (m *mixpanel) to64(data []byte) string {
184212
return base64.StdEncoding.EncodeToString(data)
185213
}

mixpanel_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,34 @@ func TestImport(t *testing.T) {
9494
}
9595
}
9696

97+
func TestGroupOperations(t *testing.T) {
98+
setup()
99+
defer teardown()
100+
101+
client.UpdateGroup("company_id", "11", &Update{
102+
Operation: "$set",
103+
Properties: map[string]interface{}{
104+
"Address": "1313 Mockingbird Lane",
105+
"Birthday": "1948-01-01",
106+
},
107+
})
108+
109+
want := "{\"$group_id\":\"11\",\"$group_key\":\"company_id\",\"$set\":{\"Address\":\"1313 Mockingbird Lane\",\"Birthday\":\"1948-01-01\"},\"$token\":\"e3bc4100330c35722740fb8c6f5abddc\"}"
110+
111+
if !reflect.DeepEqual(decodeURL(LastRequest.URL.String()), want) {
112+
t.Errorf("LastRequest.URL returned %+v, want %+v",
113+
decodeURL(LastRequest.URL.String()), want)
114+
}
115+
116+
want = "/groups"
117+
path := LastRequest.URL.Path
118+
119+
if !reflect.DeepEqual(path, want) {
120+
t.Errorf("path returned %+v, want %+v",
121+
path, want)
122+
}
123+
}
124+
97125
func TestUpdate(t *testing.T) {
98126
setup()
99127
defer teardown()

mock.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ func (mp *MockPeople) String() string {
9494
}
9595

9696
func (m *Mock) Update(distinctId string, u *Update) error {
97+
return m.UpdateUser(distinctId, u)
98+
}
99+
100+
func (m *Mock) UpdateUser(distinctId string, u *Update) error {
97101
p := m.people(distinctId)
98102

99103
if u.IP != "" {
@@ -115,6 +119,10 @@ func (m *Mock) Update(distinctId string, u *Update) error {
115119
return nil
116120
}
117121

122+
func (m *Mock) UpdateGroup(groupKey, groupUser string, u *Update) error {
123+
return nil
124+
}
125+
118126
func (m *Mock) Alias(distinctId, newId string) error {
119127
return nil
120128
}

0 commit comments

Comments
 (0)