Skip to content

mjpitz/go-ga

Repository files navigation

go-ga

Send data to Google Analytics from Go. This library implements the measurement protocol and supports a majority of the parameters.

branch workflow tag workflow

Features

  • Sending data to Google Analytics using GET or POST methods
  • Encoding payloads to URLs for embedding
  • Support for decoding url parameters into a struct using tags url:"name"
  • A simple containerized beacon for reporting on application usage

Parameter Support

The majority of the API has been implemented. There is still work to be done around some array and map style data types. The list below tries to document the current parameter support. For more information, see the parameter reference documentation.

  • General
  • User
  • Session
  • Traffic Sources
  • System Info
  • Hit
  • Content Information
    • Document Location URL
    • Document Host Name
    • Document Path
    • Document Title
    • Screen Name
    • Content Group
    • Link ID
  • Apps
  • Events
  • E-Commerce
  • Enhanced E-Commerce
  • Social Interactions
  • Timing
  • Exceptions
  • Custom Dimensions / Metrics

Examples

Sending data to Google Analytics

package main

import (
    "log"
    "time"

    "github.com/mjpitz/go-ga/client/v1"
    "github.com/mjpitz/go-ga/client/v1/gatypes"
)

func main() {
    client := v1.NewClient("UA-XXXXXX-1", "customUserAgent")
    ping := &gatypes.Payload{
        HitType: "event",
        NonInteractionHit: true,
        DisableAdvertisingPersonalization: true,
        Event: gatypes.Event{
            EventCategory: "beacon",
            EventAction: "heartbeat",
        },
    }
    
    for {
        if err := client.SendPost(ping); err != nil {
            log.Fatal(err)
        }
        time.Sleep(time.Minute)
    }
}

Encoding a payload for embed

package main

import (
    "github.com/mjpitz/go-ga/client/v1"
    "github.com/mjpitz/go-ga/client/v1/gatypes"
)

func main() {
    ping := &gatypes.Payload{
        TrackingID: "UA-XXXXXX-1",
        HitType: "event",
        Event: gatypes.Event{
            EventCategory: "email",
            EventAction: "open",
        },
    }
    
    url, err := v1.Encode(ping)
    // do something with url
}

Changelog

v0.1.0

Field TransactionID was deleted from, Transaction and Item structs, now it is available at Payload struct.

Structs ScreenView and PageView was merged into Views.