Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Add support for segment management in API client (App and Track) #50

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,11 @@ if err := track.TrackCtx(ctx, "5", "purchase", map[string]interface{}{
}
```

## Segments API

We also provide a client for managing customer segments through the Segments API. For more details on how to use it, refer to the [Segments API README](./SEGMENTS.md).


## Contributing

1. Fork it
Expand Down
176 changes: 176 additions & 0 deletions SEGMENTS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
# Customer.io Segments API Methods

This document explains how to use the new methods added for managing customer segments in Customer.io. These methods enable you to list, create, delete segments, and manage customers in segments.

## Usage

### Available Methods

1. **Create Segment**

Creates a new segment in Customer.io.

```go
request := &CreateSegmentRequest{
Segment: Segment{
Name: "New Segment",
},
}

resp, err := cio.CreateSegment(context.Background(), request)
if err != nil {
log.Fatal(err)
}

log.Printf("Segment created: ID: %d, Name: %s", resp.Segment.ID, resp.Segment.Name)
```

2. **List Segments**

Retrieves a list of all customer segments.

```go
segments, err := cio.ListSegments(context.Background())
if err != nil {
log.Fatal(err)
}

for _, segment := range segments.Segments {
log.Printf("Segment ID: %d, Name: %s", segment.ID, segment.Name)
}
```

3. **Get Segment by ID**

Retrieves a specific segment by its ID.

```go
segmentID := 1234
resp, err := cio.GetSegment(context.Background(), segmentID)
if err != nil {
log.Fatal(err)
}

log.Printf("Segment ID: %d, Name: %s", resp.Segment.ID, resp.Segment.Name)
```

4. **Delete Segment**

Deletes a segment by its ID.

```go
segmentID := 1234
err := cio.DeleteSegment(context.Background(), segmentID)
if err != nil {
log.Fatal(err)
}

log.Println("Segment deleted successfully")
```

5. **Get Segment Dependencies**

Retrieves dependencies for a specific segment.

```go
segmentID := 1234
dependencies, err := cio.GetSegmentDependencies(context.Background(), segmentID)
if err != nil {
log.Fatal(err)
}

log.Printf("Segment dependencies: %v", dependencies)
```

6. **Get Segment Customer Count**

Retrieves the number of customers in a specific segment.

```go
segmentID := 1234
count, err := cio.GetSegmentCustomerCount(context.Background(), segmentID)
if err != nil {
log.Fatal(err)
}

log.Printf("Customer count in segment %d: %d", segmentID, count.Count)
```

7. **List Customers in a Segment**

Retrieves a list of customers in a specific segment.

```go
segmentID := 1234
customers, err := cio.ListCustomersInSegment(context.Background(), segmentID)
if err != nil {
log.Fatal(err)
}

for _, customer := range customers.Identifiers {
log.Printf("Customer ID: %d", customer.ID)
}
```

### Managing Customers in Segments

You can add or remove customers from segments using the following methods:

1. **Add People to Segment**

Adds a list of customer IDs to a segment.

```go
segmentID := 1234
customerIDs := []string{"customer_1", "customer_2"}

err := track.AddPeopleToSegment(context.Background(), segmentID, customerIDs)
if err != nil {
log.Fatal(err)
}

log.Println("Customers added to segment successfully")
```

2. **Remove People from Segment**

Removes a list of customer IDs from a segment.

```go
segmentID := 1234
customerIDs := []string{"customer_1", "customer_2"}

err := track.RemovePeopleFromSegment(context.Background(), segmentID, customerIDs)
if err != nil {
log.Fatal(err)
}

log.Println("Customers removed from segment successfully")
```

## Example: Creating a Segment and Adding Customers

```go
func main() {
// Create a new segment
request := &CreateSegmentRequest{
Segment: Segment{
Name: "VIP Customers",
},
}
resp, err := cio.CreateSegment(context.Background(), request)
if err != nil {
log.Fatal(err)
}

log.Printf("Created segment: %s (ID: %d)", resp.Segment.Name, resp.Segment.ID)

// Add customers to the new segment
customerIDs := []string{"customer_1", "customer_2"}
err = track.AddPeopleToSegment(context.Background(), resp.Segment.ID, customerIDs)
if err != nil {
log.Fatal(err)
}

log.Println("Customers added to the segment successfully")
}
14 changes: 10 additions & 4 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"encoding/json"
"io"
"io/ioutil"
"net/http"
)
Expand Down Expand Up @@ -32,12 +33,17 @@ func NewAPIClient(key string, opts ...option) *APIClient {
}

func (c *APIClient) doRequest(ctx context.Context, verb, requestPath string, body interface{}) ([]byte, int, error) {
b, err := json.Marshal(body)
if err != nil {
return nil, 0, err
var requestBody io.Reader

if body != nil {
b, err := json.Marshal(body)
if err != nil {
return nil, 0, err
}
requestBody = bytes.NewBuffer(b)
}

req, err := http.NewRequest(verb, c.URL+requestPath, bytes.NewBuffer(b))
req, err := http.NewRequest(verb, c.URL+requestPath, requestBody)
if err != nil {
return nil, 0, err
}
Expand Down
Loading