Connecting to the current v4 version of Google Analytics with C# and .NET can be quite challenging, since most of the online documentation is still related to the outdated Universal Analytics (UA) interface.
Links to the correct documentation
The api is referenced to “Analytics Data API Overview” and in beta status.
The current homepage is https://developers.google.com/analytics/devguides/reporting/data/v1
The current (beta) nuget package reference is
Installing the nuget package
dotnet add package Google.Analytics.Data.V1Beta --prerelease
You need version 2.xx as date of writing – which is currently in beta / pre-release version – so not forget the –prerelease .
Prepare the environment – for a sample service account and Google Analytics v4 property
You need to follow the steps written at the quick start page:
- Create a Google Cloud Platform project and enable access to the Google Analytics Data API v1 – and create a service account.
- Add the email address of the created service account to your Analytics account – as additional user, with Read access to the property.
- For the sake of simplicity, you can use and download the service configuration json and copy it into your project (but do not add it to your git repo).
Sample code
Here is a sample .NET c# class that gets some data from the Google Analytics 4 API:
using Google.Analytics.Data.V1Beta;
using System;
namespace AnalyticsSamples
{
class QuickStart
{
static void SampleRunReport(string propertyId="YOUR-GA4-PROPERTY-ID")
{
/**
* TODO(developer): Uncomment this variable and replace with your
* Google Analytics 4 property ID before running the sample.
*/
// propertyId = "YOUR-GA4-PROPERTY-ID";
// Using a default constructor instructs the client to use the credentials
// specified in GOOGLE_APPLICATION_CREDENTIALS environment variable.
BetaAnalyticsDataClient client = BetaAnalyticsDataClient.Create();
// Initialize request argument(s)
RunReportRequest request = new RunReportRequest
{
Property = "properties/" + propertyId,
Dimensions = { new Dimension{ Name="city"}, },
Metrics = { new Metric{ Name="activeUsers"}, },
DateRanges = { new DateRange{ StartDate="2020-03-31", EndDate="today"}, },
};
// Make the request
var response = client.RunReport(request);
Console.WriteLine("Report result:");
foreach(Row row in response.Rows)
{
Console.WriteLine("{0}, {1}", row.DimensionValues[0].Value, row.MetricValues[0].Value);
}
}
static int Main(string[] args)
{
SampleRunReport();
return 0;
}
}
}
You can find the current version at https://github.com/googleanalytics/dotnet-docs-samples/blob/main/analytics-data/QuickStart/QuickStart.cs
More samples and use cases
You can find more samples and advanced use cases on the website as e.g.
- Listing Custom Definitions and Creating Reports with “Event-Scoped Custom Dimensions”, “User-Scoped Custom Dimensions”, “Event-Scoped Custom Metrics”, “Conversion Rate Metrics for one Conversion” and “Event-Scoped Custom Metric Averages”.
- Cohort Reports as Multiple cohorts and User retention fraction and Weekly cohorts and Using cohorts with other API features