1414
1515/* Google Analytics Data API sample quickstart application.
1616
17- Before you start the application, please review the comments starting with
18- "TODO(developer)" and update the code to use correct values.
17+ Example usage:
18+ dotnet restore
19+ dotnet run <GA4 property ID>
1920
20- This application demonstrates the usage of the Analytics Data API using service
21- account credentials.
21+ This application demonstrates the usage of the Analytics Data API using
22+ service account credentials. For more information on service accounts, see
2223
23- Usage:
24- cd analytics-data/QuickStart
25- dotnet restore
26- dotnet run
27- */
24+ https://cloud.google.com/iam/docs/understanding-service-accounts
2825
29- // [START google_analytics_data_quickstart]
30- using Google . Analytics . Data . V1Beta ;
31- using Google . Api . Gax ;
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 ;
3253using System ;
3354
3455namespace AnalyticsSamples
3556{
3657 class QuickStart
3758 {
38- static void SampleRunReport ( string propertyId = "YOUR-GA4-PROPERTY-ID" , string credentialsJsonPath = "" )
59+ static void SampleRunReport ( string propertyId )
3960 {
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- credentialsJsonPath = "/usr/local/google/home/ikuleshov/Downloads/google.com_ga-whitelisting-19c61b449a97.json" ;
56-
57- BetaAnalyticsDataClient client ;
58- if ( String . IsNullOrEmpty ( credentialsJsonPath ) )
59- {
60- // Using a default constructor instructs the client to use the credentials
61- // specified in GOOGLE_APPLICATION_CREDENTIALS environment variable.
62- client = BetaAnalyticsDataClient . Create ( ) ;
63- }
64- else
65- {
66- // Explicitly use service account credentials by specifying
67- // the private key file.
68- client = new BetaAnalyticsDataClientBuilder
69- {
70- CredentialsPath = credentialsJsonPath
71- } . Build ( ) ;
72- }
73- // [END google_analytics_data_initialize]
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 ( ) ;
7464
75- // [START google_analytics_data_run_report]
7665 // Initialize request argument(s)
7766 RunReportRequest request = new RunReportRequest
7867 {
79- Property = "property/" + propertyId ,
68+ Entity = new Entity { PropertyId = propertyId } ,
8069 Dimensions = { new Dimension { Name = "city" } , } ,
8170 Metrics = { new Metric { Name = "activeUsers" } , } ,
8271 DateRanges = { new DateRange { StartDate = "2020-03-31" , EndDate = "today" } , } ,
8372 } ;
8473
8574 // Make the request
86- PagedEnumerable < RunReportResponse , DimensionHeader > response = client . RunReport ( request ) ;
87- // [END google_analytics_data_run_report]
75+ RunReportResponse response = client . RunReport ( request ) ;
8876
89- // [START google_analytics_data_print_report]
9077 Console . WriteLine ( "Report result:" ) ;
91- foreach ( RunReportResponse page in response . AsRawResponses ( ) )
78+ foreach ( Row row in response . Rows )
9279 {
93- foreach ( Row row in page . Rows )
94- {
95- Console . WriteLine ( "{0}, {1}" , row . DimensionValues [ 0 ] . Value , row . MetricValues [ 0 ] . Value ) ;
96- }
80+ Console . WriteLine ( "{0}, {1}" , row . DimensionValues [ 0 ] . Value , row . MetricValues [ 0 ] . Value ) ;
9781 }
98- // [END google_analytics_data_print_report]
9982 }
83+
10084 static int Main ( string [ ] args )
10185 {
102- SampleRunReport ( ) ;
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 ) ;
10394 return 0 ;
10495 }
10596 }
10697}
107- // [END google_analytics_data_quickstart ]
98+ // [END analyticsdata_quickstart ]
0 commit comments