Quick Start
Get up and running with the Conviso Go SDK in minutes.
Initialize the Client
First, import and initialize the Conviso client with your API key:
package main
import (
"context"
"log"
"github.com/codematic/opencdp-go/cdp"
)
func main() {
client := cdp.NewClient(cdp.CDPConfig{
CDPAPIKey: "your-cdp-api-key",
})
defer client.Close()
// Use client...
}
Contact your Codematic administrator to obtain your Conviso API key. Keep this key secure and never commit it to version control.
Identify a User
Identify a user and set their attributes:
ctx := context.Background()
traits := map[string]interface{}{
"email": "[email protected]",
"firstName": "John",
"lastName": "Doe",
"plan": "premium",
"created_at": "2023-01-01T12:00:00Z",
}
if err := client.Identify(ctx, "user123", traits); err != nil {
log.Printf("Failed to identify user: %v", err)
}
The first parameter is the context, the second is the user identifier (typically your internal user ID), and the third is a map containing any custom attributes you want to associate with the user.
The Conviso automatically recognizes firstName/first_name, lastName/last_name, and email as special person attributes.
Track an Event
Track a custom event for a user:
props := map[string]interface{}{
"amount": 99.99,
"currency": "USD",
"item_id": "prod-123",
"item_name": "Premium Subscription",
}
if err := client.Track(ctx, "user123", "purchase_completed", props); err != nil {
log.Printf("Failed to track event: %v", err)
}
Events help you understand user behavior and trigger automated campaigns.
Send a Transactional Email
Send an email using a predefined template:
err := client.SendEmail(ctx, cdp.EmailPayload{
To: "[email protected]",
Identifiers: cdp.Identifiers{
ID: "user123",
},
TransactionalMessageID: "WELCOME_EMAIL",
MessageData: map[string]interface{}{
"name": "John",
"activation_link": "https://app.example.com/activate/xyz",
},
})
Or send a raw HTML email:
err := client.SendEmail(ctx, cdp.EmailPayload{
To: "[email protected]",
Identifiers: cdp.Identifiers{
ID: "user123",
},
From: "[email protected]",
Subject: "Welcome to Our Service",
Body: "<h1>Welcome!</h1><p>Thank you for joining us.</p>",
})
Send a Push Notification
Send a push notification to a user:
err := client.SendPush(ctx, cdp.PushPayload{
Identifiers: cdp.Identifiers{
ID: "user123",
},
TransactionalMessageID: "WELCOME_PUSH",
Title: "Welcome!",
Body: "Thank you for joining us!",
MessageData: map[string]interface{}{
"name": "John",
"deep_link": "/home",
},
})
Users must have a registered device to receive push notifications.
Complete Example
Here's a complete example of using the SDK:
package main
import (
"context"
"fmt"
"os"
"github.com/codematic/opencdp-go/cdp"
)
func main() {
apiKey := os.Getenv("CDP_API_KEY")
if apiKey == "" {
fmt.Println("Please set CDP_API_KEY")
return
}
client := cdp.NewClient(cdp.CDPConfig{
CDPAPIKey: apiKey,
Debug: true,
})
defer client.Close()
ctx := context.Background()
userId := "user_123456"
// 1. Identify
err := client.Identify(ctx, userId, map[string]interface{}{
"email": "[email protected]",
"name": "John Doe",
})
if err != nil {
fmt.Printf("Identify failed: %v\n", err)
}
// 2. Track
err = client.Track(ctx, userId, "signup", map[string]interface{}{
"source": "web",
})
if err != nil {
fmt.Printf("Track failed: %v\n", err)
}
}
Next Steps
- Learn about Configuration Options
- Explore the API Reference