How to get page views and events data from GA4 in .NET
![How to get page views and events data from GA4 in .NET](/content/images/size/w1200/2023/11/GA-with-C.png)
Google Analytics 4 has replaced Universal Analytics and I am sure you have made the switch as your Universal Analytics properties stopped processing any information starting July 1, 2023.
Since GA4 is a new and enhanced platform with new API and libraries, our existing approach of Read Google Analytics reports using C# console app would no longer work with GA4.
Let's go through step-by-step process of configuring API access, setting up credentials and utilizing the .NET client library to fetch reports from GA4.
Setup a new Google Cloud project and enable Google Analytics Reporting API
Go to https://console.cloud.google.com/
Login with your Google credentials
Create a new project. Let's call it "Get GA4 Report Data".
![](https://kumarvikram.com/content/images/2023/08/image-6.png)
Once the project is created, go to Enabled APIs & services section and choose +ENABLE APIS AND SERVICES
![](https://kumarvikram.com/content/images/2023/08/image-7.png)
Look for Google Analytics Reporting API and enable it.
![](https://kumarvikram.com/content/images/2023/11/image.png)
![](https://kumarvikram.com/content/images/2023/11/image-1.png)
Now setup credentials for API access
![](https://kumarvikram.com/content/images/2023/11/image-2.png)
Select Service Account option.
![](https://kumarvikram.com/content/images/2023/11/image-3.png)
Give it name. Here I have named it as "GA4 Service Account".
![](https://kumarvikram.com/content/images/2023/11/image-4.png)
![](https://kumarvikram.com/content/images/2023/11/image-5.png)
Create a key for Service Account
![](https://kumarvikram.com/content/images/2023/11/image-6.png)
![](https://kumarvikram.com/content/images/2023/11/image-7.png)
This will create and download the JSON key file which we will be using later in our project.
Open the downloaded JSON Key File in a text editor and copy the value of client_email key.
![](https://kumarvikram.com/content/images/2023/11/image-8.png)
Now provide user access to this email to your GA4 property.
Go to Admin section of your GA4 property and Property Access Management link.
![](https://kumarvikram.com/content/images/2023/11/image-9.png)
Viewer role should be sufficient to access the report.
![](https://kumarvikram.com/content/images/2023/11/image-10.png)
Our project and credentials setup are complete.
Create a new C# project
Create a new C# console application.
Install Google.Analytics.Data.V1Beta nuget package. The package is still in beta phase, so select Include prerelease option to get the latest package version.
![](https://kumarvikram.com/content/images/2023/11/image-11.png)
Rest of the code is available at https://github.com/kumarvikram/ga-reporting-dotnet-framework
Please let me know in the comments if you have any questions.