Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Implement UpdateGroup endpoint #10

Merged
merged 4 commits into from
Apr 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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