From ea961ad4a278f786e2fa81ac8912091e59b3ad06 Mon Sep 17 00:00:00 2001 From: Josh Radcliff Date: Mon, 9 Mar 2026 10:58:18 -0400 Subject: [PATCH] chore(dotnet): update to .NET 8 Includes a minor fix for reading credentials since the `GoogleCredentials.FromStream` method is deprecated. --- analytics-admin/QuickStart/QuickStart.cs | 9 ++-- analytics-admin/QuickStart/QuickStart.csproj | 6 +-- analytics-data/QuickStart/QuickStart.cs | 29 ++++++++---- analytics-data/QuickStart/QuickStart.csproj | 6 +-- .../QuickStartJsonCredentials.cs | 46 ++++++++++++++----- .../QuickStartJsonCredentials.csproj | 6 +-- 6 files changed, 66 insertions(+), 36 deletions(-) diff --git a/analytics-admin/QuickStart/QuickStart.cs b/analytics-admin/QuickStart/QuickStart.cs index 8669d46..74c46ee 100644 --- a/analytics-admin/QuickStart/QuickStart.cs +++ b/analytics-admin/QuickStart/QuickStart.cs @@ -49,9 +49,9 @@ JSON file. // [START analyticsadmin_quickstart] +using System; using Google.Analytics.Admin.V1Beta; using Google.Api.Gax; -using System; namespace AnalyticsSamples { @@ -60,9 +60,10 @@ class QuickStart static void Main(string[] args) { AnalyticsAdminServiceClient client = AnalyticsAdminServiceClient.Create(); - PagedEnumerable response = - client.ListAccounts( new ListAccountsRequest() ); - foreach( Account account in response ) + PagedEnumerable response = client.ListAccounts( + new ListAccountsRequest() + ); + foreach (Account account in response) { Console.WriteLine("Account name: {0}", account.Name); Console.WriteLine("Display name: {0}", account.DisplayName); diff --git a/analytics-admin/QuickStart/QuickStart.csproj b/analytics-admin/QuickStart/QuickStart.csproj index bc0d474..96d1805 100644 --- a/analytics-admin/QuickStart/QuickStart.csproj +++ b/analytics-admin/QuickStart/QuickStart.csproj @@ -1,13 +1,11 @@ - Exe - net6.0;net462 - net6.0 + net8.0;net462 + net8.0 - diff --git a/analytics-data/QuickStart/QuickStart.cs b/analytics-data/QuickStart/QuickStart.cs index 0904a04..76c359d 100644 --- a/analytics-data/QuickStart/QuickStart.cs +++ b/analytics-data/QuickStart/QuickStart.cs @@ -27,14 +27,14 @@ dotnet run */ // [START analyticsdata_quickstart] -using Google.Analytics.Data.V1Beta; using System; +using Google.Analytics.Data.V1Beta; namespace AnalyticsSamples { class QuickStart { - static void SampleRunReport(string propertyId="YOUR-GA4-PROPERTY-ID") + static void SampleRunReport(string propertyId = "YOUR-GA4-PROPERTY-ID") { /** * TODO(developer): Uncomment this variable and replace with your @@ -53,9 +53,12 @@ static void SampleRunReport(string propertyId="YOUR-GA4-PROPERTY-ID") RunReportRequest request = new RunReportRequest { Property = "properties/" + propertyId, - Dimensions = { new Dimension{ Name="city"}, }, - Metrics = { new Metric{ Name="activeUsers"}, }, - DateRanges = { new DateRange{ StartDate="2020-03-31", EndDate="today"}, }, + Dimensions = { new Dimension { Name = "city" } }, + Metrics = { new Metric { Name = "activeUsers" } }, + DateRanges = + { + new DateRange { StartDate = "2020-03-31", EndDate = "today" }, + }, }; // Make the request @@ -64,17 +67,25 @@ static void SampleRunReport(string propertyId="YOUR-GA4-PROPERTY-ID") // [START analyticsdata_run_report_response] Console.WriteLine("Report result:"); - foreach(Row row in response.Rows) + foreach (Row row in response.Rows) { - Console.WriteLine("{0}, {1}", row.DimensionValues[0].Value, row.MetricValues[0].Value); + Console.WriteLine( + "{0}, {1}", + row.DimensionValues[0].Value, + row.MetricValues[0].Value + ); } // [END analyticsdata_run_report_response] } + static int Main(string[] args) { - if (args.Length > 0) { + if (args.Length > 0) + { SampleRunReport(args[0]); - } else { + } + else + { SampleRunReport(); } return 0; diff --git a/analytics-data/QuickStart/QuickStart.csproj b/analytics-data/QuickStart/QuickStart.csproj index 00b4a40..9cf5f22 100644 --- a/analytics-data/QuickStart/QuickStart.csproj +++ b/analytics-data/QuickStart/QuickStart.csproj @@ -1,13 +1,11 @@ - Exe - net6.0;net462 - net6.0 + net8.0;net462 + net8.0 - diff --git a/analytics-data/QuickStartJsonCredentials/QuickStartJsonCredentials.cs b/analytics-data/QuickStartJsonCredentials/QuickStartJsonCredentials.cs index 8f101bb..dd42828 100644 --- a/analytics-data/QuickStartJsonCredentials/QuickStartJsonCredentials.cs +++ b/analytics-data/QuickStartJsonCredentials/QuickStartJsonCredentials.cs @@ -27,16 +27,21 @@ dotnet run */ // [START analyticsdata_json_credentials_quickstart] +using System; +using System.IO; using Google.Analytics.Data.V1Beta; using Google.Api.Gax; -using System; +using Google.Apis.Auth; +using Google.Apis.Auth.OAuth2; namespace AnalyticsSamples { class QuickStartJsonCredentials { - static void SampleRunReport(string propertyId="YOUR-GA4-PROPERTY-ID", - string credentialsJsonPath="YOUR-CREDENTIALS-FILE") + static void SampleRunReport( + string propertyId = "YOUR-GA4-PROPERTY-ID", + string credentialsJsonPath = "YOUR-CREDENTIALS-FILE" + ) { /** * TODO(developer): Uncomment this variable and replace with your @@ -54,11 +59,19 @@ static void SampleRunReport(string propertyId="YOUR-GA4-PROPERTY-ID", */ // credentialsJsonPath = "/path/to/credentials.json"; + GoogleCredential credential; + using (var stream = new FileStream(credentialsJsonPath, FileMode.Open, FileAccess.Read)) + { + credential = CredentialFactory + .FromStream(stream) + .ToGoogleCredential(); + } + // Explicitly use service account credentials by specifying // the private key file. BetaAnalyticsDataClient client = new BetaAnalyticsDataClientBuilder { - CredentialsPath = credentialsJsonPath + GoogleCredential = credential, }.Build(); // [END analyticsdata_json_credentials_initialize] @@ -67,9 +80,12 @@ static void SampleRunReport(string propertyId="YOUR-GA4-PROPERTY-ID", RunReportRequest request = new RunReportRequest { Property = "properties/" + propertyId, - Dimensions = { new Dimension{ Name="city"}, }, - Metrics = { new Metric{ Name="activeUsers"}, }, - DateRanges = { new DateRange{ StartDate="2020-03-31", EndDate="today"}, }, + Dimensions = { new Dimension { Name = "city" } }, + Metrics = { new Metric { Name = "activeUsers" } }, + DateRanges = + { + new DateRange { StartDate = "2020-03-31", EndDate = "today" }, + }, }; // Make the request @@ -80,17 +96,25 @@ static void SampleRunReport(string propertyId="YOUR-GA4-PROPERTY-ID", // For more information on processing paged responses, see: // https://cloud.google.com/dotnet/docs/reference/help/page-streaming Console.WriteLine("Report result:"); - foreach(Row row in response.Rows) + foreach (Row row in response.Rows) { - Console.WriteLine("{0}, {1}", row.DimensionValues[0].Value, row.MetricValues[0].Value); + Console.WriteLine( + "{0}, {1}", + row.DimensionValues[0].Value, + row.MetricValues[0].Value + ); } // [END analyticsdata_json_credentials_run_report_response] } + static int Main(string[] args) { - if (args.Length > 0) { + if (args.Length > 0) + { SampleRunReport(args[0], args[1]); - } else { + } + else + { SampleRunReport(); } return 0; diff --git a/analytics-data/QuickStartJsonCredentials/QuickStartJsonCredentials.csproj b/analytics-data/QuickStartJsonCredentials/QuickStartJsonCredentials.csproj index 00b4a40..9cf5f22 100644 --- a/analytics-data/QuickStartJsonCredentials/QuickStartJsonCredentials.csproj +++ b/analytics-data/QuickStartJsonCredentials/QuickStartJsonCredentials.csproj @@ -1,13 +1,11 @@ - Exe - net6.0;net462 - net6.0 + net8.0;net462 + net8.0 -