gamp

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Apr 6, 2023 License: MIT Imports: 10 Imported by: 6

README

Go buffered client for Google Analytics Measurement Protocol

CI Go Report Card GoDoc

Golang buffered client for Google Analytics (GA) Measurement Protocol - supports batch

Used by

License

Open source MIT license.

Documentation

Index

Constants

View Source
const (

	// GaHTTPS is secure endpoint for Google Analytics measurement protocol API
	GaHTTPS = "https://" + gaHost + "/"

	// GaHTTP is non secure endpoint for Google Analytics measurement protocol API
	GaHTTP = "http://" + gaHost + "/"
)

Variables

View Source
var ErrNoTrackingID = errors.New("no tracking ID")

ErrNoTrackingID is returned if GA message has no tracking ID

Functions

This section is empty.

Types

type BatchSender

type BatchSender interface {
	SendBatch(messages []Message) error
}

BatchSender sends a batch of messages to analytics provider

type Buffer

type Buffer interface {
	Queue(message Message) error
	Flush() error
}

Buffer accumulates messages and sends them on Flush() or if limit on queue depth reached

type BufferedClient

type BufferedClient struct {
	sync.Mutex
	// contains filtered or unexported fields
}

BufferedClient accumulates GA messages

func NewBufferedClient

func NewBufferedClient(endpoint string, httpClient *http.Client, onError func(err error)) *BufferedClient

NewBufferedClient creates new buffered client

func (*BufferedClient) Flush

func (api *BufferedClient) Flush() (err error)

Flush sends accumulated messages to GA

func (*BufferedClient) Queue

func (api *BufferedClient) Queue(message Message) error

Queue adds GA message to buffer

func (*BufferedClient) QueueDepth

func (api *BufferedClient) QueueDepth() int

QueueDepth huw many messages accumulated

type Common

type Common struct {
	TrackingID    string `key:"tid" required:"true"`
	ClientID      string `key:"cid"`
	UserID        string `key:"uid"`
	UserLanguage  string `key:"ul"`
	UserAgent     string `key:"ua"`
	DataSource    string `key:"ds"` // https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#ds
	ApplicationID string `key:"aid"`
}

Common properties of GA message

func (*Common) GetTrackingID

func (c *Common) GetTrackingID() string

GetTrackingID returns tracking ID of GA message

func (*Common) SetTrackingID

func (c *Common) SetTrackingID(v string)

GetTrackingID returns tracking ID of GA message

type Event

type Event struct {
	Common
	Category string `key:"ec" required:"true"`
	Action   string `key:"ea" required:"true"`
	Label    string `key:"el"`
	Value    uint   `key:"ev"`
}

Event is GA message of 'event' type

func NewEvent

func NewEvent(category, action string, common Common) *Event

NewEvent creates new event

func NewEventWithLabel

func NewEventWithLabel(category, action, label string, common Common) *Event

NewEventWithLabel creates new event with label

func (*Event) String

func (e *Event) String() string

String returns message as URL encoded string

func (Event) Validate

func (e Event) Validate() error

Validate is checking message for validity

func (Event) Write

func (e Event) Write(w io.Writer) (n int, err error)

Write serializes event message

type Exception

type Exception struct {
	Common
	Description string `key:"exd"`
	IsFatal     bool   `key:"exf"`
}

Exception message

func NewException

func NewException(description string, isFatal bool) *Exception

NewException creates new exception message

func (*Exception) String

func (m *Exception) String() string

func (Exception) Write

func (m Exception) Write(w io.Writer) (n int, err error)

Write serializes exception message

type Message

type Message interface {
	GetTrackingID() string
	SetTrackingID(string)
	fmt.Stringer
	//
	Write(w io.Writer) (n int, err error)
}

Message interface should be implemented by all GA messages

type Pageview

type Pageview struct {
	Common
	//DocumentLocation string `key:"dl"`
	DocumentHost  string `key:"dh"`
	DocumentPath  string `key:"dp"`
	DocumentTitle string `key:"dt"`
}

Pageview message

func NewPageviewWithDocumentHost

func NewPageviewWithDocumentHost(documentHost, documentPath, documentTitle string) *Pageview

NewPageviewWithDocumentHost creates new pageview message with document host value

func (*Pageview) String

func (m *Pageview) String() string

String returns message as URL encoded string

func (Pageview) Write

func (m Pageview) Write(w io.Writer) (n int, err error)

Write serializes page view message

type Sender

type Sender interface {
	Send(message Message) error
}

Sender sends analytic messages

type Timing

type Timing struct {
	Common
	ServerResponseTime uint
}

Timing is GA message of 'timing' type

func NewTiming

func NewTiming(serverResponseTime time.Duration) *Timing

NewTiming creates new timing

func (*Timing) String

func (t *Timing) String() string

String returns message as URL encoded string

func (Timing) Write

func (t Timing) Write(w io.Writer) (n int, err error)

Write serializes timing message

Jump to

Keyboard shortcuts

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