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)
- gaOptions: Google Analytics options