使用javaScript将多个维度传递给google analytics报告API v4

时间:2016-11-04 03:29:01

标签: javascript google-analytics-api

在执行Google Analytics Reporting API v4查询时,如何传递多个维度?例如,除了我的维度中的ga:dimension5之外,我将如何传递ga:dimension7?



function queryReports() {
        gapi.client.request({
            path: '/v4/reports:batchGet',
            root: 'https://analyticsreporting.googleapis.com/',
            method: 'POST',
            body: {
                reportRequests: [
                    {
                        viewId: VIEW_ID,
                        dateRanges: [
                            {
                                startDate: '7daysAgo',
                                endDate: 'today'
                            }
                        ],
                        dimensions: [
                            {
                                name: 'ga:dimension5'
                            }
                        ],
                        metrics: [
                            {
                                expression: 'ga:totalEvents',
                                alias: 'orderNumber'
                            }
                        ],
                        filtersExpression: 'ga:eventCategory==xxx,ga:eventAction==xxx',
                        filtersExpression: 'ga:dimension5=~\^\\\[.*\\\]\$'
                    }
                ]
            }
        }).then(displayResults, console.error.bind(console));
    }




当我用逗号分隔它们时,我有以下错误:

wrong message

有什么想法吗?

3 个答案:

答案 0 :(得分:2)

我的答案如下:



POST https://analyticsreporting.googleapis.com/v4/reports:batchGet
{
  "reportRequests":[
  {
    ...
    "dimensions": [
    {
      "name":"ga:dimension3"
    },{
      "name":"ga:dimension5"
    }],
    ...
  }]
}




在此处查看详细信息guide to migrate the Core Reporting API V3 to the Analytics Reporting API V4

答案 1 :(得分:0)

sample_request = {
      'viewId': 'xxxxxxxxx',
      'dateRanges': {
          'startDate': datetime.strftime(datetime.now() - timedelta(days = 30),'%Y-%m-%d'),
          'endDate': datetime.strftime(datetime.now(),'%Y-%m-%d')
      },

      "dimensions": 
      [
        {"name": "ga:date"},
        {"name": "ga:userType"},
        {"name": "ga:sessionDurationBucket"}
      ],
      "metrics": 
      [
        {"expression": "ga:sessions"},
        {"expression": "ga:newUsers"},
        {"expression": "ga:bounces"}
      ],
    }

此处易于理解用于调用多个维度和指标的模式。 用您选择的维度和指标替换“ ga:”。如果需要,请排除日期和时间。它将显示在该日期发生的特定会话

答案 2 :(得分:0)

您可以像这样使用多个维度

dimensions: "ga:date,ga:campaign"

这里分享一个例子:

function getData() {
  const response = await jwt.authorize();
  const result = await google.analytics("v3").data.ga.get({
    auth: jwt,
    ids: "ga:" + view_id,
    "start-date": "2021-05-01",
    "end-date": "today",
    dimensions: "ga:date,ga:campaign",
    metrics: "ga:users",
    sort: "ga:campaign,ga:date",
  });

  console.dir(result);
};

getData();