Jiew Meng
Jiew Meng

Reputation: 88287

Google Analytics Reporting API: Possible to get pivot table of page and their views from each country?

I currently have a table like

Page | Views | Singapore | US | Country A | ... -----|-------|-----------|-----|-----------| ... A | 200 | 50 | 100 | 30 | ... B | 220 | 20 | 150 | 20 | ...

Generated from the following query:

{
  "reportRequests": [
    {
      "viewId": "XXXXXX",
      "pageSize": "5",
      "dateRanges": [
        {
          "startDate": "2017-10-01",
          "endDate": "2017-10-31"
        }
      ],
      "metrics": [
        {
          "expression": "ga:pageviews"
        }
      ],
      "dimensions": [
        {
          "name": "ga:pageTitle"
        }
      ],
      "orderBys": [
        {
          "sortOrder": "DESCENDING",
          "fieldName": "ga:pageviews"
        }
      ],
      "pivots": [
        {
          "dimensions": [
            {
              "name": "ga:country"
            }
          ],
          "metrics": [
            {
              "expression": "ga:pageviews"
            }
          ]
        }
      ]
    }
  ]
}

But I want only focus only on specific countries. For example I only want to see Singapore and US columns without any other country data. How can I do that? Total views can still stay and include data from other countries.

If I provide a list of countries can it always show those columns even if theres no views?

Upvotes: 0

Views: 518

Answers (1)

Max
Max

Reputation: 13334

I see a few questions:

How to filter on specific countries?
You can use the query filters or segment to achieve this. The query explorer will help you build the correct filters: https://ga-dev-tools.appspot.com/query-explorer/.

Note: the operator for OR is , (see reference).

enter image description here

"can it always show those columns even if theres no views?"
By default no: Google Analytics only returns data it has. If it has no views, it has no data, thus won't return anything. The reason is that it would waste processing power and network bandwidth to return data that doesn't exist in GA. You will have to rebuild the missing data yourself.

You might want to try the includeEmptyRows of the V4 reporting API though, which is set by default to FALSE, as this might do what you're looking for.

Note on the country dimension
Instead of using ga:country, you can use ga:countryIsoCode to filter on 2-letter country codes and not have to deal with percent encoding for countries which are composed of several words (e.g. US vs. United%20States).

Upvotes: 1

Related Questions