3

My goal is to retrieve user's custom reports which was created in Google Analytics UI from Google Analytics API. I have registered application in Cloud console where received certificate file and generated developer email. Also configured access (granted all rights) for generated email on admin page in GA UI. To achieve my goal I am trying get segments from service, but I am not sure it is correct way to get custom reports. As a result getting only 13 default segments (with negative Ids), it is less than I can receive in Google APIs Explorer, for example one of it

{ "id": "9MaL7mLnQbWO3k52Ipni5A", "kind": "analytics#segment",
"selfLink": "https://www.googleapis.com/analytics/v3/management/segments/gaid::9MaL7mLnQbWO3k52Ipni5A", "segmentId": "gaid::9MaL7mLnQbWO3k52Ipni5A", "name": "Blog Comment Submitters", "definition": "ga:eventAction=@commentform" }

My question is it correct way to get custom reports? If it is, why I am getting only default segments?

Code:

private readonly string scope = AnalyticsService.Scopes.Analytics.GetStringValue();

private const string clientId = "xxx-xxx@developer.gserviceaccount.com";

private const string keyFile = @"C:\xxx-privatekey.p12";

string keyPassword = "notasecret";

var desc = GoogleAuthenticationServer.Description;
var key = new X509Certificate2(keyFile, keyPassword, X509KeyStorageFlags.Exportable);

var client = new AssertionFlowClient(desc, key){
    ServiceAccountId = clientId,
    Scope = scope
};

var auth = new OAuth2Authenticator<AssertionFlowClient>(client, AssertionFlowClient.GetState);

var gas = new AnalyticsService(new BaseClientService.Initializer { Authenticator = auth, });

var request = gas.Management.Segments.List();

var d = request.Execute();
4

2 回答 2

3

亚历山大,

很抱歉这么说,但你的代码不起作用:-)

您正在使用Management API,它无法直接访问数据,但用于管理帐户设置、用户等:

使用 Management API,您可以查询用户的帐户、网络媒体资源和视图(配置文件),确定用户配置了哪些账户段,甚至可以在配置的视图(配置文件)中检索有关目标的信息。

我建议使用Core Reporting API。鉴于您帖子的详细信息,我认为技术部分不会成为问题。

并回答您的问题 - 即使有直接的方法可以让您创建自定义报告,只需使用您在网络界面中看到的相同维度/指标/过滤器/细分。

查询结构非常简单,例如:

GET https://www.googleapis.com/analytics/v3/data/ga
  ?ids=ga:12345
  &start-date=2008-10-01
  &end-date=2008-10-31
  &metrics=ga:visits,ga:bounces
于 2013-10-01T07:16:24.243 回答
1

遗憾的是,您无法通过 API 访问自定义报告数据。这里有一个开放的功能请求: Expose Custom Reporting trough API

可能会对 Megalytic 工具感兴趣。它从 API 创建报告,您可以将其保存为模板,通过电子邮件或网络链接与他人共享,甚至使用您自己的封面或徽标进行品牌宣传。免责声明:我是 Megalytic 的创始人。

于 2013-11-26T16:58:07.767 回答