如何在 C# Google Analytics 实时报告 API 中使用维度?

How to use dimensions in C# Google Analytics Real Time Reporting API?

我正在尝试从 Google Analytics Real Time Reporting API (Google.Apis.Analytics.v3) 获取实时数据,以下代码工作正常并获取活跃用户。

我想不通的是如何从维度中获取数据?

外汇。此维度:rt:deviceCategory,它没有根据此的指标:https://developers.google.com/analytics/devguides/reporting/realtime/dimsmets/

如果有人能指出正确的方向,我将不胜感激。

            var credential = GetCredential().Result;
            using (var svc = new AnalyticsService(
                new BaseClientService.Initializer
                {
                    HttpClientInitializer = credential,
                    ApplicationName = "Google Analytics API Console"
                })
            )
            {
                GetRequest request;
                RealtimeData response;

                request = svc.Data.Realtime.Get("ga:XXXXX", "rt:activeUsers");         
                response = request.Execute();
                foreach (var row in response.Rows)
                {
                    foreach (string col in row)
                    {
                        Console.Write(col + " ");  // writes the value of the column
                    }
                    Console.Write("\r\n");
                }

我在这里找到了答案:https://github.com/LindaLawton/Google-Dotnet-Samples/tree/Genreated-samples1.0/Google%20Analytics%20API/v3

RealtimeSample.cs可以给Real Time增加维度API,只需要改变行号。 127 至:

if (piShared != null && property.GetValue(optional, null) != null) // TODO Test that we do not add values for items that are null

并在几个地方将 analyticsService 的 A 大写。然后用作:

                RealtimeSample.RealtimeGetOptionalParms param = new RealtimeSample.RealtimeGetOptionalParms();
                param.Dimensions = "rt:deviceCategory";
                response = RealtimeSample.Get(svc, "ga:XXXXX", "rt:activeUsers", param);
                foreach (var row in response.Rows)
                {
                    foreach (string col in row)
                    {
                        Console.Write(col + " ");  // writes the value of the column
                    }
                    Console.Write("\r\n");
                }