Skip to content

remarkablemark/googleapis-analytics-demo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

googleapis-analytics-demo

GitHub last commit

Demo of the Google Analytics API.

Prerequisites

Install

Clone the repository:

git clone https://github.com/remarkablemark/googleapis-analytics-demo.git
cd googleapis-analytics-demo

Install the dependencies:

npm install

Authentication

Login to Google Cloud Platform. Agree to the Terms of Service if you haven't already.

Create a New Project with a Project name and Project ID.

Enable the Google Analytics API.

Go to Credentials and create a Service account credential, which enables server-to-server, app-level authentication using robot accounts. Fill in the Service account name and Service account ID and click Done.

In Credentials, look for Service Accounts and click the newly created Email. Click Keys > Add Key > Create new key > Key type > JSON > Create. The private key, which allows access to your cloud resources, will be saved to your computer.

Move the key to the project directory:

mv path/to/project-id-abcdef123456.json auth.json

Only private_key and client_email are necessary for JWT authorization. See auth.example.json:

{
  "private_key": "-----BEGIN PRIVATE KEY-----\nabc123\n-----END PRIVATE KEY-----\n",
  "client_email": "service-account-name@project-id-123456.iam.gserviceaccount.com"
}

Alternatively, the keys can be set in .env. See .env.example:

CLIENT_EMAIL="service-account-name@project-id-123456.iam.gserviceaccount.com"
PRIVATE_KEY="-----BEGIN PRIVATE KEY-----\nabc123\n-----END PRIVATE KEY-----\n"

Open Google Analytics and go to Admin > View Settings and copy the View ID.

Set VIEW_ID in .env. See .env.example:

echo 'VIEW_ID=12345678' >> .env

Go back to Admin and go to View User Management. In View permissions, click + to Add users and add the service account email client_email.

Run

npm start

Prints the Google Analytics real-time data for a view (profile). See Real Time Reporting API.

License

MIT