Noah Demand
Noah Demand

Reputation: 1

How to get Total Results using Google Analytics Embed API

I'm trying to simply pull the total number of pages on my site.

Using the GA Query Explorer (https://ga-dev-tools.appspot.com/query-explorer/), you can see the "Total results found" number when the results are displayed after running a query.

How can I apply this to GA Embed API query format? ...

var pageTitles = new gapi.analytics.googleCharts.DataChart({
    reportType: 'ga',
    query: {
      'dimensions': 'ga:pageTitle',
      'metrics': 'ga:pageviews',
      'segment': 'gaid::-1',
      'sort': '-ga:pageviews',
      'filters': 'ga:pagePath!=/',
      'max-results': '10',
      'totalResults': 'integer',
    },
    chart: {
      type: 'TABLE',
      container: 'page-titles',
      options: {
        title: 'Top Pageviews',
        width: '100%'
      }
    }
  });

Thank you!!

Upvotes: 0

Views: 2040

Answers (1)

Philip Walton
Philip Walton

Reputation: 30431

If you add an event handler for successful queries, you can get access to the totals. Here's an example:

var pageTitles = new gapi.analytics.googleCharts.DataChart({
  query: {
    'dimensions': 'ga:pageTitle',
    'metrics': 'ga:pageviews',
    'segment': 'gaid::-1',
    'sort': '-ga:pageviews',
    'filters': 'ga:pagePath!=/',
    'max-results': '10'
  },
  chart: {
    type: 'TABLE',
    container: 'page-titles',
    options: {
      title: 'Top Pageviews',
      width: '100%'
    }
  }
});

pageTitles.on('success', function(result) {
  // Print the total pageview count to the console.
  console.log(result.response.totalsForAllResults['ga:pageviews']);
});

And here's the documentation for the parameters passed to the 'success' event:
https://developers.google.com/analytics/devguides/reporting/embed/v1/component-reference#datachart

Also note that you don't need the 'totalResults': 'integer' part that you have in your example, nor do you need the reportType: 'ga' part (it's optional).

Upvotes: 2

Related Questions