Created
July 5, 2017 03:11
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"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); | |
}); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"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