Skip to content

Commit 5ca2740

Browse files
committed
docs: added a sample
1 parent 2902743 commit 5ca2740

3 files changed

Lines changed: 144 additions & 0 deletions

File tree

analytics-data/AnalyticsData.sln

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 15
4+
VisualStudioVersion = 15.0.26124.0
5+
MinimumVisualStudioVersion = 15.0.26124.0
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "QuickStart", "QuickStart\QuickStart.csproj", "{7D9C4AE6-192A-4A99-9933-6AF9F4FE5ECA}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Debug|x64 = Debug|x64
12+
Debug|x86 = Debug|x86
13+
Release|Any CPU = Release|Any CPU
14+
Release|x64 = Release|x64
15+
Release|x86 = Release|x86
16+
EndGlobalSection
17+
GlobalSection(SolutionProperties) = preSolution
18+
HideSolutionNode = FALSE
19+
EndGlobalSection
20+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
21+
{7D9C4AE6-192A-4A99-9933-6AF9F4FE5ECA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
22+
{7D9C4AE6-192A-4A99-9933-6AF9F4FE5ECA}.Debug|Any CPU.Build.0 = Debug|Any CPU
23+
{7D9C4AE6-192A-4A99-9933-6AF9F4FE5ECA}.Debug|x64.ActiveCfg = Debug|Any CPU
24+
{7D9C4AE6-192A-4A99-9933-6AF9F4FE5ECA}.Debug|x64.Build.0 = Debug|Any CPU
25+
{7D9C4AE6-192A-4A99-9933-6AF9F4FE5ECA}.Debug|x86.ActiveCfg = Debug|Any CPU
26+
{7D9C4AE6-192A-4A99-9933-6AF9F4FE5ECA}.Debug|x86.Build.0 = Debug|Any CPU
27+
{7D9C4AE6-192A-4A99-9933-6AF9F4FE5ECA}.Release|Any CPU.ActiveCfg = Release|Any CPU
28+
{7D9C4AE6-192A-4A99-9933-6AF9F4FE5ECA}.Release|Any CPU.Build.0 = Release|Any CPU
29+
{7D9C4AE6-192A-4A99-9933-6AF9F4FE5ECA}.Release|x64.ActiveCfg = Release|Any CPU
30+
{7D9C4AE6-192A-4A99-9933-6AF9F4FE5ECA}.Release|x64.Build.0 = Release|Any CPU
31+
{7D9C4AE6-192A-4A99-9933-6AF9F4FE5ECA}.Release|x86.ActiveCfg = Release|Any CPU
32+
{7D9C4AE6-192A-4A99-9933-6AF9F4FE5ECA}.Release|x86.Build.0 = Release|Any CPU
33+
EndGlobalSection
34+
EndGlobal
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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]
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp3.1</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="Google.Analytics.Data.V1Alpha" Version="1.0.0-alpha01" />
10+
</ItemGroup>
11+
12+
</Project>

0 commit comments

Comments
 (0)