gameasure

package module
v0.0.0-...-a95d265 Latest Latest
Warning

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

Go to latest
Published: Jul 6, 2016 License: MPL-2.0 Imports: 7 Imported by: 0

README

gameasure

GoDoc

Google Analytics is actually a pretty powerful event tracking system allowing you to track events, timing tasks, exceptions along with the traditional pageviews. We use the Google Analytics Measurement Protocol to send events to analytics.

Usage

Check out the GoDocs but usage is simple. Create a new GA object with gameasure.New passing the TrackingID and an anonymous ClientID which can be any value. Then use the object to send events.

ga := gameasure.New("UA-XXXXXXX-X", "1231231234")
ga.Event(gameasure.Event{
    Category: "Food",
    Action:   "Eat",
    Label:    "Invoking Food to Eat",
})

Sending Timing Events:

ga := &gameasure.New("UA-XXXXXXX-X", "1231231234")
t := UserTiming{
        Category: "Subscription",
        Variable: "Subscribe",
        Label: "Stripe"
}
t.Begin()
defer func() {
        t.End()
        go ga.UserTiming(t)
}()

// Do Stuff

Use it as a middleware to log API requests to Google Analytics:

r := mux.NewRouter()
r.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
  w.Write([]byte("OK"))
})

trackerIds := map[string]string{
    "api.acksin.com":  "UA-XXXXXXX-1",
    "default":         "UA-XXXXXXX-2",
}

http.ListenAndServe(":8080", gameasure.NewGAHandler(r, trackerIds))

Other supported events are:

  • UserTiming
  • Pageview
  • Exception

Project of Acksin

This is a project of Acksin. Acksin provides tools to make your infrastructure more efficient and cost effective. Check out our spot instance bidding tool ParkingSpot.

License

Copyright (C) 2016 Acksin hey@acksin.com

This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Event

type Event struct {
	// Category is the event category
	Category string `ga:"ec"`
	// Action is the event action
	Action string `ga:"ea"`
	// Label is the event label
	Label string `ga:"el"`
	// Value event value
	Value string `ga:"ev"`
}

Event sends an event hit type.

type Exception

type Exception struct {
	// Description is the exception description. i.e. IOException
	Description string `ga:"exd"`
	// Fatal is if the exception was fatal.
	Fatal bool `ga:"exf"`
}

Exception sends an exception hit type.

type GA

type GA struct {
	// XX-XXXXXXX-X
	TrackingID string `ga:"tid"`
	// ClientID is the Anonymous ID
	ClientID string `ga:"cid"`
	// contains filtered or unexported fields
}

GA is how we send events to Google Analytics

func New

func New(trackingID, clientID string) *GA

New creates a new GA object with the trackingID and clientID.

func (*GA) Event

func (g *GA) Event(e Event) error

Event sends events to Google Analytics

func (*GA) Exception

func (g *GA) Exception(e Exception) error

Exception sends exceptions to Google Analytics

func (*GA) Pageview

func (g *GA) Pageview(p Pageview) error

Pageview sends pageviews to Google Analytics

func (*GA) UserTiming

func (g *GA) UserTiming(e UserTiming) error

UserTiming sends user timings to Google Analytics

type GAHandler

type GAHandler struct {
	TrackerIDs map[string]string
	// TODO
	IgnorePaths []string
	// contains filtered or unexported fields
}

GAHandler is a middleware which logs backend requests to Google Analytics.

trackerIds := map[string]string{
	"api.acksin.com":  "UA-XXXXXXX-1",
	"default":         "UA-XXXXXXX-2",
}

gameasure.NewGAHandler(r, trackerIds)

func NewGAHandler

func NewGAHandler(handler http.Handler, trackerIds map[string]string) *GAHandler

NewGAHandler creates a new GAHandler. Pass in a map of the Host to TrackingID. Pass a default key for a default trackingid

trackerIds := map[string]string{
	"api.acksin.com":  "UA-XXXXXXX-1",
	"default":         "UA-XXXXXXX-2",
}

func (*GAHandler) ServeHTTP

func (s *GAHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP shows the requested page but logs the page request in goroutine to Google Analytics.

type Pageview

type Pageview struct {
	// DocumentHost is the Document hostname example.com
	DocumentHost string `ga:"dh"`
	// Page is the page path. Ex. /foo/bar
	Page string `ga:"dp"`
	// Title is the page title. Ex The foobar page
	Title string `ga:"dt"`
}

Pageview sends events for pageviews.

type UserTiming

type UserTiming struct {
	// Category is the timing category. e.g. jsonLoader
	Category string `ga:"utc"`
	// Variable is the timing variable. e.g. load
	Variable string `ga:"utv"`
	// Time is the time it took in milliseconds.
	Time time.Duration `ga:"utt"`
	// Label is the timing label. e.g jQuery
	Label string `ga:"utl"`
	// contains filtered or unexported fields
}

UserTiming sends a timing hit type.

func (*UserTiming) Begin

func (u *UserTiming) Begin()

Begin starts the timer.

func (*UserTiming) End

func (u *UserTiming) End()

Calculate the timing

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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