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 2 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 @@ -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",
Expand Down
30 changes: 28 additions & 2 deletions mixpanel.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

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

// UpdateUser: Updates a group in mixpanel. See
dukex marked this conversation as resolved.
Show resolved Hide resolved
// 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 @@ -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()
Expand Down
8 changes: 8 additions & 0 deletions mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 != "" {
Expand All @@ -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
}
Expand Down