https://github.com/sudo-suhas/example-analyticsreporting
Example for Google Analytics Reporting API v4 in Golang
https://github.com/sudo-suhas/example-analyticsreporting
example-code google-analytics-api google-api
Last synced: 3 months ago
JSON representation
Example for Google Analytics Reporting API v4 in Golang
- Host: GitHub
- URL: https://github.com/sudo-suhas/example-analyticsreporting
- Owner: sudo-suhas
- License: mit
- Created: 2017-06-11T03:03:17.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2017-10-07T03:00:01.000Z (over 7 years ago)
- Last Synced: 2025-03-20T00:38:43.418Z (3 months ago)
- Topics: example-code, google-analytics-api, google-api
- Language: Go
- Homepage:
- Size: 8.79 KB
- Stars: 20
- Watchers: 1
- Forks: 7
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# example-analyticsreporting
Example for Google Analytics Reporting API v4 in golang using the [official client](https://github.com/google/google-api-go-client).This example was written using the official examples for [java](https://developers.google.com/analytics/devguides/reporting/core/v4/quickstart/service-java), [python](https://developers.google.com/analytics/devguides/reporting/core/v4/quickstart/service-py)
## How to run
* Enable the API - Using steps described in https://developers.google.com/analytics/devguides/reporting/core/v4/quickstart/service-java,
create a private key `client_secrets.json` and download it to your file system.
Also add the new service account to the Google Analytics Account with [Read & Analyze](https://support.google.com/analytics/answer/2884495) permission.
* Clone the repo and install dependencies. [Glide](https://github.com/Masterminds/glide) is preferred.
```
$ mkdir -p $GOPATH/src/github.com/sudo-suhas && cd $_
$ git clone https://github.com/sudo-suhas/example-analyticsreporting.git
$ cd example-analyticsreporting# preferred method
$ glide install# or use `go get`
$ go get .```
* Build the executable binary.
```
# This should generate an executable binary in the current folder
# Example example-analyticsreporting.exe on windows
$ go build .```
* You need to pass the location of the `client_secrets.json` file
and the Google analytics view ID from the command line.
You can use the [Account Explorer](https://ga-dev-tools.appspot.com/account-explorer/) to find a View ID.
Additionally, you can pass the flag `--debug` for verbose logging.Usage:
```
$ ./example-analyticsreporting.exe --help
usage: hello_analytics.exe --keyfile=KEYFILE --view-id=VIEW-ID []Flags:
--help Show context-sensitive help (also try --help-long and
--help-man).
-d, --debug Enable debug mode.
-k, --keyfile=KEYFILE Path to JSON key file.
-v, --view-id=VIEW-ID Google Analytics View ID.$ ./example-analyticsreporting.exe --keyfile=E:\creds\client_secrets.json -view-id=299792458 --debug
```
## File Structure
* `hello_analytics.go` - This is the main file. It does the following:
- Create a Google Analytics Reporting API v4 service client
- Execute a GET analytics report request
- Parse and print the response using [`logrus`](https://github.com/Sirupsen/logrus/blob/master/logrus.go).
* `debug.go` - This is copied from https://github.com/google/google-api-go-client/blob/master/examples/debug.go.
It is used to log the HTTP request and response to `os.Stdout` in debug mode.
* `util.go` - This has a simple utility function for tracking function execution time in debug mode.