如何在Google AnalyticsAPI中使用细分

时间:2022-01-11 14:42:11

In Google Analytics, we are able to create segments like below:

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

如何在Google AnalyticsAPI中使用细分

From the Google Analytics API explorer, we can pull the segment information be it by segment id or by its code:

在Google AnalyticsAPI资源管理器中,我们可以按细分ID或其代码提取细分信息:

如何在Google AnalyticsAPI中使用细分

I would assume we can just cut and paste that code into the google analytics core reporting v3 reference code like we do with all the other parameters:

我想我们可以将该代码剪切并粘贴到谷歌分析核心报告v3参考代码中,就像我们对所有其他参数一样:

如何在Google AnalyticsAPI中使用细分

When I run the code however, I get this error:

然而,当我运行代码时,我收到此错误:

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.

I am not sure what could be wrong. When I use the segment id instead in my code for the segment parameter(segment ='gaid::xxxxxxx'), i get this error:

我不确定会出现什么问题。当我在我的代码中使用段ID代替段参数(segment ='gaid :: xxxxxxx')时,我收到此错误:

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

Not sure how I should structure the code.

不知道我应该如何构造代码。

2 个解决方案

#1


I found the solution after tweaking with the segment code and reading more about segments in the segments dev guide.

我在使用细分代码进行调整后,在段开发指南中阅读了更多有关细分的信息后找到了解决方案。

I was having issues with the dateOfsession parameter where i should have used '<>' instead of '=' for the date. So this is what i should have done: dateOfsession<>2015-04-30_2015-04-30 Somehow using '==' doesn't work.

我遇到了dateOfsession参数的问题,我应该使用'<>'代替'='作为日期。所以这就是我应该做的:dateOfsession <> 2015-04-30_2015-04-30以某种方式使用'=='不起作用。

The dateOfsession is a new parameter in the GA API and hence have little examples and documentation about it.

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

#2


Everything you send to the segment, actually anything you send to any of the parameter to the reporting API must be URL encoded.

您发送到细分受众群的所有内容,实际上您发送到报告API的任何参数的任何内容都必须进行URL编码。

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

becomes

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

That should work. the only thing I am not sure about is if the ; needs to be encode or not. You may need to test that. I know the == does. If it doesn't work let me know and I will add a few segments and do some testing myself.

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

#1


I found the solution after tweaking with the segment code and reading more about segments in the segments dev guide.

我在使用细分代码进行调整后,在段开发指南中阅读了更多有关细分的信息后找到了解决方案。

I was having issues with the dateOfsession parameter where i should have used '<>' instead of '=' for the date. So this is what i should have done: dateOfsession<>2015-04-30_2015-04-30 Somehow using '==' doesn't work.

我遇到了dateOfsession参数的问题,我应该使用'<>'代替'='作为日期。所以这就是我应该做的:dateOfsession <> 2015-04-30_2015-04-30以某种方式使用'=='不起作用。

The dateOfsession is a new parameter in the GA API and hence have little examples and documentation about it.

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

#2


Everything you send to the segment, actually anything you send to any of the parameter to the reporting API must be URL encoded.

您发送到细分受众群的所有内容,实际上您发送到报告API的任何参数的任何内容都必须进行URL编码。

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

becomes

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

That should work. the only thing I am not sure about is if the ; needs to be encode or not. You may need to test that. I know the == does. If it doesn't work let me know and I will add a few segments and do some testing myself.

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