如何在 Google Analytics API 中使用细分

How to use segments in Google Analytics API

在 Google Analytics 中,我们可以创建如下细分:

Google Analytics API explorer 中,我们可以通过段 ID 或代码提取段信息:

我假设我们可以将该代码剪切并粘贴到 google 分析核心报告 v3 参考代码中,就像我们处理所有其他参数一样:

然而,当我 运行 代码时,我得到这个错误:

Arg, there was an API error : 400 : Invalid value 'users::condition::dateOfSession==2015-04-30;ga:sessionCount==1;condition::ga:campaign=@33100;sessions::condition::ga:userType=@Returning Visitor' 
for segment parameter.

我不确定哪里出了问题。当我在代码中使用段 ID 代替段参数(段 ='gaid::xxxxxxx')时,出现此错误:

Arg, there was an API error : 400 : Segment XXXXXXXXXXX is not supported in the API.

不确定我应该如何构建代码。

您发送到该段的所有内容,实际上您发送到报告 API 的任何参数的任何内容都必须 URL 编码。

users::condition::dateOfSession==2015-04-30;ga:sessionCount==1;condition::ga:campaign=@33100;sessions::condition::ga:userType=@Returning Visitor

变成

users%3A%3Acondition%3A%3AdateOfSession%3D%3D2015-04-30%3Bga%3AsessionCount%3D%3D1%3Bcondition%3A%3Aga%3Acampaign%3D%4033100%3Bsessions%3A%3Acondition%3A%3Aga%3AuserType%3D%40Returning+Visitor

应该可以。我唯一不确定的是 ; 是否需要编码。您可能需要对此进行测试。我知道 == 会。如果它不起作用请告诉我,我会添加一些片段并自己做一些测试。

在调整段代码并阅读 segments dev guide 中有关段的更多信息后,我找到了解决方案。

我在 dateOfsession 参数方面遇到问题,我应该使用“<>”而不是“=”作为日期。所以这是我应该做的: dateOfsession<>2015-04-30_2015-04-30 不知何故,使用“==”不起作用。

dateOfsession 是 GA API 中的一个新参数,因此几乎没有关于它的示例和文档。