Documentation
¶
Overview ¶
Package insights provides integration with Hanzo Insights (PostHog fork).
Insights is Hanzo's product analytics platform, forked from PostHog. This package enables Commerce to send events to Insights for tracking user behavior, conversions, feature usage, and more.
Usage:
client := insights.NewClient(&insights.Config{
Endpoint: "https://insights.hanzo.ai",
APIKey: "phc_...",
})
client.Capture(&insights.Event{
DistinctID: "user_123",
Event: "order_completed",
Properties: map[string]interface{}{
"order_id": "ord_abc",
"total": 99.99,
},
})
Package insights provides Gin middleware for automatic event tracking.
Index ¶
- Variables
- func ErrorMiddleware(client *Client) gin.HandlerFunc
- func Middleware(client *Client) gin.HandlerFunc
- func TrackEvent(c *gin.Context, client *Client, eventName string, ...)
- type Client
- func (c *Client) Alias(distinctID string, alias string) error
- func (c *Client) Capture(event *Event) error
- func (c *Client) CaptureOrderEvent(distinctID string, eventName string, orderID string, total float64, ...) error
- func (c *Client) CapturePageView(distinctID string, url string, title string, referrer string) error
- func (c *Client) Close() error
- func (c *Client) Flush() error
- func (c *Client) GroupIdentify(groupType string, groupKey string, properties map[string]interface{}) error
- func (c *Client) Identify(distinctID string, properties map[string]interface{}) error
- type Config
- type Event
- type IdentifyEvent
Constants ¶
This section is empty.
Variables ¶
var StandardEventNames = struct { PageView string ProductViewed string ProductAdded string ProductRemoved string CartViewed string CheckoutStarted string CheckoutStep string OrderCompleted string OrderRefunded string SignedUp string SignedIn string SignedOut string }{ PageView: "$pageview", ProductViewed: "product_viewed", ProductAdded: "product_added", ProductRemoved: "product_removed", CartViewed: "cart_viewed", CheckoutStarted: "checkout_started", CheckoutStep: "checkout_step_completed", OrderCompleted: "order_completed", OrderRefunded: "order_refunded", SignedUp: "signed_up", SignedIn: "signed_in", SignedOut: "signed_out", }
StandardEventNames defines common e-commerce event names.
Functions ¶
func ErrorMiddleware ¶
func ErrorMiddleware(client *Client) gin.HandlerFunc
ErrorMiddleware tracks errors that occur during request processing.
func Middleware ¶
func Middleware(client *Client) gin.HandlerFunc
Middleware returns a Gin middleware that tracks HTTP requests.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is the Insights API client.
func (*Client) CaptureOrderEvent ¶
func (c *Client) CaptureOrderEvent(distinctID string, eventName string, orderID string, total float64, items []map[string]interface{}) error
CaptureOrderEvent sends a commerce order event.
func (*Client) CapturePageView ¶
func (c *Client) CapturePageView(distinctID string, url string, title string, referrer string) error
CapturePageView sends a page view event.
type Config ¶
type Config struct {
// Endpoint is the Insights API endpoint (e.g., "https://insights.hanzo.ai")
Endpoint string
// APIKey is the project API key (e.g., "phc_...")
APIKey string
// BatchSize is the number of events to batch before sending (default: 100)
BatchSize int
// FlushInterval is how often to flush batched events (default: 30s)
FlushInterval time.Duration
// Timeout is the HTTP request timeout (default: 10s)
Timeout time.Duration
// Async enables asynchronous event sending (default: true)
Async bool
}
Config holds Insights client configuration.
func DefaultConfig ¶
func DefaultConfig() *Config
DefaultConfig returns a Config with sensible defaults.
type Event ¶
type Event struct {
// Event name (required)
Event string `json:"event"`
// DistinctID is the user identifier (required)
DistinctID string `json:"distinct_id"`
// Properties contains custom event data
Properties map[string]interface{} `json:"properties,omitempty"`
// Timestamp is when the event occurred (defaults to now)
Timestamp time.Time `json:"timestamp,omitempty"`
// SentAt is when the event was sent (defaults to now)
SentAt time.Time `json:"sent_at,omitempty"`
}
Event represents an analytics event to send to Insights.
type IdentifyEvent ¶
type IdentifyEvent struct {
DistinctID string `json:"distinct_id"`
Properties map[string]interface{} `json:"$set,omitempty"`
SetOnce map[string]interface{} `json:"$set_once,omitempty"`
}
IdentifyEvent represents a user identification event.