Client
The Client struct is the main interface for interacting with the Codematic Conviso.
Constructor
NewClient(config CDPConfig)
Creates a new Conviso client instance.
import "github.com/codematic/opencdp-go/cdp"
client := cdp.NewClient(cdp.CDPConfig{
CDPAPIKey: "your-api-key",
})
Parameters:
config(CDPConfig) - Configuration options
Returns: *Client instance
Methods
Ping(ctx context.Context)
Tests the connection to the Conviso API server.
err := client.Ping(context.Background())
Returns: error if connection fails.
When to use:
- During application startup to verify configuration.
Identify(ctx context.Context, userID string, traits map[string]interface)
Identifies a user and sets their attributes.
err := client.Identify(ctx, "user123", map[string]interface{}{
"email": "[email protected]",
"name": "Alice",
})
Parameters:
ctx: Context for cancellation/timeout.userID: Unique identifier for the user.traits: Map of user attributes.
Track(ctx context.Context, userID string, eventName string, properties map[string]interface)
Tracks a custom event for a user.
err := client.Track(ctx, "user123", "purchase", map[string]interface{}{
"price": 99.99,
})
Parameters:
ctx: Context.userID: User identifier.eventName: Name of the event.properties: Event properties.
SendEmail(ctx context.Context, payload EmailPayload)
Sends a transactional email.
err := client.SendEmail(ctx, cdp.EmailPayload{
To: "[email protected]",
Identifiers: cdp.Identifiers{ID: "user123"},
TransactionalMessageID: "WELCOME",
})
Parameters:
payload:EmailPayloadstruct.
EmailPayload Struct:
type EmailPayload struct {
To string `json:"to"`
Identifiers Identifiers `json:"identifiers"`
TransactionalMessageID string `json:"transactional_message_id,omitempty"`
MessageData map[string]interface{} `json:"message_data,omitempty"`
Body string `json:"body,omitempty"`
Subject string `json:"subject,omitempty"`
From string `json:"from,omitempty"`
ReplyTo string `json:"reply_to,omitempty"`
BCC []string `json:"bcc,omitempty"`
CC []string `json:"cc,omitempty"`
SendAt int64 `json:"send_at,omitempty"`
}
SendPush(ctx context.Context, payload PushPayload)
Sends a push notification.
err := client.SendPush(ctx, cdp.PushPayload{
Identifiers: cdp.Identifiers{ID: "user123"},
TransactionalMessageID: "PUSH_ALERT",
Title: "Hello",
})
Parameters:
payload:PushPayloadstruct.
Close()
Cleans up resources used by the client (e.g., stopping background routines, closing idle connections).
client.Close()
Error Handling
All methods return error if something goes wrong. If FailOnException is set to false in config (default), many errors might be logged instead of returned, unless they are validation errors caught before the API call.
if err := client.Identify(ctx, "user1", nil); err != nil {
// Handle error
}