Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Google Analytics Report into Treasure Data (for the previous day)
// See https://developers.google.com/analytics/devguides/reporting/core/v3/reference for how Core Reporting API works
// TODO: Filter by user segment, etc.
INPUTS = {
groupBy: ['country'],
metrics:['visits'],
sortBy:['visits'],
profileId: 'YOUR_GA_PROFILE_ID',
databaseName: 'google_analytics',
tableName: 'sample_report',
tdAPIKey: 'YOUR_TD_API_KEY'
}
function buildQuery(params) {
var res = [];
var dimName;
for (var i in params) {
dimName = params[i];
if (!dimName.startsWith("ga:")) { dimName = "ga:" + dimName; }
res.push(dimName);
}
return res.join(',');
}
function extractColumnNames(report) {
var cols = [];
report.columnHeaders.map(function(c) { cols.push(c.name.replace(/^ga:/, '')) });
return cols;
}
function makeEvent(cols, row, timestamp) {
if (cols.length !== row.length) { throw new Error("mismatched column header and row"); }
var o = {"time":timestamp};
var ii;
for (ii = 0; ii < cols.length; ii++) { o[cols[ii]] = row[ii]; }
return o;
}
function fetchReport() {
var options = {
'dimensions': buildQuery(INPUTS.groupBy),
'sort': buildQuery(INPUTS.sortBy),
'max-results': 10000
};
var today = new Date();
var yesterday = new Date(today.getTime() - 24*60*60*1000);
var yesterdayUnix = 24*60*60*Math.floor(yesterday.getTime()/(24*60*60*1000));
var yesterdayStr = Utilities.formatDate(yesterday, Session.getTimeZone(), 'yyyy-MM-dd');
var tableId = 'ga:' + INPUTS.profileId;
var metrics = buildQuery(INPUTS.metrics);
var report = Analytics.Data.Ga.get(tableId, yesterdayStr, yesterdayStr, metrics, options);
var events = [];
var rows = report.rows;
var cols = extractColumnNames(report);
for (var ii in rows) {
var row = rows[ii];
events.push(makeEvent(cols, row, yesterdayUnix));
}
return events;
}
function postTreasureData(events, database, table, apikey) {
var data = {};
data[database+"."+table] = events;
var payload = JSON.stringify(data);
var options = {
"method": "POST",
"contentType" : "application/json",
"headers" : {
"X-TD-Write-Key": apikey,
"X-TD-Data-Type": "k"
},
"payload": payload
};
var response = UrlFetchApp.fetch("http://in.treasuredata.com/js/v3/event/", options);
Logger.log(response);
}
function main() {
var events = fetchReport();
postTreasureData(events, INPUTS.databaseName, INPUTS.tableName, INPUTS.tdAPIKey);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.