https://github.com/mjpitz/go-ga
Send data to Google Analytics from Go
https://github.com/mjpitz/go-ga
golang golang-library google-analytics
Last synced: over 1 year ago
JSON representation
Send data to Google Analytics from Go
- Host: GitHub
- URL: https://github.com/mjpitz/go-ga
- Owner: mjpitz
- License: mit
- Created: 2020-10-16T13:49:42.000Z (almost 6 years ago)
- Default Branch: main
- Last Pushed: 2022-11-11T14:57:15.000Z (over 3 years ago)
- Last Synced: 2025-03-16T18:42:21.125Z (over 1 year ago)
- Topics: golang, golang-library, google-analytics
- Language: Go
- Homepage:
- Size: 61.5 KB
- Stars: 19
- Watchers: 1
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# go-ga
Send data to Google Analytics from Go.
This library implements the [measurement protocol] and supports a majority of the parameters.
[](https://github.com/mjpitz/go-ga/actions?query=workflow%3Abranch)
[](https://github.com/mjpitz/go-ga/actions?query=workflow%3Atag)

[measurement protocol]: https://developers.google.com/analytics/devguides/collection/protocol/v1/reference
## 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.
[parameter reference]: https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters
- [x] General
- [x] User
- [x] Session
- [x] Traffic Sources
- [x] System Info
- [x] Hit
- [x] Content Information
- [x] Document Location URL
- [x] Document Host Name
- [x] Document Path
- [x] Document Title
- [x] Screen Name
- [x] Content Group
- [x] Link ID
- [x] Apps
- [x] Events
- [x] E-Commerce
- [ ] Enhanced E-Commerce
- [x] Social Interactions
- [x] Timing
- [x] Exceptions
- [x] Custom Dimensions / Metrics
## Examples
### Sending data to Google Analytics
```go
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
```go
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`.