- 先创建客户端
-
教程地址:https://developers.google.com/analytics/devguides/reporting/data/v1/quickstart-client-libraries?hl=zh-cn#.net
-
将下载的配置文件复制到项目,并设置为始终复制
-
将配置文件中的
client_email
添加访问权限
-
Nuget添加引用:Google.Analytics.Data.V1Beta
-
使用官方的测试代码
public void SampleRunReport(string propertyId) { // 指定文件名 string fileName = "SmartAnalytics1.json"; // 获取当前应用程序域的基本目录路径 string basePath = AppDomain.CurrentDomain.BaseDirectory; // 拼接 "protos" 文件夹路径 string protosFolderPath = Path.Combine(basePath, "protos"); // 构建文件路径 string filePath = Path.Combine(protosFolderPath, fileName); string credential_path = filePath; System.Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", credential_path); BetaAnalyticsDataClient client = BetaAnalyticsDataClient.Create(); // Initialize request argument(s) RunReportRequest request = new RunReportRequest { Property = "properties/" + propertyId, Dimensions = { new Dimension{ Name="city"}, }, Metrics = { new Metric{ Name="activeUsers"}, }, DateRanges = { new DateRange{ StartDate="2024-06-11", EndDate="today"}, }, }; // Make the request var 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); } }
-
分享网址:
官方的维度和指标:https://developers.google.com/analytics/devguides/reporting/data/v1/api-schema?hl=zh-cn
维度和指标前后端对应:https://data.ga4spy.com/
这个网站很好用,可以通过前端的名称查询出API调用的名称