admin管理员组

文章数量:1414621

How would I go about passing multiple dimensions when executing a Google Analytics Reporting API v4 Query. For example how would I pass ga:dimension7 in addition to ga:dimension5 in my dimensions?

function queryReports() {
        gapi.client.request({
            path: '/v4/reports:batchGet',
            root: '/',
            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));
    }

How would I go about passing multiple dimensions when executing a Google Analytics Reporting API v4 Query. For example how would I pass ga:dimension7 in addition to ga:dimension5 in my dimensions?

function queryReports() {
        gapi.client.request({
            path: '/v4/reports:batchGet',
            root: 'https://analyticsreporting.googleapis./',
            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));
    }

When I separate them with ma,then I have the error below:

Any idea?

Share Improve this question edited Nov 4, 2016 at 9:21 Atzmon 1,3181 gold badge7 silver badges15 bronze badges asked Nov 4, 2016 at 3:29 PlutorsPlutors 721 silver badge9 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

I have the answer below:

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

see details here guide to migrate the Core Reporting API V3 to the Analytics Reporting API V4

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"}
      ],
    }

Easy to understand pattern here for calling multiple dimensions and metrics. Replace 'ga:' with your choice of Dimensions and Metrics. Exclude date and time if ou want. It will show specific sessions occurring on that dates

Your can use multiples dimencions like this

dimensions: "ga:date,ga:campaign"

Here share an example:

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();

本文标签: Passing multiple dimensions to google analytics reporting API v4 with javaScriptStack Overflow