Documentation
¶
Index ¶
Constants ¶
const ( // DefaultEngagementTimeMS is the default engagement time in milliseconds DefaultEngagementTimeMS = "100" // SessionIDParam is the parameter name for the session ID SessionIDParam = "session_id" // EngagementTimeParam is the parameter name for the engagement time in milliseconds EngagementTimeParam = "engagement_time_msec" // MaxEventsPerRequest is the maximum number of events per request MaxEventsPerRequest = 25 // URLFormat is the format for the URL URLFormat = "%s?measurement_id=%s&api_secret=%s" // ContentTypeHeader is the header for the content type ContentTypeHeader = "Content-Type" // ContentTypeJSON is the content type for JSON ContentTypeJSON = "application/json" )
const (
// ContextKey is the key middleware uses to store the Google Analytics session in the echo context
ContextKey = "ga4m.session"
)
Variables ¶
var EmptySession = Session{}
EmptySession is an empty Google Analytics session
Functions ¶
func GoogleAnalyticsCookieEchoMiddleware ¶
func GoogleAnalyticsCookieEchoMiddleware() echo.MiddlewareFunc
GoogleAnalyticsCookieEchoMiddleware extracts user Google Analytics session data from cookies and stores it in the context for later use
Types ¶
type AnalyticsClient ¶
type AnalyticsClient struct { MeasurementID string APISecret string Endpoint string DebugEndpoint string HTTPClient HTTPClient }
AnalyticsClient is the client for sending events to Google Analytics
func NewClient ¶
func NewClient(measurementID, apiSecret string) *AnalyticsClient
NewClient creates a new AnalyticsClient with the provided measurement ID and API secret
func (*AnalyticsClient) SendEvent ¶
func (c *AnalyticsClient) SendEvent(session Session, eventName string, params map[string]string, opts ...SendEventOption) error
SendEvent sends a single event to Google Analytics.
func (*AnalyticsClient) SendEvents ¶
func (c *AnalyticsClient) SendEvents(session Session, events []EventParams, opts ...SendEventOption) error
SendEvents sends multiple events in a single batch request to Google Analytics.
func (*AnalyticsClient) SetHTTPClient ¶
func (c *AnalyticsClient) SetHTTPClient(client HTTPClient)
SetHTTPClient allows setting a custom HTTP client
type AnalyticsEvent ¶
type AnalyticsEvent struct { ClientID string `json:"client_id"` Events []EventParams `json:"events"` UserID string `json:"user_id,omitempty"` TimestampMicros int64 `json:"timestamp_micros,omitempty"` }
AnalyticsEvent represents the payload structure for GA4 events.
type EventParams ¶
type EventParams struct { Name string `json:"name"` Params map[string]string `json:"params,omitempty"` TimestampMicros int64 `json:"timestamp_micros,omitempty"` }
EventParams represents parameters for a GA4 event.
type HTTPClient ¶
HTTPClient interface allows for mocking of http.Client in tests
type SendEventOption ¶
type SendEventOption func(*sendEventOptions)
SendEventOption allows for optional parameters when sending events.
func WithContext ¶
func WithContext(ctx context.Context) SendEventOption
WithContext sets a custom context for the request.
func WithDebug ¶
func WithDebug(debug bool) SendEventOption
WithDebug enables or disables debug mode.
func WithSessionID ¶
func WithSessionID(sessionID string) SendEventOption
WithSessionID sets the session ID for the event.
func WithTimestamp ¶
func WithTimestamp(timestamp time.Time) SendEventOption
WithTimestamp sets a custom timestamp for the event.
func WithUserID ¶
func WithUserID(userID string) SendEventOption
WithUserID sets the user ID for the event.
type Session ¶
type Session struct { // Client Cookie Data ClientID string // The client ID from _ga cookie. ClientVersion string // The version from _ga cookie (e.g., "1") FirstVisit time.Time // First visit timestamp. // Session Cookie Data SessionCount int // Number of sessions. LastSession time.Time // Last session timestamp. SessionID string // Unique identifier for the current session SessionVersion string // The version from _ga_* cookie (e.g., "1") IsEngaged bool // Indicates if the user is actively engaged HitCount int // Number of hits/interactions in the current session IsFirstSession bool // Indicates if this is the user's first session IsNewSession bool // Indicates if this is a new session }
Session represents the Google Analytics session tracking data for a user.
func LatestSessions ¶
LatestSessions compares Google Analytics sessions and returns the latest one
func ParseSessionFromEchoContext ¶
func ParseSessionFromEchoContext(e echo.Context) Session
ParseSessionFromEchoContext returns the Google Analytics tracking data from an echo.Context
func ParseSessionFromRequest ¶
ParseSessionFromRequest parses the Google Analytics cookies from an HTTP request and returns a Session.