【问题标题】:Google Analytics API: Get Sessions per hourGoogle Analytics API:每小时获取会话数
【发布时间】:2017-11-30 02:56:49
【问题描述】:

我需要从我们的 Google Analytics API 创建一些数据库条目。需要特别注意的是,我每天午夜只请求一次此数据。以下是详细信息:

所需数据: 对象:

{ sessions: Int, hourOfDay: Int date: 'YYYY-MM-DD }

我每天每个小时都需要这些数据。我想将其作为 7 天期间的实际值(即 24 个对象重复 7 天)或过去一周该小时的平均值(即 24 个具有过去平均值的对象) 7 天那个时间。)

这是一些示例代码,我知道为此需要对其进行编辑:

authorize(function(err, token) {
  const weekInSeconds = 436800000;
  const endDate = new Date();
  const startDate = new Date(endDate.getTime() - weekInSeconds);

  // Code to convert startDate + endDate to Strings (e.g: '2017-01-05')

  var requestConfig = {
    'ids': 'ga:1234567890',
    'start-date': endDateStr,
    'end-date' : startDateStr,
    'metrics': 'ga:sessions'
  };

  // Code here sends request to Google API

});

requestConfig 是这里的关键,或者可能是开始/编辑日期。以下是我目前缺乏的知识:

是否可以使用requestConfig 中的'metrics' 字段来获取上述日期对象?

我的另一个思路是为每个不同的字段并行发出批量请求,并从汇总的响应中创建对象。对此的解释(伪代码):

for (each hour of past 7 days) {
  var requestConfig = {
      'ids': 'ga:1234567890',
      'start-date': endDateStr,
      'end-date' : startDateStr,
      'metrics': 'ga:sessions'
  };

  // Create fields for hour, date | fill with appropriate value
  // Send requestConfig and create field in object from response
}

我宁愿只使用前一种方法,因为它需要更少的 API 操作和更少的地方引入错误。

【问题讨论】:

    标签: node.js google-analytics google-analytics-api google-api-nodejs-client


    【解决方案1】:

    没关系,我已经找到答案了。我了解到在 API 对象中存在另一个字段:'dimensions',通过它我可以按各种参数(例如小时、分钟、日期、日期小时等)对结果进行分组。

    【讨论】:

    • 你能告诉我你是怎么用 batchGet 做的吗?喜欢 startTime 和 endTime 而不是 startDate 和 endDate ?
    • 没有任何线索,伙计。我不再属于那家公司,也不记得我做了什么。对不起
    【解决方案2】:

    我发现查询资源管理器非常有用see query explorer

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-01
      • 1970-01-01
      • 2011-11-22
      相关资源
      最近更新 更多