2

我正在Google.Apis.AnalyticsReporting.v4为旧的谷歌分析视图使用库。如何将此代码转换为 GA4?我在代码中找不到关于将 View Id 切换为其他内容的行。

我已经检查了这篇文章 “How do I get view id in GA4”,但我的属性已经存在,并且我没有看到创建后修改它们的选项。

using (var svc = new AnalyticsReportingService(authInitializer.CreateInitializer()))
{
    var dateRange = new DateRange
    {
        StartDate = analyticsParams.From.ToString("yyyy-MM-dd"),
        EndDate = analyticsParams.To.ToString("yyyy-MM-dd")
    };
    var sessions = new Metric
    {
        Expression = "ga:sessions",
        Alias = "Sessions"
    };
    var date = new Dimension { Name = "ga:date" };

    var reportRequest = new ReportRequest
    {
        DateRanges = new List<DateRange> { dateRange },
        Dimensions = new List<Dimension> { date },
        Metrics = new List<Metric> { sessions },
        ViewId = analyticsParams.ViewId, // <------------------------- My view id
    };

    var getReportsRequest = new GetReportsRequest
    {
        ReportRequests = new List<ReportRequest> { reportRequest }
    };

    var batchRequest = svc.Reports.BatchGet(getReportsRequest);
    var response = batchRequest.Execute();

    var reports = response.Reports.First();

    return reports.Data.Rows.Select(x => new DataEntry()
    {
        Date = DateTime.ParseExact(x.Dimensions[0], "yyyyMMdd", CultureInfo.InvariantCulture),
        Value = int.Parse(x.Metrics[0].Values[0]),
    }).ToList();
}
4

2 回答 2

0

目前没有可用于 GA4 属性的 API。此外,GA4 不提供视图,您必须使用 BigQuery 以编程方式获取数据。

于 2020-11-13T18:44:57.247 回答
0

您需要使用Google Analytics Data API V1(目前处于 alpha 阶段)才能访问您的 GA4 属性。这是.NET 的快速入门示例,看起来与您尝试做的类似。

using Google.Analytics.Data.V1Alpha;
using System;

namespace AnalyticsSamples
{
    class QuickStart
    {
        static void SampleRunReport(string propertyId)
        {
            // Using a default constructor instructs the client to use the credentials
            // specified in GOOGLE_APPLICATION_CREDENTIALS environment variable.
            AlphaAnalyticsDataClient client = AlphaAnalyticsDataClient.Create();

            // Initialize request argument(s)
            RunReportRequest request = new RunReportRequest
            {
                Entity = new Entity{ PropertyId = propertyId },
                Dimensions = { new Dimension{ Name="city"}, },
                Metrics = { new Metric{ Name="activeUsers"}, },
                DateRanges = { new DateRange{ StartDate="2020-03-31", EndDate="today"}, },
            };

            // Make the request
            RunReportResponse response = client.RunReport(request);

            Console.WriteLine("Report result:");
            foreach( Row row in response.Rows )
            {
                Console.WriteLine("{0}, {1}", row.DimensionValues[0].Value, row.MetricValues[0].Value);
            }
        }

        static int Main(string[] args)
        {
            if (args.Length == 0 || args.Length > 2)
            {
                Console.WriteLine("Arguments: <GA4 property ID>");
                Console.WriteLine("A GA4 property id parameter is required to make a query to the Google Analytics Data API.");
                return 1;
            }
            string propertyId = args[0];
            SampleRunReport(propertyId);
            return 0;
        }
    }
}
于 2020-11-13T20:56:24.517 回答