AyDee
AyDee

Reputation: 221

Google Analytics reporting v4 (filters)

I am using this example https://developers.google.com/analytics/devguides/reporting/core/v4/quickstart/web-js I want to insert a filter in the body of gapi.client.request.

This is my sample code

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: 'yesterday'
          }
        ],
        metrics: [
          {
            expression: 'ga:pageviews'
          }
        ],
        dimensions: [
          {
            name: 'ga:date'
          }
        ]
        // filters: [
        //   {
        //     name: 'ga:pagePath=~/mypath/'
        //   }
        // ]
      }
    ]
  }
 }).then(displayResults, console.error.bind(console));
}

What is the format of filters in the reportRequest?

Upvotes: 0

Views: 3180

Answers (3)

Shravya Mutyapu
Shravya Mutyapu

Reputation: 318

"filtersExpression":"ga:pagePath=~/mypath/"

The above will work like charm. Please refer this link.

Upvotes: -1

Shravan Ravva
Shravan Ravva

Reputation: 41

This solution is working in my project and you want give dimension_name that your choice:

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: 'yesterday'
                        }

                      ],

                      metrics: [
                        {

                            expression: 'ga:pageviews'

                        }

                      ],

                      dimensions: [
                        {

                            name: 'ga:date'

                        }

                      ],

                      dimensionFilterClauses: [{

                          filters: [{

                              dimension_name: 'ga:browser',
                              operator: 'EXACT',
                              expressions: ["Firefox"]
                          }]
                      }]
                  }
                ]
            }
        }).then(displayResults, console.error.bind(console));
    }

Upvotes: 3

miken32
miken32

Reputation: 42675

According to the documentation, there is no field named filters. There is, however, a filtersExpression string field:

Dimension or metric filters that restrict the data returned for your request. To use the filtersExpression, supply a dimension or metric on which to filter, followed by the filter expression. For example, the following expression selects ga:browser dimension which starts with Firefox; ga:browser=~^Firefox. For more information on dimensions and metric filters, see Filters reference.

So you should be able to add the following to your request body:

filtersExpression: 'ga:pagePath=~/mypath/'

Upvotes: 4

Related Questions