Skip to content

Commit 255aa86

Browse files
authored
Merge pull request #4 from googleanalytics/quickstart
docs: update sample to use the Google Analytics Data V1 beta
2 parents d4904bd + 3af53c4 commit 255aa86

2 files changed

Lines changed: 62 additions & 53 deletions

File tree

analytics-data/QuickStart/QuickStart.cs

Lines changed: 60 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -14,85 +14,94 @@
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;
5332
using System;
5433

5534
namespace 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+
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]
6474

75+
// [START google_analytics_data_run_report]
6576
// Initialize request argument(s)
6677
RunReportRequest request = new RunReportRequest
6778
{
68-
Entity = new Entity{ PropertyId = propertyId },
79+
Property = "property/" + propertyId,
6980
Dimensions = { new Dimension{ Name="city"}, },
7081
Metrics = { new Metric{ Name="activeUsers"}, },
7182
DateRanges = { new DateRange{ StartDate="2020-03-31", EndDate="today"}, },
7283
};
7384

7485
// Make the request
75-
RunReportResponse response = client.RunReport(request);
86+
PagedEnumerable<RunReportResponse, DimensionHeader> response = client.RunReport(request);
87+
// [END google_analytics_data_run_report]
7688

89+
// [START google_analytics_data_print_report]
7790
Console.WriteLine("Report result:");
78-
foreach( Row row in response.Rows )
91+
foreach(RunReportResponse page in response.AsRawResponses())
7992
{
80-
Console.WriteLine("{0}, {1}", row.DimensionValues[0].Value, row.MetricValues[0].Value);
93+
foreach(Row row in page.Rows)
94+
{
95+
Console.WriteLine("{0}, {1}", row.DimensionValues[0].Value, row.MetricValues[0].Value);
96+
}
8197
}
98+
// [END google_analytics_data_print_report]
8299
}
83-
84100
static int Main(string[] args)
85101
{
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);
102+
SampleRunReport();
94103
return 0;
95104
}
96105
}
97106
}
98-
// [END analyticsdata_quickstart]
107+
// [END google_analytics_data_quickstart]

analytics-data/QuickStart/QuickStart.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
<TargetFramework>netcoreapp3.1</TargetFramework>
66
</PropertyGroup>
77

8-
<ItemGroup>
9-
<PackageReference Include="Google.Analytics.Data.V1Alpha" Version="1.0.0-alpha01" />
8+
<ItemGroup>
9+
<PackageReference Include="Google.Analytics.Data.V1Beta" Version="1.0.0-beta01" />
1010
</ItemGroup>
1111

1212
</Project>

0 commit comments

Comments
 (0)