-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathsegments.go
More file actions
44 lines (37 loc) · 1.4 KB
/
segments.go
File metadata and controls
44 lines (37 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package customerio
import (
"context"
"fmt"
"net/url"
)
// AddPeopleToSegment adds customers to a manual segment by segment ID.
// See https://docs.customer.io/api/track/#operation/add_customers
func (c *CustomerIO) AddPeopleToSegment(ctx context.Context, segmentID int, ids []string, opts ...SegmentOption) error {
return c.segmentMembership(ctx, "add_customers", segmentID, ids, opts...)
}
// RemovePeopleFromSegment removes customers from a manual segment by segment ID.
// See https://docs.customer.io/api/track/#operation/remove_customers
func (c *CustomerIO) RemovePeopleFromSegment(ctx context.Context, segmentID int, ids []string, opts ...SegmentOption) error {
return c.segmentMembership(ctx, "remove_customers", segmentID, ids, opts...)
}
func (c *CustomerIO) segmentMembership(ctx context.Context, action string, segmentID int, ids []string, opts ...SegmentOption) error {
if segmentID <= 0 {
return ParamError{Param: "segmentID"}
}
if len(ids) == 0 {
return ParamError{Param: "ids"}
}
u := fmt.Sprintf("%s/api/v1/segments/%d/%s", c.URL, segmentID, action)
// url.Values handles encoding for any future option that carries an
// arbitrary string; current options (id_type) only emit URL-safe values.
q := url.Values{}
for _, opt := range opts {
opt(q)
}
if encoded := q.Encode(); encoded != "" {
u += "?" + encoded
}
return c.request(ctx, "POST", u, map[string]interface{}{
"ids": ids,
})
}