This is an example of a simple console application that pulls pageview data from Google Analytics.
To query Google Analytics data, you'll need to enable the Google Analytics Reporting API in your Google Cloud Platform project.
- Create a service account in Google Cloud Platform console for your project.
- Assign the service account no permissions/roles.
- Click "Create Key" to create a private key and save it in JSON format.
- In Google Analytics, open Admin > View > View User Management and add the email address of the service account you just created (it will have an email like
[name]@[project ID].iam.gserviceaccount.com
. - Grant the user only "Read & Analyze" permissions.
- In Google Analytics, open Admin > View > View Settings
- Save the View ID displayed
go run main.go -viewID 123456789 -keyFile google-analytics-viewer-key.json