Skip to content

Commit 7146af0

Browse files
committed
chore(dotnet): update to .NET 8
Includes a minor fix for reading credentials since the `GoogleCredentials.FromStream` method is deprecated.
1 parent b1aabf3 commit 7146af0

6 files changed

Lines changed: 66 additions & 36 deletions

File tree

analytics-admin/QuickStart/QuickStart.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ JSON file.
4949

5050
// [START analyticsadmin_quickstart]
5151

52+
using System;
5253
using Google.Analytics.Admin.V1Beta;
5354
using Google.Api.Gax;
54-
using System;
5555

5656
namespace AnalyticsSamples
5757
{
@@ -60,9 +60,10 @@ class QuickStart
6060
static void Main(string[] args)
6161
{
6262
AnalyticsAdminServiceClient client = AnalyticsAdminServiceClient.Create();
63-
PagedEnumerable<ListAccountsResponse, Account> response =
64-
client.ListAccounts( new ListAccountsRequest() );
65-
foreach( Account account in response )
63+
PagedEnumerable<ListAccountsResponse, Account> response = client.ListAccounts(
64+
new ListAccountsRequest()
65+
);
66+
foreach (Account account in response)
6667
{
6768
Console.WriteLine("Account name: {0}", account.Name);
6869
Console.WriteLine("Display name: {0}", account.DisplayName);
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
<Project Sdk="Microsoft.NET.Sdk">
2-
32
<PropertyGroup>
43
<OutputType>Exe</OutputType>
5-
<TargetFrameworks>net6.0;net462</TargetFrameworks>
6-
<TargetFrameworks Condition=" '$(OS)' != 'Windows_NT' ">net6.0</TargetFrameworks>
4+
<TargetFrameworks>net8.0;net462</TargetFrameworks>
5+
<TargetFrameworks Condition=" '$(OS)' != 'Windows_NT' ">net8.0</TargetFrameworks>
76
</PropertyGroup>
87

98
<ItemGroup>
109
<PackageReference Include="Google.Analytics.Admin.V1Beta" Version="1.0.0-beta10" />
1110
</ItemGroup>
12-
1311
</Project>

analytics-data/QuickStart/QuickStart.cs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ dotnet run
2727
*/
2828

2929
// [START analyticsdata_quickstart]
30-
using Google.Analytics.Data.V1Beta;
3130
using System;
31+
using Google.Analytics.Data.V1Beta;
3232

3333
namespace AnalyticsSamples
3434
{
3535
class QuickStart
3636
{
37-
static void SampleRunReport(string propertyId="YOUR-GA4-PROPERTY-ID")
37+
static void SampleRunReport(string propertyId = "YOUR-GA4-PROPERTY-ID")
3838
{
3939
/**
4040
* TODO(developer): Uncomment this variable and replace with your
@@ -53,9 +53,12 @@ static void SampleRunReport(string propertyId="YOUR-GA4-PROPERTY-ID")
5353
RunReportRequest request = new RunReportRequest
5454
{
5555
Property = "properties/" + propertyId,
56-
Dimensions = { new Dimension{ Name="city"}, },
57-
Metrics = { new Metric{ Name="activeUsers"}, },
58-
DateRanges = { new DateRange{ StartDate="2020-03-31", EndDate="today"}, },
56+
Dimensions = { new Dimension { Name = "city" } },
57+
Metrics = { new Metric { Name = "activeUsers" } },
58+
DateRanges =
59+
{
60+
new DateRange { StartDate = "2020-03-31", EndDate = "today" },
61+
},
5962
};
6063

6164
// Make the request
@@ -64,17 +67,25 @@ static void SampleRunReport(string propertyId="YOUR-GA4-PROPERTY-ID")
6467

6568
// [START analyticsdata_run_report_response]
6669
Console.WriteLine("Report result:");
67-
foreach(Row row in response.Rows)
70+
foreach (Row row in response.Rows)
6871
{
69-
Console.WriteLine("{0}, {1}", row.DimensionValues[0].Value, row.MetricValues[0].Value);
72+
Console.WriteLine(
73+
"{0}, {1}",
74+
row.DimensionValues[0].Value,
75+
row.MetricValues[0].Value
76+
);
7077
}
7178
// [END analyticsdata_run_report_response]
7279
}
80+
7381
static int Main(string[] args)
7482
{
75-
if (args.Length > 0) {
83+
if (args.Length > 0)
84+
{
7685
SampleRunReport(args[0]);
77-
} else {
86+
}
87+
else
88+
{
7889
SampleRunReport();
7990
}
8091
return 0;
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
<Project Sdk="Microsoft.NET.Sdk">
2-
32
<PropertyGroup>
43
<OutputType>Exe</OutputType>
5-
<TargetFrameworks>net6.0;net462</TargetFrameworks>
6-
<TargetFrameworks Condition=" '$(OS)' != 'Windows_NT' ">net6.0</TargetFrameworks>
4+
<TargetFrameworks>net8.0;net462</TargetFrameworks>
5+
<TargetFrameworks Condition=" '$(OS)' != 'Windows_NT' ">net8.0</TargetFrameworks>
76
</PropertyGroup>
87

98
<ItemGroup>
109
<PackageReference Include="Google.Analytics.Data.V1Beta" Version="2.0.0-beta10" />
1110
</ItemGroup>
12-
1311
</Project>

analytics-data/QuickStartJsonCredentials/QuickStartJsonCredentials.cs

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,21 @@ dotnet run
2727
*/
2828

2929
// [START analyticsdata_json_credentials_quickstart]
30+
using System;
31+
using System.IO;
3032
using Google.Analytics.Data.V1Beta;
3133
using Google.Api.Gax;
32-
using System;
34+
using Google.Apis.Auth;
35+
using Google.Apis.Auth.OAuth2;
3336

3437
namespace AnalyticsSamples
3538
{
3639
class QuickStartJsonCredentials
3740
{
38-
static void SampleRunReport(string propertyId="YOUR-GA4-PROPERTY-ID",
39-
string credentialsJsonPath="YOUR-CREDENTIALS-FILE")
41+
static void SampleRunReport(
42+
string propertyId = "YOUR-GA4-PROPERTY-ID",
43+
string credentialsJsonPath = "YOUR-CREDENTIALS-FILE"
44+
)
4045
{
4146
/**
4247
* TODO(developer): Uncomment this variable and replace with your
@@ -54,11 +59,19 @@ static void SampleRunReport(string propertyId="YOUR-GA4-PROPERTY-ID",
5459
*/
5560
// credentialsJsonPath = "/path/to/credentials.json";
5661

62+
GoogleCredential credential;
63+
using (var stream = new FileStream(credentialsJsonPath, FileMode.Open, FileAccess.Read))
64+
{
65+
credential = CredentialFactory
66+
.FromStream<ServiceAccountCredential>(stream)
67+
.ToGoogleCredential();
68+
}
69+
5770
// Explicitly use service account credentials by specifying
5871
// the private key file.
5972
BetaAnalyticsDataClient client = new BetaAnalyticsDataClientBuilder
6073
{
61-
CredentialsPath = credentialsJsonPath
74+
GoogleCredential = credential,
6275
}.Build();
6376
// [END analyticsdata_json_credentials_initialize]
6477

@@ -67,9 +80,12 @@ static void SampleRunReport(string propertyId="YOUR-GA4-PROPERTY-ID",
6780
RunReportRequest request = new RunReportRequest
6881
{
6982
Property = "properties/" + propertyId,
70-
Dimensions = { new Dimension{ Name="city"}, },
71-
Metrics = { new Metric{ Name="activeUsers"}, },
72-
DateRanges = { new DateRange{ StartDate="2020-03-31", EndDate="today"}, },
83+
Dimensions = { new Dimension { Name = "city" } },
84+
Metrics = { new Metric { Name = "activeUsers" } },
85+
DateRanges =
86+
{
87+
new DateRange { StartDate = "2020-03-31", EndDate = "today" },
88+
},
7389
};
7490

7591
// Make the request
@@ -80,17 +96,25 @@ static void SampleRunReport(string propertyId="YOUR-GA4-PROPERTY-ID",
8096
// For more information on processing paged responses, see:
8197
// https://cloud.google.com/dotnet/docs/reference/help/page-streaming
8298
Console.WriteLine("Report result:");
83-
foreach(Row row in response.Rows)
99+
foreach (Row row in response.Rows)
84100
{
85-
Console.WriteLine("{0}, {1}", row.DimensionValues[0].Value, row.MetricValues[0].Value);
101+
Console.WriteLine(
102+
"{0}, {1}",
103+
row.DimensionValues[0].Value,
104+
row.MetricValues[0].Value
105+
);
86106
}
87107
// [END analyticsdata_json_credentials_run_report_response]
88108
}
109+
89110
static int Main(string[] args)
90111
{
91-
if (args.Length > 0) {
112+
if (args.Length > 0)
113+
{
92114
SampleRunReport(args[0], args[1]);
93-
} else {
115+
}
116+
else
117+
{
94118
SampleRunReport();
95119
}
96120
return 0;
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
<Project Sdk="Microsoft.NET.Sdk">
2-
32
<PropertyGroup>
43
<OutputType>Exe</OutputType>
5-
<TargetFrameworks>net6.0;net462</TargetFrameworks>
6-
<TargetFrameworks Condition=" '$(OS)' != 'Windows_NT' ">net6.0</TargetFrameworks>
4+
<TargetFrameworks>net8.0;net462</TargetFrameworks>
5+
<TargetFrameworks Condition=" '$(OS)' != 'Windows_NT' ">net8.0</TargetFrameworks>
76
</PropertyGroup>
87

98
<ItemGroup>
109
<PackageReference Include="Google.Analytics.Data.V1Beta" Version="2.0.0-beta10" />
1110
</ItemGroup>
12-
1311
</Project>

0 commit comments

Comments
 (0)