Skip to content

Instantly share code, notes, and snippets.

@yi-jiayu
Created July 5, 2017 03:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yi-jiayu/e1fccd54a3ef7a4eed6c2a999420b50c to your computer and use it in GitHub Desktop.
Save yi-jiayu/e1fccd54a3ef7a4eed6c2a999420b50c to your computer and use it in GitHub Desktop.
Using Google Cloud Functions to display data from Google Analytics as a README badge
"use strict";
const google = require('googleapis');
const key = require('./credentials.json');
const viewId = 'YOUR_VIEW_ID';
function getUsers(key, viewId) {
// https://github.com/google/google-api-nodejs-client#using-jwt-service-tokens
const jwtClient = new google.auth.JWT(
key.client_email,
null,
key.private_key,
['https://www.googleapis.com/auth/analytics.readonly'],
null
);
return new Promise((resolve, reject) => {
jwtClient.authorize(err => {
if (err) {
console.log(err);
return;
}
// based on https://github.com/google/google-api-nodejs-client/issues/561
const analytics = google.analyticsreporting('v4');
analytics.reports.batchGet({
auth: jwtClient,
resource: {
reportRequests: [
{
viewId: viewId,
dateRanges: [
{
startDate: '7daysAgo',
endDate: 'today'
}
],
metrics: [
{
expression: 'ga:users'
}
]
}
]
}
}, (err, data) => {
if (err) {
return reject(err);
} else {
return resolve(data);
}
});
});
});
}
exports.function = function (req, res) {
return getUsers(key, viewId)
.then(data => {
const value = data.reports[0].data.totals[0].values[0];
return res.redirect(302, `https://img.shields.io/badge/users-${value}%2Fweek-yellow.svg`);
})
.catch(err => {
console.error(err);
return res.sendStatus(500);
});
};
{
"name": "google-analytics-repo-badge",
"version": "0.1.0",
"description": "Using Google Cloud Functions to display data from Google Analytics as a README badge",
"main": "index.js",
"author": "Jiayu Yi <yi-jiayu@users.noreply.github.com>",
"license": "MIT",
"dependencies": {
"googleapis": "^20.0.1"
}
}
"use strict";
const google = require('googleapis');
const key = require('./credentials.json');
const viewId = 'YOUR_VIEW_ID';
function getUsers(key, viewId) {
// https://github.com/google/google-api-nodejs-client#using-jwt-service-tokens
const jwtClient = new google.auth.JWT(
key.client_email,
null,
key.private_key,
['https://www.googleapis.com/auth/analytics.readonly'],
null
);
return new Promise((resolve, reject) => {
jwtClient.authorize(err => {
if (err) {
console.log(err);
return;
}
const analytics = google.analyticsreporting('v4');
analytics.reports.batchGet({
auth: jwtClient,
resource: {
reportRequests: [
{
viewId: viewId,
dateRanges: [
{
startDate: '7daysAgo',
endDate: 'today'
}
],
metrics: [
{
expression: 'ga:users'
}
]
}
]
}
}, (err, data) => {
if (err) {
return reject(err);
} else {
return resolve(data);
}
});
});
});
}
getUsers(key, viewId)
.then(data => console.log(JSON.stringify(data)))
.catch(console.error);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment