Skip to content

Instantly share code, notes, and snippets.

@williamjulianvicary
Forked from chipoglesby/costdata.gs
Created August 13, 2017 13:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save williamjulianvicary/6b8c35d3d3ce495f85c4df14e16c30d2 to your computer and use it in GitHub Desktop.
Save williamjulianvicary/6b8c35d3d3ce495f85c4df14e16c30d2 to your computer and use it in GitHub Desktop.
Cost Data Upload via Google Analytic's Management API with Google Sheets
function uploadData() {
var accountId = "xxxxxxxx";
var webPropertyId = "UA-xxxxxxxx-x";
var customDataSourceId = "xxxxxxxx";
var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var maxRows = ss.getLastRow();
var maxColumns = ss.getLastColumn();
var data = [];
for (var i = 1; i <= maxRows;i++) {
data.push(ss.getRange([i], 1,1, maxColumns).getValues());
}
var newData = data.join("\n");
var blobData = Utilities.newBlob(newData, "application/octet-stream", "GA import data");
try {
var upload = Analytics.Management.Uploads.uploadData(accountId, webPropertyId, customDataSourceId, blobData);
SpreadsheetApp.getUi().alert("Uploading: OK");
}
catch(err) {
SpreadsheetApp.getUi().alert("Cannot upload: Failed");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment