@@ -27,16 +27,21 @@ dotnet run
2727 */
2828
2929// [START analyticsdata_json_credentials_quickstart]
30+ using System ;
31+ using System . IO ;
3032using Google . Analytics . Data . V1Beta ;
3133using Google . Api . Gax ;
32- using System ;
34+ using Google . Apis . Auth ;
35+ using Google . Apis . Auth . OAuth2 ;
3336
3437namespace 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 ;
0 commit comments