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-chartbuilder

v1.0.0

Published

Fetches Google Analytics data from your app and builds charts from javascript objects.

Downloads

2

Readme

ga-chartbuilder

Fetches Google Analytics data from your app and builds charts from javascript objects.

Features

  • Renders charts as an image or as html with inline javascript.
  • Supports all features of chartjs.

Installation

npm install ga-chartbuilder

Usage

The ChartBuilder object must be configured before it can be used to build reports.

Setup

ChartBuilder authenticates using Google Service Account credentials. Before creating the Service Account, make sure you are logged in to Google with the same account as you will be retrieving analytics data from.

To create the service account follow this link, then select New Service Account in the dropdown and JSON key file type. Enter a service account name, this name does not have to match the project name you are collecting analytics for. Then select a role, I used Project > Owner, but I'm not sure how other roles would impact access.

Next download the file and put it somewhere you can reference from the project you are using ga-chartbuilder in.

VERY IMPORTANT: Make sure you add the client email from the file you just downloaded as a Google Analytics user for the Analytics project you are getting data from. The Google API will not have access to read the data otherwise.

Configuring

Call configure on the ChartBuilder class to return an instance of Chartbuilder.

const ChartBuilder = require("ga-chartbuilder");

const cb = ChartBuilder.config({
  keyFile: "path/to/keyfile",
  scopes: ["http://googla.com/other-scopes"],
  ids: ["XXXX"],
  chartOptions: {

  }
})

Parameters:

  • keyFile (REQUIRED): Path from current directory to the keyFile downloaded for the Google Service Account.
  • scopes: Default scope is https://www.googleapis.com/auth/analytics.readonly.
  • ids (REQUIRED): Google Analytics view IDs. To find this, log in to Google Analytics, go to Admin > View Settings and copy the View ID for any view you want to be included in the report.
  • chartOptions: global options for chart.js. These options apply to all charts created, but can be overwritten by the chart options given for each individual chart.

Generating Charts

To generate charts, call generateCharts on the instance of ChartBuilder you created during configuration.

cb.generateCharts([
  {
    gaOptions: {
      dimensions: ["dimension1"],
      metrics: ["metric1"],
    }, chartOptions: {
      type: "bar",
      datasetLabel: "my dataset",
    }
  }
])
  .then(charts => {
    // do things with your charts!
    // note: charts is an array of either image streams or html charts
  })

Parameters:

  • options: An array of objects containing Google Analytics objects and Chart js options.
    • gaOptions: Google Analytics options
      • dimensions (REQUIRED) (do not include ga:... part, each dimension should be separate element of array),
      • metrics (REQUIRED) ^^,
      • startDate,
      • endDate,
      • filters (string of ga:... things comma separated),
      • ids,
      • includeEmptyRows
    • chartOptions:
      • type (REQUIRED),
      • columnHeaders (REQUIRED for type=table),
      • datasetLabel (REQUIRED for type!=table),
      • renderAsImage (return image instead of html, works for when script can't be executed),
      • dateFormat (type of date format given from that GA request, depends on time metric)
      • chartOptions (any chart js options for options param of chart),
      • datasetOptions (any chartjs options for dataset)