如何使用Google SpreadSheet将费用上传到Google Analytics(分析) [英] How to upload cost to Google Analytics using Google SpreadSheet

查看:234
本文介绍了如何使用Google SpreadSheet将费用上传到Google Analytics(分析)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道可以通过网络界面将数据费用上传到Google Analytics(分析) 。但是,我想让这个过程更自动一些,所以我想使用谷歌电子表格和谷歌应用程序脚本缩短这一过程。

I know it is possible to upload data cost to Google Analytics via the web interface. I, however, want to make this process a bit more automatic, so I thought about using google spreadsheet and google app script to short this processes.

我的想法是我要在spreadSheet上上传到Google Analytics(分析)的数据,然后使用Google Apps脚本高级服务将数据费用上传到我的Google Analytics(分析)媒体资源。不幸的是,Google App Script Advanced Services的文档非常有限。所以我跳了一些可以帮助我。这是到目前为止做的:

My ideia is to get the data I want to upload to Google Analytics on a spreadSheet and then use Google Apps Script Advanced Services to upload the data cost to my google Analytics Property. Unfortunately the documentation on Google App Script Advanced Services is very limited. So I was hopping some one could help me. Here is what´ve done so far:

function uploadDataCost() {

  var accountId = "XXXXXXXX";
  var webPropertyId = "UA-XXXXXXX";
  var customDataSourceId = "XXXXXXXXXXXXX";

  var csvData = convertRangeToCSVString();      

  var mediaData = Utilities.newBlob([]); //This variable is my main problem.

  Analytics.Management.Uploads.uploadData(accountId, webPropertyId, customDataSourceId, mediaData);



}

代码,用于转换范围为CSV作为字符串。但我不知道如何获取这个字符串,并将其转换为Blob,所以我可以使用它作为mediaData参数中的方法 Analytics.Management.Uploads.uploadData(accountId,webPropertyId,customDataSourceId,mediaData );

I have found a code that converts the range to a CSV as a string. But I don´t know how to take this string and convert it to a blob so I can use it as the mediaData parameter in the method Analytics.Management.Uploads.uploadData(accountId, webPropertyId, customDataSourceId, mediaData);.

我相信调用这个方法会做的伎俩,但是我不知道如何传递mediaData参数正如我所说,我找不到任何文档解释这种方法如何工作。我知道mediaData应该是Blob类型,但是我找不到任何解释如何创建一个CSV文件的Blob的例子。

I believe calling this method would do the trick, however I´m not sure how to pass the data in the mediaData parameter and as I said, I could not find any documentation explaining how this method works. I know that mediaData should be of Blob type, but I could not find any examples explaining how to create a Blob that is a CSV file.

推荐答案

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");
  }
}

来源

这篇关于如何使用Google SpreadSheet将费用上传到Google Analytics(分析)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆