1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
| const { google } = require('googleapis'); const OAuth2 = google.auth.OAuth2; const fs = require("fs"); const _ = require("lodash"); const moment = require("moment");
_.mixin({ compactObject: function (o) { var clone = _.clone(o); _.each(clone, function (v, k) { if (!v) { delete clone[k]; } }); return clone; }, });
module.exports = function (settings, final_callback) { try { var GOOGLEAPI_CLIENTID = settings.clientId ? settings.clientId : process.env.GOOGLEAPI_CLIENTID, GOOGLEAPI_EMAIL = settings.serviceEmail ? settings.serviceEmail : process.env.GOOGLEAPI_EMAIL, GOOGLEAPI_KEY = settings.key ? settings.key : process.env.GOOGLEAPI_KEY, GOOGLEAPI_ANALYTICS_TABLE = settings.ids ? settings.ids : process.env.GOOGLEAPI_ANALYTICS_TABLE;
var sessionFile = process.env.TMPDIR + "ga-analytics-" + GOOGLEAPI_EMAIL.replace(/[^a-zA-Z\-]/gi, "_");
if (!GOOGLEAPI_CLIENTID || !GOOGLEAPI_EMAIL || !GOOGLEAPI_KEY || !GOOGLEAPI_ANALYTICS_TABLE) throw "Missing"; } catch (e) { var error = "\n A valid client id, service email, key, and table are required" + "\n\n OAuth Client id: " + settings.clientId + "\n Service Email: " + settings.serviceEmail + "\n Path to Private Key: " + settings.key + "\n Google Analytics view: " + GOOGLEAPI_ANALYTICS_TABLE;
final_callback(new Error(error)); return; }
if (!settings.startDate) settings.startDate = moment().subtract(moment.duration(1, "M")).format("YYYY-MM-DD");
if (!settings.endDate) settings.endDate = moment(settings.startDate).add(moment.duration(1, "M")).format("YYYY-MM-DD");
const oauth2Client = new OAuth2(GOOGLEAPI_CLIENTID, null, "postmessage"); const jwt = new google.auth.JWT(GOOGLEAPI_EMAIL, GOOGLEAPI_KEY, null, [ "https://www.googleapis.com/auth/analytics.readonly", ]);
const authorize = function (callback) { fs.exists(sessionFile, function (exists) { if (exists) { fs.readFile(sessionFile, function (err, res) { if (err) { jwt.authorize(callback); return; }
try { var json = JSON.parse(res); } catch (e) { jwt.authorize(callback); return; }
if (new Date(json.expiry_date) > Date.now()) { callback(null, json); } else { jwt.authorize(callback); } }); } else { jwt.authorize(callback); } }); };
authorize(function (err, result) { if (err) { final_callback(err); return; }
fs.writeFile(sessionFile, JSON.stringify(result), function (err) { final_callback(err); });
oauth2Client.setCredentials({ access_token: result.access_token, refresh_token: result.refresh_token, });
google.analytics("v3").data.ga.get( _.compactObject({ ids: GOOGLEAPI_ANALYTICS_TABLE, "start-date": settings.startDate, "end-date": settings.endDate, metrics: settings.metrics, filters: settings.filter, dimensions: settings.dimensions, "max-results": settings.max, sort: settings.sort, auth: oauth2Client, }), function (err, r) { if (err) { final_callback(err); return; }
final_callback(null, r); } ); }); };
|