Home > database >  How to query data via a .netcore app from Google Analytics V4
How to query data via a .netcore app from Google Analytics V4

Time:05-09

I found a lot of examples online regarding the topic, but apparently Google have switched to a new way of presenting the data in Google Analytics.

In their V4 (for which I guess there is a nuget package, since the letter "V" and the number "4" appear in the name of the dll - Google.Apis.AnalyticsReporting.v4) version there are no Views. You go to Admin -> Account -> Property and that's it - no views.

As far as I read the ViewId is mandatory for the request when you query GA.

I can only assume that there is a new way of doing it now.

I have the setup: the api is enabled in the project I created in the Google Console, a service account is created, the authentication is done via json file, the user associated with that service account is added as an admin in Account Access Management in GA.

But how to query the reporting api from .netcore? Is there another package with newer libraries?

CodePudding user response:

Universal analytics and Google Analytics GA4 are two completely different systems and they can not be intermixed.

Universal analytics has view ids, GA4 has measurement ids.

The APIs used to access these different systems are also different so there for they also have their own nuget packages.

Google.Apis.AnalyticsReporting.v4 is used for the Google analytics reporting api, and all of the other apis which gives you access to the data behind Universal analytics accounts. It cant give you access to GA4 the data isnt the same.

How to access GA4

To access GA4 accounts you need to go though the Google analytics data api and the Google analytics admin api.

At the time of writing both of these apis are in fact still in beta and alpha.

GOOGLE_APPLICATION_CREDENTIALS  C:\tmp\ServiceAccountCred.json
var client = await BetaAnalyticsDataClient.CreateAsync(CancellationToken.None);
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"},},
};
var response = await client.RunReportAsync(request);
foreach (var row in response.Rows)
{
    Console.WriteLine($"{row.DimensionValues[0].Value}, {row.MetricValues[0].Value}, {row.MetricValues[1].Value}");
}

Code shamelessly ripped from my tutorial Guide to Google analytics Data Api Beta with C#

  • Related