Google 分析 API 设备类别

Google Analytics API deviceCategory

我正在尝试使用来自 Google Analytics 的 Google Analytics API (v3) in Google Scripts to pull Device Category 信息。

Audience -> Mobile -> Overview 下的 Analytics 中,有一个 Device Category 部分列表 'tablet', 'mobile' and 'desktop'。我希望将这些数字拉入我的脚本所附的 Google Sheet。

我认为我应该使用的代码是: ga:deviceCategory==mobile (to pull mobile traffic)ga:deviceCategory==tablet 拉动平板电脑流量。

但是,我收到错误消息:

Invalid dimension or metric: ga:deviceCategory==desktop

我对此感到有些困惑,因为 Google 自己的文档说 deviceCategory 是一个有效维度

(https://developers.google.com/analytics/devguides/reporting/core/dimsmets#view=detail&group=platform_or_device&jump=ga_devicecategory)

如果我从末尾删除设备类型 ('ga:deviceCategory'),我会收到错误消息:

Unknown metric(s): ga:deviceCategory (line 43, file "Code")

这让我相信我需要事先包含指标(我认为是 'pageviews')。如果是这种情况,谁能告诉我如何提取 mobile/tablet 流量的数据?

我在提取数据的其他方面时没有遇到其他问题。例如:

var impressions = Analytics.Data.Ga.get(tableId, startDate, endDate, 'ga:visits').rows.toString(); 

工作正常。

作为参考,这是我用于设备类别的完整代码:

  // Get Mobile Visits, this is the number of visits from mobile devices
  var mvisits = Analytics.Data.Ga.get(tableId, startDate, endDate, 'ga:deviceCategory==mobile').rows.toString();
      mvisits = mvisits;

我将不胜感激,如果您需要我的脚本中的更多代码,请告诉我。

提前致谢。

我的应用程序脚本有点生疏,但您的问题是 == 维度字段只是您想要 select 的列。

var tableId  = 'ga:' + profileId;
var metric = 'ga:visits';
var options = {
    'dimensions': 'ga:deviceCategory',
    'filters': 'ga:deviceCategory==mobile'
    };
var report = Analytics.Data.Ga.get(tableId, startDate, endDate, metric,
  options);

您需要做的是添加一些 filters。如果你把它想象成一个关系数据库,这就像说

select deviceCategory, visits
from tableId  
where devicecategory == mobile

请记住,您需要添加一个指标。

示例:无耻地从 Analytics service 页面撕下。