Documentation
¶
Overview ¶
Package customerio is Golang library for implementing the CustomerIO API following their documentation: https://customer.io/docs/api/
If you have any suggestions or comments, please feel free to open an issue on this GitHub repository!
By CustomerIO (Original repo) By MrZ (Fork of Original repo)
Index ¶
- Variables
- type APIError
- type Client
- func (c *Client) DeleteCustomer(customerIDOrEmail string) error
- func (c *Client) DeleteDevice(customerIDOrEmail, deviceID string) error
- func (c *Client) FindRegion() (region *RegionInfo, err error)
- func (c *Client) GetUserAgent() string
- func (c *Client) NewAnonymousEvent(eventName string, timestamp time.Time, data map[string]interface{}) error
- func (c *Client) NewEvent(customerIDOrEmail string, eventName string, timestamp time.Time, ...) error
- func (c *Client) NewEventUsingInterface(customerIDOrEmail string, eventName string, timestamp time.Time, ...) error
- func (c *Client) SendEmail(emailRequest *EmailRequest) (*EmailResponse, error)
- func (c *Client) TestAuth() error
- func (c *Client) UpdateCollection(collectionID, collectionName string, items []map[string]interface{}) error
- func (c *Client) UpdateCollectionViaURL(collectionID, collectionName string, jsonURL string) error
- func (c *Client) UpdateCustomer(customerIDOrEmail string, attributes map[string]interface{}) error
- func (c *Client) UpdateDevice(customerIDOrEmail string, device *Device) error
- func (c *Client) WithCustomHTTPClient(client *resty.Client) *Client
- type ClientOps
- func WithAppKey(appAPIKey string) ClientOps
- func WithHTTPTimeout(timeout time.Duration) ClientOps
- func WithRegion(r region) ClientOps
- func WithRequestTracing() ClientOps
- func WithRetryCount(retries int) ClientOps
- func WithTrackingKey(siteID, trackingAPIKey string) ClientOps
- func WithUserAgent(userAgent string) ClientOps
- type Device
- type DevicePlatform
- type EmailRequest
- type EmailResponse
- type ParamError
- type RegionInfo
- type StandardResponse
- type TransactionalError
- type TransactionalResponse
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( RegionUS = region{ // contains filtered or unexported fields } RegionEU = region{ // contains filtered or unexported fields } )
Current regions available
var ErrAttachmentExists = errors.New("attachment with this name already exists")
ErrAttachmentExists is the error message if the attachment already exists
Functions ¶
This section is empty.
Types ¶
type APIError ¶ added in v1.3.0
type APIError struct {
// contains filtered or unexported fields
}
APIError is returned by any method that fails at the API level
type Client ¶ added in v1.3.0
type Client struct {
// contains filtered or unexported fields
}
Client is the CustomerIO client/configuration
func NewClient ¶ added in v1.3.0
NewClient creates a new client for all CustomerIO requests (tracking, app, beta)
If no options are given, it will use the DefaultClientOptions() If no client is supplied it will use a default Resty HTTP client
Example ¶
ExampleNewClient example using NewClient()
See more examples in /examples/
client, err := NewClient(WithTrackingKey(testSiteID, testTrackingAPIKey)) if err != nil { fmt.Printf("error loading client: %s", err.Error()) return } fmt.Printf("loaded client: %s", client.options.userAgent)
Output: loaded client: go-customerio: v1.5.0
func (*Client) DeleteCustomer ¶ added in v1.3.0
DeleteCustomer will remove a customer given their id or email If not found, a customer will be created. If found, the attributes will be updated See: https://customer.io/docs/api/#operation/delete Only use "email" if the workspace is setup to use email instead of ID
Example ¶
ExampleClient_DeleteCustomer example using DeleteCustomer()
See more examples in /examples/
// Load the client client, err := newTestClient() if err != nil { fmt.Printf("error loading client: %s", err.Error()) return } mockDeleteCustomer(http.StatusOK, testCustomerID) // Delete customer err = client.DeleteCustomer(testCustomerID) if err != nil { fmt.Printf("error deleting customer: %s", err.Error()) return } fmt.Printf("customer deleted: %s", testCustomerID)
Output: customer deleted: 123
func (*Client) DeleteDevice ¶ added in v1.3.0
DeleteDevice will remove a customer's device See: https://customer.io/docs/api/#operation/delete_device Only use "email" if the workspace is setup to use email instead of ID
Example ¶
ExampleClient_DeleteDevice example using DeleteDevice()
See more examples in /examples/
// Load the client client, err := newTestClient() if err != nil { fmt.Printf("error loading client: %s", err.Error()) return } mockDeleteDevice(http.StatusOK, testCustomerID, testDeviceID) // Delete device err = client.DeleteDevice(testCustomerID, testDeviceID) if err != nil { fmt.Printf("error deleting device: %s", err.Error()) return } fmt.Printf("device deleted: %s", testDeviceID)
Output: device deleted: abcdefghijklmnopqrstuvwxyz
func (*Client) FindRegion ¶ added in v1.3.0
func (c *Client) FindRegion() (region *RegionInfo, err error)
FindRegion will return the url, data center and environment id See: https://customer.io/docs/api/#tag/trackAuth
func (*Client) GetUserAgent ¶ added in v1.3.0
GetUserAgent will return the user agent string of the client
func (*Client) NewAnonymousEvent ¶ added in v1.3.0
func (c *Client) NewAnonymousEvent(eventName string, timestamp time.Time, data map[string]interface{}) error
NewAnonymousEvent will create a new event for the anonymous visitor See: https://customer.io/docs/api/#operation/trackAnonymous AKA: TrackAnonymous() Use "timestamp" to send events in the past. If not set, it will use Now().UTC()
Example ¶
ExampleClient_NewAnonymousEvent example using NewAnonymousEvent()
See more examples in /examples/
// Load the client client, err := newTestClient() if err != nil { fmt.Printf("error loading client: %s", err.Error()) return } mockNewAnonymousEvent(http.StatusOK) // New event err = client.NewAnonymousEvent( testEventName, time.Now().UTC(), map[string]interface{}{ "field_name": "some_value", "int_field": 123, "timestamp_field": time.Now().UTC().Unix(), }) if err != nil { fmt.Printf("error creating event: %s", err.Error()) return } fmt.Printf("event created: %s", testEventName)
Output: event created: test_event
func (*Client) NewEvent ¶ added in v1.3.0
func (c *Client) NewEvent(customerIDOrEmail string, eventName string, timestamp time.Time, data map[string]interface{}) error
NewEvent will create a new event for the supplied customer See: https://customer.io/docs/api/#tag/Track-Events AKA: Track() Only use "email" if the workspace is setup to use email instead of ID Use "timestamp" to send events in the past. If not set, it will use Now().UTC()
Example ¶
ExampleClient_NewEvent example using NewEvent()
See more examples in /examples/
// Load the client client, err := newTestClient() if err != nil { fmt.Printf("error loading client: %s", err.Error()) return } mockNewEvent(http.StatusOK, testCustomerID) // New event err = client.NewEvent( testCustomerID, testEventName, time.Now().UTC(), map[string]interface{}{ "field_name": "some_value", "int_field": 123, "timestamp_field": time.Now().UTC().Unix(), }) if err != nil { fmt.Printf("error creating event: %s", err.Error()) return } fmt.Printf("event created: %s", testEventName)
Output: event created: test_event
func (*Client) NewEventUsingInterface ¶ added in v1.3.1
func (c *Client) NewEventUsingInterface(customerIDOrEmail string, eventName string, timestamp time.Time, data interface{}) error
NewEventUsingInterface is a wrapper for NewEvent() which can take a custom struct vs map[string]interface{} See: https://customer.io/docs/api/#tag/Track-Events AKA: Track() Only use "email" if the workspace is setup to use email instead of ID Use "timestamp" to send events in the past. If not set, it will use Now().UTC()
func (*Client) SendEmail ¶ added in v1.3.0
func (c *Client) SendEmail(emailRequest *EmailRequest) (*EmailResponse, error)
SendEmail sends a single transactional email using the Customer.io transactional API See: https://customer.io/docs/api/#tag/Transactional
Example ¶
ExampleClient_SendEmail example using SendEmail()
See more examples in /examples/
// Load the client client, err := newTestClient() if err != nil { fmt.Printf("error loading client: %s", err.Error()) return } mockSendEmail(http.StatusOK) // Start the email request (with template) emailRequestWithTemplate := &EmailRequest{ TransactionalMessageID: "123", Identifiers: map[string]string{"id": "123"}, MessageData: map[string]interface{}{ "name": "Person", "items": map[string]interface{}{ "name": "shoes", "price": "59.99", }, "products": []interface{}{}, }, To: "bob@example.com", } // Send email _, err = client.SendEmail(emailRequestWithTemplate) if err != nil { fmt.Printf("error sending email: %s", err.Error()) return } fmt.Printf("email sent to: %s", emailRequestWithTemplate.To)
Output: email sent to: bob@example.com
func (*Client) TestAuth ¶ added in v1.3.0
TestAuth will test your current Tracking API credentials See: https://customer.io/docs/api/#tag/trackAuth
func (*Client) UpdateCollection ¶ added in v1.3.3
func (c *Client) UpdateCollection(collectionID, collectionName string, items []map[string]interface{}) error
UpdateCollection will create or update a collection with raw data See: https://customer.io/docs/api/#operation/addCollection See: https://customer.io/docs/api/#operation/updateCollection
The name of the collection. This is how you'll reference your collection in message. Updating the data or url for your collection fully replaces the contents of the collection. Data example: {"data":[{"property1":null,"property2":null}]}}
Example ¶
ExampleClient_UpdateCollection example using UpdateCollection()
See more examples in /examples/
// Load the client client, err := newTestClient() if err != nil { fmt.Printf("error loading client: %s", err.Error()) return } mockNewCollection(http.StatusOK) // New collection err = client.UpdateCollection( "", testCollectionName, []map[string]interface{}{ { "item_name": "test_item_1", "id_field": 1, "timestamp_field": time.Now().UTC().Unix(), }, { "item_name": "test_item_2", "id_field": 2, "timestamp_field": time.Now().UTC().Unix(), }, }) if err != nil { fmt.Printf("error creating collection: %s", err.Error()) return } fmt.Printf("collection created: %s", testCollectionName)
Output: collection created: test_collection
func (*Client) UpdateCollectionViaURL ¶ added in v1.3.3
UpdateCollectionViaURL will create or update a collection using a URL to a JSON file See: https://customer.io/docs/api/#operation/addCollection See: https://customer.io/docs/api/#operation/updateCollection
The name of the collection. This is how you'll reference your collection in message.
The URL where your data is stored. If your URL does not include a Content-Type, Customer.io assumes your data is JSON. This URL can also be a google sheet that you've shared with cio_share@customer.io. Updating the data or url for your collection fully replaces the contents of the collection.
Example ¶
ExampleClient_UpdateCollectionViaURL example using UpdateCollectionViaURL()
See more examples in /examples/
// Load the client client, err := newTestClient() if err != nil { fmt.Printf("error loading client: %s", err.Error()) return } mockNewCollectionViaURL(http.StatusOK) // New collection err = client.UpdateCollectionViaURL( "", testCollectionName, testCollectionURL, ) if err != nil { fmt.Printf("error creating collection: %s", err.Error()) return } fmt.Printf("collection created: %s", testCollectionName)
Output: collection created: test_collection
func (*Client) UpdateCustomer ¶ added in v1.3.0
UpdateCustomer will add/update a customer and sets their attributes If not found, a customer will be created. If found, the attributes will be updated See: https://customer.io/docs/api/#operation/identify AKA: Identify() Only use "email" if the workspace is setup to use email instead of ID
Example ¶
ExampleClient_UpdateCustomer example using UpdateCustomer()
See more examples in /examples/
// Load the client client, err := newTestClient() if err != nil { fmt.Printf("error loading client: %s", err.Error()) return } mockUpdateCustomer(http.StatusOK, testCustomerID) // Update customer err = client.UpdateCustomer(testCustomerID, map[string]interface{}{ "created_at": time.Now().Unix(), "email": testCustomerEmail, "first_name": "Bob", "plan": "basic", }) if err != nil { fmt.Printf("error updating customer: %s", err.Error()) return } fmt.Printf("customer updated: %s", testCustomerID)
Output: customer updated: 123
func (*Client) UpdateDevice ¶ added in v1.3.0
UpdateDevice will add/update a customer's device If not found, a device will be created. If found, the attributes will be updated See: https://customer.io/docs/api/#operation/add_device Only use "email" if the workspace is setup to use email instead of ID
Example ¶
ExampleClient_UpdateDevice example using UpdateDevice()
See more examples in /examples/
// Load the client client, err := newTestClient() if err != nil { fmt.Printf("error loading client: %s", err.Error()) return } mockUpdateDevice(http.StatusOK, testCustomerID) // Delete customer err = client.UpdateDevice(testCustomerID, &Device{ ID: testDeviceID, LastUsed: time.Now().UTC().Unix(), Platform: PlatformIOs, }) if err != nil { fmt.Printf("error updating device: %s", err.Error()) return } fmt.Printf("device updated: %s", testDeviceID)
Output: device updated: abcdefghijklmnopqrstuvwxyz
func (*Client) WithCustomHTTPClient ¶ added in v1.3.0
WithCustomHTTPClient will overwrite the default client with a custom client.
type ClientOps ¶ added in v1.3.0
type ClientOps func(c *clientOptions)
ClientOps allow functional options to be supplied that overwrite default client options.
func WithAppKey ¶ added in v1.3.0
WithAppKey will provide the App or Beta API key See: https://fly.customer.io/settings/api_credentials?keyType=app
func WithHTTPTimeout ¶ added in v1.3.0
WithHTTPTimeout can be supplied to adjust the default http client timeouts. The http client is used when creating requests Default timeout is 20 seconds.
func WithRegion ¶ added in v1.3.0
func WithRegion(r region) ClientOps
WithRegion will change the region API endpoints
func WithRequestTracing ¶ added in v1.3.0
func WithRequestTracing() ClientOps
WithRequestTracing will enable tracing. Tracing is disabled by default.
func WithRetryCount ¶ added in v1.3.0
WithRetryCount will overwrite the default retry count for http requests. Default retries is 2.
func WithTrackingKey ¶ added in v1.3.0
WithTrackingKey will provide the SiteID and Tracking API key See: https://fly.customer.io/settings/api_credentials
func WithUserAgent ¶ added in v1.3.0
WithUserAgent will overwrite the default useragent. Default is package name + version.
type Device ¶ added in v1.3.0
type Device struct { ID string `json:"id"` LastUsed int64 `json:"last_used"` Platform DevicePlatform `json:"platform"` }
Device is the customer device model
type DevicePlatform ¶ added in v1.3.0
type DevicePlatform string
DevicePlatform is the platform for the customer device
const ( PlatformIOs DevicePlatform = "ios" PlatformAndroid DevicePlatform = "android" )
Allowed types of platforms
type EmailRequest ¶ added in v1.3.0
type EmailRequest struct { AMPBody string `json:"amp_body,omitempty"` Attachments map[string]string `json:"attachments,omitempty"` BCC string `json:"bcc,omitempty"` Body string `json:"body,omitempty"` DisableMessageRetention *bool `json:"disable_message_retention,omitempty"` EnableTracking *bool `json:"tracked,omitempty"` FakeBCC *bool `json:"fake_bcc,omitempty"` From string `json:"from,omitempty"` Headers map[string]string `json:"headers,omitempty"` Identifiers map[string]string `json:"identifiers"` MessageData map[string]interface{} `json:"message_data,omitempty"` PlaintextBody string `json:"plaintext_body,omitempty"` Preheader string `json:"preheader,omitempty"` QueueDraft *bool `json:"queue_draft,omitempty"` ReplyTo string `json:"reply_to,omitempty"` SendToUnsubscribed *bool `json:"send_to_unsubscribed,omitempty"` Subject string `json:"subject,omitempty"` To string `json:"to,omitempty"` TransactionalMessageID string `json:"transactional_message_id,omitempty"` }
EmailRequest is the request structure for sending an email
type EmailResponse ¶ added in v1.3.0
type EmailResponse struct {
TransactionalResponse
}
EmailResponse is the response from sending the email
type ParamError ¶ added in v1.3.0
type ParamError struct {
Param string // Param is the name of the parameter.
}
ParamError is an error returned if a parameter to the track API is invalid.
func (ParamError) Error ¶ added in v1.3.0
func (p ParamError) Error() string
Error is used to display the error message
type RegionInfo ¶ added in v1.3.0
type RegionInfo struct { DataCenter string `json:"data_center"` EnvironmentID uint64 `json:"environment_id"` URL string `json:"url"` }
RegionInfo is the response from FindRegion
type StandardResponse ¶ added in v1.3.0
type StandardResponse struct { Body []byte `json:"-"` // Body of the response request StatusCode int `json:"-"` // Status code returned on the request Tracing resty.TraceInfo `json:"-"` // Trace information if enabled on the request }
StandardResponse is the standard fields returned on all responses
type TransactionalError ¶ added in v1.3.0
type TransactionalError struct { Err string // Err is a more specific error message. StatusCode int // StatusCode is the http status code for the error. }
TransactionalError is returned if a transactional message fails to send.
func (*TransactionalError) Error ¶ added in v1.3.0
func (e *TransactionalError) Error() string
Error with display the string error message
type TransactionalResponse ¶ added in v1.3.0
type TransactionalResponse struct { DeliveryID string `json:"delivery_id"` // DeliveryID is a unique id for the given message. QueuedAt time.Time `json:"queued_at"` // QueuedAt is when the message was queued. }
TransactionalResponse is a response to the send of a transactional message.
func (*TransactionalResponse) UnmarshalJSON ¶ added in v1.3.0
func (t *TransactionalResponse) UnmarshalJSON(b []byte) (err error)
UnmarshalJSON will unmarshall the response