Skip to content
LogZuno

Go event tracking

Send events to LogZuno from Go, with code that is tested against the API.

This example sends one event to LogZuno's ingestion API from Go. It is the exact file our snippet tests send to the API before the docs are published.

Requirements

Go 1.18 or later (the example uses any). Standard library only.

Send an event

// Go, standard library only.
package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io"
	"net/http"
	"os"
	"time"
)

func main() {
	endpoint := os.Getenv("LOGZUNO_ENDPOINT")
	if endpoint == "" {
		endpoint = "https://api.logzuno.app/v1"
	}

	body, _ := json.Marshal(map[string]any{
		"channel": "signups",
		"event":   "User signed up",
		"user_id": "user-123",
		"icon":    "🎉",
		"notify":  true,
		"tags":    map[string]string{"plan": "growth", "source": "website"},
	})

	req, _ := http.NewRequest(http.MethodPost, endpoint+"/log", bytes.NewReader(body))
	req.Header.Set("Authorization", "Bearer "+os.Getenv("LOGZUNO_API_KEY"))
	req.Header.Set("Content-Type", "application/json")

	client := &http.Client{Timeout: 10 * time.Second}
	res, err := client.Do(req)
	if err != nil {
		fmt.Println("request failed:", err)
		os.Exit(1)
	}
	defer res.Body.Close()

	reply, _ := io.ReadAll(res.Body)
	fmt.Println(res.StatusCode, string(reply))
}

Export LOGZUNO_API_KEY and run go run logzuno.go. LogZuno answers 202 and the event appears in your project's live feed within seconds. Keep the key in an environment variable or your secrets manager, not in source control.

Notes for Go

Set a timeout. The default http.Client has none; the example uses 10 seconds so a network problem can't hang your program.

Reuse the client. Create one http.Client and share it: it keeps connections open between requests.

Off the hot path. Send from a goroutine fed by a buffered channel and flush in batches to /v1/batch, so handlers never wait on analytics.

What the request contains

The body is the same in every language: a channel and an event, and optionally user_id, group_id, description, icon, notify and tags. All fields, limits and response codes are in sending events and the ingestion API reference.