Google Analytics自定义维度页面视图

时间:2015-08-21 18:33:52

标签: javascript google-analytics

我有以下工作代码,我希望看到每个自定义维度的页面视图的控制台日志。

var report = new gapi.analytics.report.Data({
  query: {
    'ids': 'ga:XXXXXX',
    'metrics': 'ga:pageviews',
    'dimensions': 'ga:dimension1',
    'start-date': '7daysAgo',
    'end-date': 'yesterday',
  }
});

// Runs the query.
report.execute();

// Specifies the callback function  to be run when the query succeeds.
report.on('success', function(response) {

  // Logs the entire response object.
  console.log(response);

  // Logs just the total pageviews.
  console.log(response.totalsForAllResults['ga:pageviews']);
});

1 个答案:

答案 0 :(得分:0)

您必须执行多项查询才能获得这些计数。由于维度可以重叠(例如ga:browser=Chromega:date=20150821)或不重叠(例如ga:browser=Internet Explorerga:operatingSystem=Windows),因此解析结果并统计计数将会复杂得多你自己。只需对您拥有的每个维度进行单独查询。

以下是一个例子:

var baseQueryData = {
  'ids': 'ga:XXXXXX',
  'metrics': 'ga:pageviews',
  'start-date': '7daysAgo',
  'end-date': 'yesterday',
}

function runQuery(dimension, callback) {
  var report = new gapi.analytics.report.Data({query: baseQueryData});
  report.set({query: {dimensions: dimension}});
  report.on('success', callback);
  report.execute();
}

function logResult(response) {
  var dimension = response.query.dimensions;
  var count = response.totalsForAllResults['ga:pageviews'];
  console.log(dimension, count);
}

runQuery('ga:dimension1', logResult);
runQuery('ga:dimension2', logResult);
runQuery('ga:dimension3', logResult);