I have a problem, I can't get the call to analytics to work with the example from this documentation ( Quick start ). I've changed some things I've read, since it doesn't allow me to use the example as it is, things like the
'using Google.Analytics.Data.V1Beta;' or the part 'BetaAnalyticsDataClient client = BetaAnalyticsDataClient.Create();' I think it no longer works. Does anyone have it working? This is my code:
using Google.Apis.AnalyticsData.v1beta;
//////
// Google Analytics 4 property ID.
string propertyId = "407521144";
// Credential Path
string credentialsPath = HostingEnvironment.MapPath("~") + @"Scripts\mktwebsuitedigital-afe56dbd25f6.json";
// Authenticate and create the service.
GoogleCredential credential = GoogleCredential.FromFile(credentialsPath);
AnalyticsDataService service = new AnalyticsDataService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "Google Analytics 4",
});
RunReportRequest request = new RunReportRequest
{
Property = "properties/" + propertyId,
Dimensions = { new Dimension { Name = "city" }, },
Metrics = { new Metric { Name = "activeUsers" }, },
DateRanges = { new DateRange { StartDate = "2023-03-31", EndDate = "today" }, },
};
RunReportResponse response = service.Properties.RunReport(request, propertyId).Execute();
// Make the request
Console.WriteLine("Report result:");
foreach (Row row in response.Rows)
{
Console.WriteLine("{0}, {1}", row.DimensionValues[0].Value, row.MetricValues[0].Value);
}
I have tried this code and other variants I have researched, like the one I posted, but I can't find a way. I know the part with the analytics id and the JSON is correct. It always fails with a
System.NullReferenceException: 'null object' error on the line 'RunReportRequest request = new RunReportRequest'.
This is my sample you should be using
BetaAnalyticsDataClientBuilder