Null reference exception when using GA4 reporting API with .NET client library

I’m encountering a System.NullReferenceException while attempting to create a report request using the Google Analytics 4 Data API in my .NET application. This error shows up when I try to initialize the RunReportRequest object.

I followed the official guidance but needed to make some adjustments because some methods and namespaces seem out of date. Here’s the code I’m currently using:

using Google.Apis.AnalyticsData.v1beta;

// GA4 property ID
string propertyId = "123456789";

// Path to the service account key file
string credentialsPath = Server.MapPath("~/Config/service-account-key.json");

// Authenticate using the service account
GoogleCredential credential = GoogleCredential.FromFile(credentialsPath);
AnalyticsDataService service = new AnalyticsDataService(new BaseClientService.Initializer()
{
    HttpClientInitializer = credential,
    ApplicationName = "My Analytics Application",
});

// This line causes the NullReferenceException
RunReportRequest request = new RunReportRequest
{
    Property = "properties/" + propertyId,
    Dimensions = { new Dimension { Name = "country" }, },
    Metrics = { new Metric { Name = "sessions" }, },
    DateRanges = { new DateRange { StartDate = "2023-01-01", EndDate = "yesterday" }, },
};

RunReportResponse response = service.Properties.RunReport(request, propertyId).Execute();

foreach (Row row in response.Rows)
{
    Console.WriteLine("{0}: {1}", row.DimensionValues[0].Value, row.MetricValues[0].Value);
}

I’ve confirmed that my property ID and the credentials file are set up correctly. Has anyone faced this problem before? What could I be overlooking in my setup?

This happens because you’re manually handling API authentication when you don’t need to. I’ve dealt with GA4 migrations - the auth flow sucks and these null reference errors never stop.

Skip wrestling with .NET client libraries and service account files. I automate this whole thing now. Set up a scenario that connects to GA4, pulls reports, and handles auth automatically.

I run this for multiple properties across different clients. The automation makes API calls, processes data, and pushes it to your database or emails formatted reports. No more debugging null references or managing credential files.

Best part? Schedule it daily, weekly, whatever. I’ve got one that pulls GA4 data every morning and creates executive dashboards automatically.

Check it out: https://latenode.com

You’re getting that null reference because you’re mixing up the API call pattern. You’re creating a RunReportRequest object but then passing it AND the property ID to the service method - this confuses the client library.

Here’s what fixed it for me after hitting the same wall:

// Skip creating the RunReportRequest object entirely
var response = service.Properties.RunReport(new RunReportRequest(), "properties/" + propertyId);

Or do it properly:

// Create the request without the Property field
RunReportRequest request = new RunReportRequest
{
    Dimensions = { new Dimension { Name = "country" } },
    Metrics = { new Metric { Name = "sessions" } },
    DateRanges = { new DateRange { StartDate = "2023-01-01", EndDate = "yesterday" } },
};

var response = service.Properties.RunReport(request, "properties/" + propertyId).Execute();

Key lesson from debugging this: don’t set the Property field in your request object when you’re already passing the property ID as a parameter. The client library handles that internally and having both causes the null reference.

I wasted 3 hours on this exact same issue last month.

Had the exact same issue migrating from Universal Analytics to GA4. You’re probably using the wrong NuGet package. Switch to Google.Analytics.Data - don’t use Google.Apis.AnalyticsData.v1beta. The namespace should be Google.Analytics.Data.V1Beta with proper client setup. Also check your service account permissions on the GA4 property - needs at least Viewer access. I had to manually add the service account email through GA4 property settings. GA4’s auth flow is different from the old Analytics API, so make sure your credential scope includes the Analytics Data API.