.Net对接Google Analytics API

1 篇文章 0 订阅
1 篇文章 0 订阅
  1. 先创建客户端
    1. 教程地址:https://developers.google.com/analytics/devguides/reporting/data/v1/quickstart-client-libraries?hl=zh-cn#.net在这里插入图片描述

    2. 将下载的配置文件复制到项目,并设置为始终复制
      image.png

    3. 将配置文件中的 client_email添加访问权限
      image.png

    4. Nuget添加引用:Google.Analytics.Data.V1Beta

    5. 使用官方的测试代码

      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调用的名称
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值