Google Analytics v4 Setup Guide for C# (not Universal Analytics)

July 1, 2023
4 min read

Google Analytics v4 (GA4) is the latest iteration of Google's analytics platform, introduced in October 2020. It represents a significant shift from its predecessor, Google Universal Analytics (UA), offering several key differences and new features.

The most obvious change is that GA4 introduces a new approach to data modeling by adopting an event-based system, departing from the conventional pageview-centric model used in Google Universal Analytics (UA). Unlike UA, GA4 expands beyond pageviews, enabling you to monitor a wider array of user interactions and events across your website or app. This shift offers increased flexibility in capturing and analyzing user behavior, allowing for a more comprehensive understanding of how users engage with your digital properties.

With that said, the old API is kicked to the curb which results in developers needing to do a fair bit of work to setup the new API.  I'm going to walk you through how to setup everything because it's super confusing if you read the Google documentation.  I had 10 tabs open and did a bunch of searches to figure this out.  I'm going to document it for the .net/c# developers out there since that's my favorite flavor.


Project and Security Setup

  1. To start off, you'll need to create a project in the google cloud console, so go and create a new project.
  2. Once you have your project, go to the APIs & Services page and Enable the "Google Analytics Data API".  DO NOT use the "Google Analytics API", that's the old one for UA and will not work for GA4.
  3. Now you will need to add a Service Account.  A service account refers to a type of Google account specifically designed for server-to-server interactions and automated access to Google services, including Google Analytics. It allows applications or services to access and interact with Google Analytics data programmatically, without requiring user intervention or authentication.  Mostly likely, you can give it permissions for "Viewer", unless you have some special requirements.  
  4. Once you have the service account created, you'll have to create a key for it.  Go to the “Keys” tab to handle that.  Once you create a key, a json key file will be created and should download automatically in your browser.  Save this in your c# project.  I put my in a "Certs" folder.
  5. Now copy the email address of the service account as we'll need that in the next step.

Setup Permissions in Google Analytics

Okay, now go to your project in google analytics.   

Go to Admin -> Account Access Management and add a new user.  Enter the email address of the service account (and uncheck the checkbox for "Notify new users by email").  Add the "Viewer" role to this user and hit the Add button to complete the setup.  

Optionally, if you wanted to just have this service account have access to a single property, you could go to the Admin -> Property Access Management page and add the user at that level.

Before we leave Google Analytics, we need to grab the property id of the property we want to do reporting on.  So go to the Admin -> Property Settings and grab the PROPERTY ID, which will be in the upper right of the page.  

Copy that and hang on to it while we get the code setup.

.NET Coding

Okay, we're at the point were we can start coding.  The first thing to do is to grab the nuget package for GA4.  At the time of this writing, it's still in beta so I'm using:

dotnet add package Google.Analytics.Data.V1Beta


Install-Package Google.Analytics.Data.V1Beta

I'm grabbing this from:  Odds are it'll change at some point in the future.

Now in your code, you'll need to add the following using statement:

using Google.Analytics.Data.V1Beta;

Then let's add some code to pull some data:

// create the Google client 
// this needs a GOOGLE_APPLICATION_CREDENTIALS environment variable or a direct pointer to the json file you created up above
var builder = new BetaAnalyticsDataClientBuilder { CredentialsPath = Path.Combine(Environment.CurrentDirectory, "Certs", "your-service-creds.json") };

BetaAnalyticsDataClient client = builder.Build();

// Initialize request argument(s) RunReportRequest request = new RunReportRequest { Property = "properties/" + propertyId, // This is the PROPERTY ID you pulled from Google Analytics Dimensions = { new Dimension , }, Metrics = { new Metric , }, DateRanges = { new DateRange { StartDate = "2023-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); }

There are a bunch of different dimensions and metrics you can slice and dice with.  You can find the lists of both in their reporting docs

Hopefully, this saves you a ton of time and headache.