如何使用Google Analytics Embed API获取总结果

时间:2023-01-16 15:19:06

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.

使用GA Query Explorer(https://ga-dev-tools.appspot.com/query-explorer/),在运行查询后显示结果时,您可以看到“找到的总结果”编号。

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

如何将此应用于GA嵌入API查询格式? ...

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!!

1 个解决方案

#1


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

以下是传递给“成功”事件的参数文档: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).

另请注意,您不需要示例中的'totalResults':'integer'部分,也不需要reportType:'ga'部分(它是可选的)。

#1


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

以下是传递给“成功”事件的参数文档: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).

另请注意,您不需要示例中的'totalResults':'integer'部分,也不需要reportType:'ga'部分(它是可选的)。