Skip to main content

Basic Usage

This guide covers the most common operations you'll perform with the Conviso Go SDK.

Initialization​

Initialize the client once and reuse it across your application. The client is thread-safe and manages its own connection pool.

package main

import (
"context"
"log"

"github.com/codematic/opencdp-go/cdp"
)

func main() {
// Initialize with your API key
config := cdp.CDPConfig{
CDPAPIKey: "your-api-key",
// Optional: Enable debug logging in development
Debug: true,
}

client := cdp.NewClient(config)
defer client.Close()
}

Identifying Users​

Use Identify to create or update a user profile.

func identifyUser(client *cdp.Client) {
ctx := context.Background()
userID := "u_12345"

traits := map[string]interface{}{
"email": "[email protected]",
"firstName": "Jane",
"lastName": "Doe",
"plan": "enterprise",
"signup_date": "2023-10-27T10:00:00Z",
}

if err := client.Identify(ctx, userID, traits); err != nil {
log.Printf("Error identifying user: %v", err)
}
}

Tracking Events​

Use Track to record user actions.

func trackPurchase(client *cdp.Client) {
ctx := context.Background()
userID := "u_12345"

properties := map[string]interface{}{
"order_id": "ord_987",
"amount": 199.99,
"currency": "USD",
"products": []map[string]interface{}{
{"id": "p_1", "name": "Widget A", "price": 99.99},
{"id": "p_2", "name": "Widget B", "price": 100.00},
},
}

if err := client.Track(ctx, userID, "order_completed", properties); err != nil {
log.Printf("Error tracking event: %v", err)
}
}

Sending Transactional Emails​

Send emails using the SendEmail method.

func sendWelcomeEmail(client *cdp.Client) {
ctx := context.Background()

payload := cdp.EmailPayload{
To: "[email protected]",
Identifiers: cdp.Identifiers{
ID: "u_12345",
},
TransactionalMessageID: "welcome_v1",
MessageData: map[string]interface{}{
"firstName": "Jane",
"loginURL": "https://app.example.com/login",
},
}

if err := client.SendEmail(ctx, payload); err != nil {
log.Printf("Error sending email: %v", err)
}
}

Sending Push Notifications​

Send push notifications using the SendPush method.

func sendPush(client *cdp.Client) {
ctx := context.Background()

payload := cdp.PushPayload{
Identifiers: cdp.Identifiers{
ID: "u_12345",
},
TransactionalMessageID: "order_shipped",
Title: "Order Shipped!",
Body: "Your order #ord_987 is on its way.",
MessageData: map[string]interface{}{
"order_id": "ord_987",
"tracking": "xyz-123",
},
}

if err := client.SendPush(ctx, payload); err != nil {
log.Printf("Error sending push: %v", err)
}
}