Universal Analytics C# SDK 是否与 GA4 兼容?

问题描述 投票:0回答:2

我正在使用

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

我已经检查了这篇文章“我如何在 GA4 中获取视图 ID”,但是我的属性已经存在并且我没有看到创建后修改它们的选项。

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();
}
c# google-analytics google-analytics-api google-api-dotnet-client
2个回答
0
投票

您需要使用 Google Analytics Data API V1(目前处于 alpha 阶段)才能访问您的 GA4 属性。这是一个 quick start sample for .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;
        }
    }
}

-1
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.