【问题标题】:Retrieve data from Google Analytics API with .NET, multiple metrics?使用 .NET、多个指标从 Google Analytics API 检索数据?
【发布时间】:2014-03-29 06:17:45
【问题描述】:

我正在编写这个控制台应用程序来获取一些基本的“Google Analytics API 数据”逻辑。一切工作正常,但我不想添加更多指标来查询谷歌分析 API。我不想添加 ga:newvisitors、ga:reaccuringvisitors 等。我如何在下面的代码中实现呢?

这是一段相关的代码:

var gas = new AnalyticsService(new BaseClientService.Initializer()
      {
       HttpClientInitializer = credential,
       ApplicationName = "TestGoogleAnalytics",
       });

var r = gas.Data.Ga.Get("ga:ProfileID", "2010-02-24", "2014-02-24", "ga:visitors");

//Specify some addition query parameters
r.Dimensions = "ga:pagePath";
r.Sort = "-ga:visitors";
r.MaxResults = 10000;

//Execute and fetch the results of our query
Google.Apis.Analytics.v3.Data.GaData d = r.Execute();
foreach (KeyValuePair<string, string> kvp in d.TotalsForAllResults)
    {
    Console.WriteLine("Total Visitors:" +" " + kvp.Value);
    }

Console.WriteLine(d.TotalsForAllResults.Keys + " = " + d.TotalsForAllResults.Values);
Console.ReadLine();

谢谢

【问题讨论】:

    标签: c# .net google-analytics google-analytics-api google-api-client


    【解决方案1】:

    指标是获取请求中的最后一项,只需将它们用逗号分隔即可。

    var r = gas.Data.Ga.Get("ga:ProfileID", "2010-02-24", "2014-02-24", "ga:visitors,ga:newvisitors,ga:reaccuringvisitors");
    

    这样阅读可能更容易:

    string Metrics = "ga:visitors,ga:newvisitors,ga:reaccuringvisitors";
    var r = gas.Data.Ga.Get("ga:ProfileID", "2010-02-24", "2014-02-24", Metrics);
    

    确保你的名字对我来说看起来不正确,但我没有检查:Dimension and metric reference

    更新:ga:newvisitors 早在 2009 年就被删除了。我找不到任何对 ga:reaccuringvisitors 的引用。确保您请求的指标正确。

    【讨论】:

    • 嗯,好吧,我做了类似的事情,但我猜的代码格式错误。谢谢,我会试试这个=)
    • 哦,我不知道他们删除了它,我需要更多的统计数据来绘制图表。 Reacurringvisitors 不存在,但我认为可以使用 ga:percentNewVisits。但是我在运行代码时收到此错误,当我像这样执行“r”时出现错误 Google.Apis.Analytics.v3.Data.GaData d = r.Execute();错误说 - ““ids”的参数验证失败
    • ga:ProfileID 是您要访问的 GA 中的配置文件的 ID。转到视图的管理员,它在视图设置下 - 视图 ID 它是一个你必须输入 ga: 的数字 (ga:78110423)
    • 现在可以使用了,谢谢!我在使用“字符串指标”变体时遇到了 id-problem,但您提供的其他替代方法有效。我现在得到 Total visitor 和 PercentNewVisits =)
    猜你喜欢
    • 2014-03-23
    • 1970-01-01
    • 2022-01-12
    • 1970-01-01
    • 1970-01-01
    • 2012-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多