Skip to content

Commit 29d4f06

Browse files
committed
Support command line args in samples
1 parent 8a7db92 commit 29d4f06

2 files changed

Lines changed: 105 additions & 97 deletions

File tree

analytics-data/QuickStart/QuickStart.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,11 @@ static void SampleRunReport(string propertyId="YOUR-GA4-PROPERTY-ID")
7272
}
7373
static int Main(string[] args)
7474
{
75-
SampleRunReport();
75+
if (args.Length > 0) {
76+
SampleRunReport(args[0]);
77+
} else {
78+
SampleRunReport();
79+
}
7680
return 0;
7781
}
7882
}
Lines changed: 100 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -1,96 +1,100 @@
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-
Before you start the application, please review the comments starting with
18-
"TODO(developer)" and update the code to use correct values.
19-
20-
This application demonstrates the usage of the Analytics Data API using service
21-
account credentials.
22-
23-
Usage:
24-
cd analytics-data/QuickStartJsonCredentials
25-
dotnet restore
26-
dotnet run
27-
*/
28-
29-
// [START analyticsdata_json_credentials_quickstart]
30-
using Google.Analytics.Data.V1Beta;
31-
using Google.Api.Gax;
32-
using System;
33-
34-
namespace AnalyticsSamples
35-
{
36-
class QuickStartJsonCredentials
37-
{
38-
static void SampleRunReport(string propertyId="YOUR-GA4-PROPERTY-ID",
39-
string credentialsJsonPath="YOUR-CREDENTIALS-FILE")
40-
{
41-
/**
42-
* TODO(developer): Uncomment this variable and replace with your
43-
* Google Analytics 4 property ID before running the sample.
44-
*/
45-
// propertyId = "YOUR-GA4-PROPERTY-ID";
46-
47-
// [START analyticsdata_json_credentials_initialize]
48-
/**
49-
* TODO(developer): Uncomment this variable and replace with a valid path to
50-
* the credentials.json file for your service account downloaded from the
51-
* Cloud Console.
52-
* Otherwise, default service account credentials will be derived from
53-
* the GOOGLE_APPLICATION_CREDENTIALS environment variable.
54-
*/
55-
// credentialsJsonPath = "/path/to/credentials.json";
56-
57-
// Explicitly use service account credentials by specifying
58-
// the private key file.
59-
BetaAnalyticsDataClient client = new BetaAnalyticsDataClientBuilder
60-
{
61-
CredentialsPath = credentialsJsonPath
62-
}.Build();
63-
// [END analyticsdata_json_credentials_initialize]
64-
65-
// [START analyticsdata_json_credentials_run_report]
66-
// Initialize request argument(s)
67-
RunReportRequest request = new RunReportRequest
68-
{
69-
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"}, },
73-
};
74-
75-
// Make the request
76-
RunReportResponse response = client.RunReport(request);
77-
// [END analyticsdata_json_credentials_run_report]
78-
79-
// [START analyticsdata_json_credentials_run_report_response]
80-
// For more information on processing paged responses, see:
81-
// https://cloud.google.com/dotnet/docs/reference/help/page-streaming
82-
Console.WriteLine("Report result:");
83-
foreach(Row row in response.Rows)
84-
{
85-
Console.WriteLine("{0}, {1}", row.DimensionValues[0].Value, row.MetricValues[0].Value);
86-
}
87-
// [END analyticsdata_json_credentials_run_report_response]
88-
}
89-
static int Main(string[] args)
90-
{
91-
SampleRunReport();
92-
return 0;
93-
}
94-
}
95-
}
96-
// [END analyticsdata_json_credentials_quickstart]
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+
Before you start the application, please review the comments starting with
18+
"TODO(developer)" and update the code to use correct values.
19+
20+
This application demonstrates the usage of the Analytics Data API using service
21+
account credentials.
22+
23+
Usage:
24+
cd analytics-data/QuickStartJsonCredentials
25+
dotnet restore
26+
dotnet run
27+
*/
28+
29+
// [START analyticsdata_json_credentials_quickstart]
30+
using Google.Analytics.Data.V1Beta;
31+
using Google.Api.Gax;
32+
using System;
33+
34+
namespace AnalyticsSamples
35+
{
36+
class QuickStartJsonCredentials
37+
{
38+
static void SampleRunReport(string propertyId="YOUR-GA4-PROPERTY-ID",
39+
string credentialsJsonPath="YOUR-CREDENTIALS-FILE")
40+
{
41+
/**
42+
* TODO(developer): Uncomment this variable and replace with your
43+
* Google Analytics 4 property ID before running the sample.
44+
*/
45+
// propertyId = "YOUR-GA4-PROPERTY-ID";
46+
47+
// [START analyticsdata_json_credentials_initialize]
48+
/**
49+
* TODO(developer): Uncomment this variable and replace with a valid path to
50+
* the credentials.json file for your service account downloaded from the
51+
* Cloud Console.
52+
* Otherwise, default service account credentials will be derived from
53+
* the GOOGLE_APPLICATION_CREDENTIALS environment variable.
54+
*/
55+
// credentialsJsonPath = "/path/to/credentials.json";
56+
57+
// Explicitly use service account credentials by specifying
58+
// the private key file.
59+
BetaAnalyticsDataClient client = new BetaAnalyticsDataClientBuilder
60+
{
61+
CredentialsPath = credentialsJsonPath
62+
}.Build();
63+
// [END analyticsdata_json_credentials_initialize]
64+
65+
// [START analyticsdata_json_credentials_run_report]
66+
// Initialize request argument(s)
67+
RunReportRequest request = new RunReportRequest
68+
{
69+
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"}, },
73+
};
74+
75+
// Make the request
76+
RunReportResponse response = client.RunReport(request);
77+
// [END analyticsdata_json_credentials_run_report]
78+
79+
// [START analyticsdata_json_credentials_run_report_response]
80+
// For more information on processing paged responses, see:
81+
// https://cloud.google.com/dotnet/docs/reference/help/page-streaming
82+
Console.WriteLine("Report result:");
83+
foreach(Row row in response.Rows)
84+
{
85+
Console.WriteLine("{0}, {1}", row.DimensionValues[0].Value, row.MetricValues[0].Value);
86+
}
87+
// [END analyticsdata_json_credentials_run_report_response]
88+
}
89+
static int Main(string[] args)
90+
{
91+
if (args.Length > 0) {
92+
SampleRunReport(args[0], args[1]);
93+
} else {
94+
SampleRunReport();
95+
}
96+
return 0;
97+
}
98+
}
99+
}
100+
// [END analyticsdata_json_credentials_quickstart]

0 commit comments

Comments
 (0)