Skip to content

Getting Started

Install

bash
go get github.com/foomo/sesamy-go

Requires Go 1.22+.

Minimal server-side collect endpoint

This exposes two HTTP handlers that accept GA4 traffic and forward it to a downstream tagging server.

go
package main

import (
	"log"
	"net/http"

	"github.com/foomo/sesamy-go/pkg/collect"
	"go.uber.org/zap"
)

func main() {
	l, _ := zap.NewProduction()

	c, err := collect.New(l,
		collect.WithTagging("https://sgtm.example.com"),
	)
	if err != nil {
		log.Fatal(err)
	}

	mux := http.NewServeMux()
	mux.HandleFunc("/g/collect", c.GTagHTTPHandler)   // gtag.js
	mux.HandleFunc("/mp/collect", c.MPv2HTTPHandler)  // Measurement Protocol v2

	log.Fatal(http.ListenAndServe(":8080", mux))
}

Point gtag.js at https://your-domain/g/collect (via the GTM container generated by sesamy-cli) and events will start flowing through.

Add middleware

Middleware composes around the per-protocol handler. Both protocols share the same shape: Middleware = func(next Handler) Handler.

go
import (
	gtaghttp "github.com/foomo/sesamy-go/pkg/http/gtag"
	mpv2http "github.com/foomo/sesamy-go/pkg/http/mpv2"
)

c, _ := collect.New(l,
	collect.WithTagging("https://sgtm.example.com"),
	collect.WithGTagHTTPMiddlewares(
		gtaghttp.MiddlewareLogger,
		gtaghttp.MiddlewareUserID("uid"),
		gtaghttp.MiddlewareWithTimeout(2 * time.Second),
	),
	collect.WithMPv2HTTPMiddlewares(
		mpv2http.MiddlewareLogger,
		mpv2http.MiddlewareClientID,
		mpv2http.MiddlewareIPOverride,
		mpv2http.MiddlewareUserAgent,
		mpv2http.MiddlewarePageLocation,
		mpv2http.MiddlewareEngagementTime,
	),
)

Full middleware list: reference/http.

Send events from Go directly

If you want to emit events from a Go backend (server-to-GA, no browser), use the client layer:

go
import (
	"github.com/foomo/sesamy-go/pkg/client"
	"github.com/foomo/sesamy-go/pkg/sesamy"
)

c := client.NewMPv2(l, "https://www.google-analytics.com",
	client.MPv2WithAPISecret("xxx"),
	client.MPv2WithMeasurementID("G-XXXXXXX"),
)

err := c.Collect(r,
	sesamy.NewEvent(sesamy.EventNamePageView, map[string]any{
		"page_location": "/home",
	}).AnyEvent(),
)

See reference/client.

Next steps