1+ // Copyright(c) 2020 Google Inc.
2+ //
3+ // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4+ // use this file except in compliance with the License. You may obtain a copy of
5+ // the License at
6+ //
7+ // http://www.apache.org/licenses/LICENSE-2.0
8+ //
9+ // Unless required by applicable law or agreed to in writing, software
10+ // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11+ // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12+ // License for the specific language governing permissions and limitations under
13+ // the License.
14+
15+ /* Google Analytics Data API sample quickstart application.
16+
17+ Example usage:
18+ dotnet restore
19+ dotnet run <GA4 property ID>
20+
21+ This application demonstrates the usage of the Analytics Data API using
22+ service account credentials. For more information on service accounts, see
23+
24+ https://cloud.google.com/iam/docs/understanding-service-accounts
25+
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 ;
53+ using System ;
54+
55+ namespace AnalyticsSamples
56+ {
57+ class QuickStart
58+ {
59+ static void SampleRunReport ( string propertyId )
60+ {
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 ( ) ;
64+
65+ // Initialize request argument(s)
66+ RunReportRequest request = new RunReportRequest
67+ {
68+ Entity = new Entity { PropertyId = propertyId } ,
69+ Dimensions = { new Dimension { Name = "city" } , } ,
70+ Metrics = { new Metric { Name = "activeUsers" } , } ,
71+ DateRanges = { new DateRange { StartDate = "2020-03-31" , EndDate = "today" } , } ,
72+ } ;
73+
74+ // Make the request
75+ RunReportResponse response = client . RunReport ( request ) ;
76+
77+ Console . WriteLine ( "Report result:" ) ;
78+ foreach ( Row row in response . Rows )
79+ {
80+ Console . WriteLine ( "{0}, {1}" , row . DimensionValues [ 0 ] . Value , row . MetricValues [ 0 ] . Value ) ;
81+ }
82+ }
83+
84+ static int Main ( string [ ] args )
85+ {
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 ) ;
94+ return 0 ;
95+ }
96+ }
97+ }
98+ // [END analyticsdata_quickstart]
0 commit comments