Guide to Google analytics Data Api Beta with C#.


Please Share

Google Analytics 4

When Google released the new version of Google Analytics GA4, a number of users flocked to the new type of Google analytics some abandoning their old universal analytics accounts. This became a big problem for developers who had applications that were designed to extract the data from a users universal analytics account using the Google Analytics Reporting API V4. Google analytics GA4 is completely different from universal analytics the two are not compatible, because of this the the Google Analytics Reporting api V4 does not work with Google Analytics ga4 properties.

How to extract GA4 data?

Seeing as the Reporting api doesn’t work with GA4 this left a lot of developers to wonder how they would be able to support GA4 analytics accounts within their existing applications. The answer to that question came in the form of the Google Analytics Data API. The Google Analytics data API does form GA4 what the Google Analytics Reporting api did for universal analytics accounts. The Google Analytics admin API does for GA4 what the Google analytics management api did for universal analytics.

So we now have two brand new APIs for use with Google Analytics GA4 accounts, and we will need to implement this into our application. The first thing to consider is that both of these apis at the time of writing are still in beta. They can change, they do change, and they have changed several times in the last six months. You should expect breaking changes without being notified.

Where to find your libraires.

Google has created several libraries for the use with these new API, in this article i am going to show you how to use the Google Analytics data api beta with .net 5 and C#.

Code

For this example I will be creating a simple .net 5 console application, I have created service account credentials up on Google developer console, and enabled the Google analytics data api under libraries. I have granted the service account access to my Google analytics account by sharing the service account email address with my account on Google Analytics.

NuGet Package

We will need to include the Google.Analytics.Data.V1Beta package into our project.

Authorization

Authorization with this library is different than it was previously. WIthin our project we first need to create a Env var called. GOOGLE_APPLICATION_CREDENTIALS which should point to the location of the service account credential key file which we downloaded from Google Developer console.

GOOGLE_APPLICATION_CREDENTIALS  C:\tmp\ServiceAccountCred.json

Now we will need to create a client which we can use to access the API from, there is no need to pass it our key file it knows to look for it in the env var that we set up

var client = await BetaAnalyticsDataClient.CreateAsync(CancellationToken.None);

Requesting Data from Google Analytics GA4

In order to request data from Google Analytics GA4 we create a RunReportRequest, and supply the property id of our Google Analytics GA4 property. Google Analytics GA4 accounts do not have View Ids like Universal analytics accounts had.

var request = new RunReportRequest
{
Property = "properties/" + PropertyId,
Dimensions = {new Dimension {Name = "date"},},
Metrics = {new Metric {Name = "totalUsers"},new Metric {Name = "newUsers"}},
DateRanges = {new DateRange {StartDate = "2021-04-01", EndDate = "today"},},
};

Google Analytics GA4 has a completely different set of dimensions and metrics its a good idea to go through them and understand how this system works. The Reports you created for your customers using universal analytics will not work with Google Analytics GA4.

Finally we run the report

var response = await client.RunReportAsync(request);

Displaying Google analytics data api report data.

You will find the output of the RunReport Request to look very similar to that of the old Google Analytics reporting api.

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

Conclusion

If you have an application that currently uses the Google Analytics reporting api v4, in order to access data from Universal analytics accounts, and you would like to also support the new Google Analytics GA4 accounts. You will need to upgrade your application to support the new Google Analytics data api which will allow you to request data from this new type of Google Analytics system.

Please Share


About Linda Lawton

My name is Linda Lawton I have more than 20 years experience working as an application developer and a database expert. I have also been working with Google APIs since 2012 and I have been contributing to the Google .Net client library since 2013. In 2013 I became a a Google Developer Experts for Google Analytics.

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.