insights

package
v1.35.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 24, 2026 License: MIT Imports: 8 Imported by: 0

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

Constants

This section is empty.

Variables

View Source
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.

func TrackEvent

func TrackEvent(c *gin.Context, client *Client, eventName string, properties map[string]interface{})

TrackEvent is a helper to track custom events from handlers.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client is the Insights API client.

func NewClient

func NewClient(config *Config) *Client

NewClient creates a new Insights client.

func (*Client) Alias

func (c *Client) Alias(distinctID string, alias string) error

Alias links two distinct IDs (e.g., anonymous → authenticated).

func (*Client) Capture

func (c *Client) Capture(event *Event) error

Capture sends an event to Insights.

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.

func (*Client) Close

func (c *Client) Close() error

Close gracefully shuts down the client.

func (*Client) Flush

func (c *Client) Flush() error

Flush sends all queued events immediately.

func (*Client) GroupIdentify

func (c *Client) GroupIdentify(groupType string, groupKey string, properties map[string]interface{}) error

GroupIdentify associates a user with a group (e.g., organization).

func (*Client) Identify

func (c *Client) Identify(distinctID string, properties map[string]interface{}) error

Identify sends user identification data.

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL