npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ga-analytics

v0.0.7

Published

Access Google Analytics reports via the command-line or node module

Downloads

3,108

Readme

ga-analytics

This node module allows direct access to your Google Analytics reports.

To use the module you will need a Google Service Account with these pieces of information (screenshots are given to show where they can be found)

See Google's Dimensions & Metrics Reference and take a look at some of the examples below to get started.

Setup

This module requires a Google Service Account connected to your GA account.

Creating a Service Account

  1. Visit the Google Developer Console
  2. Click APIs and enabled Analytics API
  3. Click credentials
  4. Create new client ID
  5. Create a service account

Incorperate service account.

After you create a service account you should recieve a .p12 file. Convert the .p12 (key and certificate in one) to an ascii formatted .pem which can be read as text. Use this command:

$ openssl pkcs12 -in *.p12 -out google-services-private-key.pem -nodes -clcerts

The password is always "notasecret"

Authorize your service account for Google Analytics

  1. Access Google Analytics and click Admin
  2. Click on "User Management", add your google service email with read permissions

Command-Line

$ npm install ga-analytics -g
$ ga-analytics --help

Usage: ga-analytics [options]

Options:

  -h, --help                     output usage information
  -V, --version                  output the version number
  --client-id [clientid]         OAuth client id. Can be omitted if the GOOGLEAPI_CLIENTID environment variable is set.
  --service-email [email]        Google Service Account Email. Can be omitted if the GOOGLEAPI_EMAIL environment variable is set.
  --key [path_to_key]            Path to Service Account private key. Can be omitted if the GOOGLEAPI_KEY environment variable is set.
  --ids [ids]                    Google Analytics view (profile). Can be omitted if the GOOGLEAPI_ANALYTICS_TABLE environment variable is set.
  -s, --start-date [start-date]  Start Date (2010-01-01). Defaults to one month ago
  -e, --end-date [end_date]      End Date (2010-01-15). Defaults to one month after start date.
  -m, --metrics [metric]         Metric, gs:sessions by default
  -d, --dimensions [dimension]   Dimension (ga:source,ga:keyword)
  -s, --sort [sort]              Sort (-ga:sessions,source)
  -f, --filter [filter]          Filter (ga:medium==organic)
  --max [num]                    Define the max amount of results
  --json                         Show entire JSON output rather than formatted results.
  
$ export GOOGLEAPI_CLIENTID="******.apps.googleusercontent.com"
$ export GOOGLEAPI_EMAIL="*****@developer.gserviceaccount.com"
$ export GOOGLEAPI_KEY="/path/to/google-services.pem"
$ export GOOGLEAPI_ANALYTICS_TABLE="ga:12345678"
$ ga-analytics 
 ┌────────────────────┐
 │ sessions (INTEGER) │
 ├────────────────────┤
 │ 83                 │
 └────────────────────┘
 
 Total sessions: 83
 
$ ga-analytics -d ga:pagePath
 ┌────────────────────────────┬────────────────────┐
 │ pagePath (STRING)          │ sessions (INTEGER) │
 ├────────────────────────────┼────────────────────┤
 │ /                          │ 63                 │
 ├────────────────────────────┼────────────────────┤
 │ /aronson/                  │ 2                  │
 ├────────────────────────────┼────────────────────┤
 │ /furtherreading/           │ 4                  │
 ├────────────────────────────┼────────────────────┤
 │ /furtherreading/index.html │ 0                  │
 ├────────────────────────────┼────────────────────┤
 │ /history/                  │ 1                  │
 ├────────────────────────────┼────────────────────┤
 │ /index.html                │ 9                  │
 ├────────────────────────────┼────────────────────┤
 │ /jigsawbuttontest/         │ 3                  │
 ├────────────────────────────┼────────────────────┤
 │ /tips/                     │ 1                  │
 └────────────────────────────┴────────────────────┘
 
 Total sessions: 83
 
$ ga-analytics -d ga:pagePath -m ga:users,ga:sessions
 ┌────────────────────────────┬─────────────────┬────────────────────┐
 │ pagePath (STRING)          │ users (INTEGER) │ sessions (INTEGER) │
 ├────────────────────────────┼─────────────────┼────────────────────┤
 │ /                          │ 25              │ 64                 │
 ├────────────────────────────┼─────────────────┼────────────────────┤
 │ /aronson/                  │ 3               │ 2                  │
 ├────────────────────────────┼─────────────────┼────────────────────┤
 │ /furtherreading/           │ 14              │ 4                  │
 ├────────────────────────────┼─────────────────┼────────────────────┤
 │ /furtherreading/index.html │ 3               │ 0                  │
 ├────────────────────────────┼─────────────────┼────────────────────┤
 │ /history/                  │ 12              │ 1                  │
 ├────────────────────────────┼─────────────────┼────────────────────┤
 │ /index.html                │ 12              │ 9                  │
 ├────────────────────────────┼─────────────────┼────────────────────┤
 │ /jigsawbuttontest/         │ 3               │ 3                  │
 ├────────────────────────────┼─────────────────┼────────────────────┤
 │ /tips/                     │ 12              │ 1                  │
 └────────────────────────────┴─────────────────┴────────────────────┘
 
 Total users: 84
 Total sessions: 84
 
$ ga-analytics -d ga:pagePath -m ga:users,ga:sessions -s 2014-11-18 -e 2014-11-22
 ┌────────────────────────────┬─────────────────┬────────────────────┐
 │ pagePath (STRING)          │ users (INTEGER) │ sessions (INTEGER) │
 ├────────────────────────────┼─────────────────┼────────────────────┤
 │ /                          │ 23              │ 46                 │
 ├────────────────────────────┼─────────────────┼────────────────────┤
 │ /furtherreading/           │ 13              │ 4                  │
 ├────────────────────────────┼─────────────────┼────────────────────┤
 │ /furtherreading/index.html │ 1               │ 0                  │
 ├────────────────────────────┼─────────────────┼────────────────────┤
 │ /history/                  │ 9               │ 1                  │
 ├────────────────────────────┼─────────────────┼────────────────────┤
 │ /index.html                │ 11              │ 8                  │
 ├────────────────────────────┼─────────────────┼────────────────────┤
 │ /jigsawbuttontest/         │ 3               │ 3                  │
 ├────────────────────────────┼─────────────────┼────────────────────┤
 │ /tips/                     │ 11              │ 1                  │
 └────────────────────────────┴─────────────────┴────────────────────┘
 
 Total users: 71
 Total sessions: 63

Node Module

var pullAnalytics = require("ga-analytics");

pullAnalytics({
    metrics: "ga:users"
}, function(err, res) {	
    if(err) throw err;
    console.log(res);
});