1414
1515/* Google Analytics Data API sample quickstart application.
1616
17- Example usage:
18- dotnet restore
19- dotnet run <GA4 property ID>
17+ Before you start the application, please review the comments starting with
18+ "TODO(developer)" and update the code to use correct values.
2019
21- This application demonstrates the usage of the Analytics Data API using
22- service account credentials. For more information on service accounts, see
20+ This application demonstrates the usage of the Analytics Data API using service
21+ account credentials.
2322
24- https://cloud.google.com/iam/docs/understanding-service-accounts
23+ Usage:
24+ cd analytics-data/QuickStart
25+ dotnet restore
26+ dotnet run
27+ */
2528
26- The following document provides instructions on setting service account
27- credentials for your application:
28-
29- https://cloud.google.com/docs/authentication/production
30-
31- In a nutshell, you need to:
32- 1. Create a service account and download the key JSON file.
33-
34- https://cloud.google.com/docs/authentication/production#creating_a_service_account
35-
36- 2. Provide service account credentials using one of the following options:
37- - set the GOOGLE_APPLICATION_CREDENTIALS environment variable, the API
38- client will use the value of this variable to find the service account key
39- JSON file.
40-
41- https://cloud.google.com/docs/authentication/production#setting_the_environment_variable
42-
43- OR
44- - manually pass the path to the service account key JSON file to the API client
45- by specifying the keyFilename parameter in the constructor:
46- https://cloud.google.com/docs/authentication/production#passing_the_path_to_the_service_account_key_in_code
47-
48- */
49-
50- // [START analyticsdata_quickstart]
51-
52- using Google . Analytics . Data . V1Alpha ;
29+ // [START google_analytics_data_quickstart]
30+ using Google . Analytics . Data . V1Beta ;
31+ using Google . Api . Gax ;
5332using System ;
5433
5534namespace AnalyticsSamples
5635{
5736 class QuickStart
5837 {
59- static void SampleRunReport ( string propertyId )
38+ static void SampleRunReport ( string propertyId = "YOUR-GA4-PROPERTY-ID" , string credentialsJsonPath = "" )
6039 {
61- // Using a default constructor instructs the client to use the credentials
62- // specified in GOOGLE_APPLICATION_CREDENTIALS environment variable.
63- AlphaAnalyticsDataClient client = AlphaAnalyticsDataClient . Create ( ) ;
40+ /**
41+ * TODO(developer): Uncomment this variable and replace with your
42+ * Google Analytics 4 property ID before running the sample.
43+ */
44+ // propertyId = "YOUR-GA4-PROPERTY-ID";
45+
46+ // [START google_analytics_data_initialize]
47+ /**
48+ * TODO(developer): Uncomment this variable and replace with a valid path to
49+ * the credentials.json file for your service account downloaded from the
50+ * Cloud Console.
51+ * Otherwise, default service account credentials will be derived from
52+ * the GOOGLE_APPLICATION_CREDENTIALS environment variable.
53+ */
54+ // credentialsJsonPath = "/path/to/credentials.json";
55+
56+ BetaAnalyticsDataClient client ;
57+ if ( String . IsNullOrEmpty ( credentialsJsonPath ) )
58+ {
59+ // Using a default constructor instructs the client to use the credentials
60+ // specified in GOOGLE_APPLICATION_CREDENTIALS environment variable.
61+ client = BetaAnalyticsDataClient . Create ( ) ;
62+ }
63+ else
64+ {
65+ // Explicitly use service account credentials by specifying
66+ // the private key file.
67+ client = new BetaAnalyticsDataClientBuilder
68+ {
69+ CredentialsPath = credentialsJsonPath
70+ } . Build ( ) ;
71+ }
72+ // [END google_analytics_data_initialize]
6473
74+ // [START google_analytics_data_run_report]
6575 // Initialize request argument(s)
6676 RunReportRequest request = new RunReportRequest
6777 {
68- Entity = new Entity { PropertyId = propertyId } ,
78+ Property = "property/" + propertyId ,
6979 Dimensions = { new Dimension { Name = "city" } , } ,
7080 Metrics = { new Metric { Name = "activeUsers" } , } ,
7181 DateRanges = { new DateRange { StartDate = "2020-03-31" , EndDate = "today" } , } ,
7282 } ;
7383
7484 // Make the request
75- RunReportResponse response = client . RunReport ( request ) ;
85+ PagedEnumerable < RunReportResponse , DimensionHeader > response = client . RunReport ( request ) ;
86+ // [END google_analytics_data_run_report]
7687
88+ // [START google_analytics_data_print_report]
7789 Console . WriteLine ( "Report result:" ) ;
78- foreach ( Row row in response . Rows )
90+ foreach ( RunReportResponse page in response . AsRawResponses ( ) )
7991 {
80- Console . WriteLine ( "{0}, {1}" , row . DimensionValues [ 0 ] . Value , row . MetricValues [ 0 ] . Value ) ;
92+ foreach ( Row row in page . Rows )
93+ {
94+ Console . WriteLine ( "{0}, {1}" , row . DimensionValues [ 0 ] . Value , row . MetricValues [ 0 ] . Value ) ;
95+ }
8196 }
97+ // [END google_analytics_data_print_report]
8298 }
83-
8499 static int Main ( string [ ] args )
85100 {
86- if ( args . Length == 0 || args . Length > 2 )
87- {
88- Console . WriteLine ( "Arguments: <GA4 property ID>" ) ;
89- Console . WriteLine ( "A GA4 property id parameter is required to make a query to the Google Analytics Data API." ) ;
90- return 1 ;
91- }
92- string propertyId = args [ 0 ] ;
93- SampleRunReport ( propertyId ) ;
101+ SampleRunReport ( ) ;
94102 return 0 ;
95103 }
96104 }
97105}
98- // [END analyticsdata_quickstart ]
106+ // [END google_analytics_data_quickstart ]
0 commit comments