package module
Version: v0.0.0-...-827eb0c Latest Latest

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

Go to latest
Published: Mar 4, 2020 License: MIT Imports: 11 Imported by: 0


##Google analytics Data pull

Lightweight Golang library for pulling Google Analytics API data. Built for use with Core Reporting API (v3):

Is being used for BAU report generation and collation. Pull requests welcome!

Codeship Status for vly/go-gadata


In order to authenticate this library for use with your Google Analytics account, an oauth2 token needs to be generated. For a new project login to Google Developers Console and Create Project. Add Analytics API to list of APIs, create a new Client ID and download it in JSON format. Place the client_secret.json in the root of your application.


Example single request flow:

import (

func main() {
    // initialise GAData
    gaTest := new(GAData)
		// initialise instance incl. authentication
    // build a basic GA query, replace your ga ID
    testRequest := GaRequest{"ga:43047246", // GA id
		                         "2014-01-01",  // start date
		                         "2014-01-02",  // end date 
		                         "ga:visits",   // metrics 
		                         "ga:day",      // dimensions
		                         "",            // filters
		                         "",            // segments
		                         "",            // sort
		                         100}           // results no.
    // launch data
		result := gaTemp.GetData(1, &testRequest)
		fmt.Printf("results: %s\n", result)

Example multiple requests flow. Returns a sorted slice (array) of results...

import (

func main() {
    // initialise GAData
    gaTest := new(gadata.GAData)
		// initialise instance incl. authentication
    // build a basic GA query, replace your ga ID
    for i := 0; i < 5; i++ {
		testRequests = append(testRequests, &GaRequest{"ga:43047246",
			"2014-01-0" + strconv.Itoa(i+1),
			"2014-01-0" + strconv.Itoa(i+2),
	if results, err := gaTemp.BatchGet(testRequests); err == nil {
		for a, b := range results {
			log.Printf("results: %d: %s", a, b)


Unit tests are included with this library, use go test ./... to run through the set provided.



  • Fix broken import


  • Implemented batch processing
  • New request period segmentation functionality
  • Cleaner error reporting and resolution suggestions


  • Initial release




View Source
const (
	StdEndpoint string = "" // standard endpoint
	Limit       int    = 5                                                 // max requests / sec guard

Base constants

View Source
const (
	BaseEndpoint string = ""
	End          string = "unsampledReports"


This section is empty.


This section is empty.


type CleanResponse

type CleanResponse struct {
	Columns []struct {
		Name  string `json:"name"`
		CType string `json:"columnType"`
		DType string `json:"dataType"`
	} `json:"columnHeaders"`
	Total map[string]string `json:"totalsForAllResults"`
	Rows  [][]string        `json:"rows"`

Processed GA response

type GAData

type GAData struct {
	Auth     *utils.OauthData
	Request  *GaRequest
	Response *GaResponse

GAData is the primary Google Analytics API pull structure

func (*GAData) BatchGet

func (g *GAData) BatchGet(requests []*GaRequest) (responses []*CleanResponse, err error)

BatchGet runs all queries in parellel and returns the results (or times out)

func (*GAData) GetData

func (g *GAData) GetData(key int, request *GaRequest) *GaResponse

GetData queries GA API endpoint, returns response

func (*GAData) Init

func (g *GAData) Init()

Initialise the GAData connection, ready to make a new request

type GaRequest

type GaRequest struct {
	Id         string `json:"ids"`
	StartDate  string `json:"start-date"`
	EndDate    string `json:"end-date"`
	Metrics    string `json:"metrics"`
	Dimensions string `json:"dimensions"`
	Filters    string `json:"filters"`
	Segments   string `json:"segment"`
	Sort       string `json:"sort"`
	MaxResults int    `json:"max-results"`
	Attempts   int

GaRequest is the Google Analytics request structure

func (*GaRequest) ToURLValues

func (a *GaRequest) ToURLValues() (out url.Values)

ToURLValues converts struct to url.Values struct

type GaResponse

type GaResponse struct {
	Data string
	Pos  int

Initial returned response

func (GaResponse) Process

func (rawResponse GaResponse) Process() (data CleanResponse, ok bool)

type URData

type URData struct {
	// contains filtered or unexported fields

type UnsampledRequest

type UnsampledRequest struct {
	// contains filtered or unexported fields


Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL