Skip to content

Commit

Permalink
Merge pull request #10 from AlexisMontagne/am/update-groups
Browse files Browse the repository at this point in the history
Implement UpdateGroup endpoint
  • Loading branch information
dukex committed Apr 10, 2022
2 parents 75438a0 + 3f6c53b commit e822513
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 1 deletion.
2 changes: 1 addition & 1 deletion example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func ExampleMixpanel() {
func ExamplePeople() {
client := NewWithSecret("mytoken", "myapisecret", "")

client.Update("1", &Update{
client.UpdateUser("1", &Update{
Operation: "$set",
Properties: map[string]interface{}{
"$email": "user@email.com",
Expand Down
28 changes: 28 additions & 0 deletions mixpanel.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,15 @@ type Mixpanel interface {
Import(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

// Create an alias for an existing distinct id
Alias(distinctId, newId string) error
}
Expand Down Expand Up @@ -158,7 +165,14 @@ func (m *mixpanel) Import(distinctId, eventName string, e *Event) error {

// Update 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,
Expand All @@ -180,6 +194,20 @@ func (m *mixpanel) Update(distinctId string, u *Update) error {
return m.send("engage", params, autoGeolocate)
}

// 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{}{
"$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)
}
Expand Down
28 changes: 28 additions & 0 deletions mixpanel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,34 @@ func TestImport(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 TestUpdate(t *testing.T) {
setup()
defer teardown()
Expand Down
8 changes: 8 additions & 0 deletions mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,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 != "" {
Expand All @@ -115,6 +119,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
}
Expand Down

0 comments on commit e822513

Please sign in to comment.