How to get data from Google Analytics 4 using C#

Robin 66 Reputation points
2023-05-17T13:41:18.15+00:00

Hi,

I am developing a program in C# to get data from Google Analytics 4. I am not an expert in C#. I have the following code. But getting the following error.

CS7036: There is no argument given that corresponds to the required formal parameter 'property' of 'Properties.RunReport(RunReportRequest, string)'

I tried everything I could but nothing seems to work. Can someone please help?

using Google.Apis.AnalyticsData.v1beta;
using Google.Apis.AnalyticsData.v1beta.Data;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Services;
using System;

namespace ConsoleApp4
{
    class Program
    {
        static void Main(string[] args)
        {

            // Google Analytics 4 property ID.
            string propertyId = "123456789";

            // Credential Path
            string credentialsPath = @"D:\\Google Analytics\GoogleAnalytics4.json";


            // Authenticate and create the service.
            GoogleCredential credential = GoogleCredential.FromFile(credentialsPath);
            AnalyticsDataService service = new AnalyticsDataService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName = "Google Analytics 4",
            });

            // Construct the request.
            RunReportRequest request = new RunReportRequest()
            {

                Property = $"properties/{propertyId}",

                DateRanges =
                {
                    new DateRange()
                    {
                        StartDate = "2023-05-15",
                        EndDate = "2023-05-15"
                    }
                },
               
                Dimensions =
                {
                    new Dimension()
                    {
                        Name = "date"
                    },
                    new Dimension()
                    {
                        Name = "landingPagePlusQueryString"
                    },
                    new Dimension()
                    {
                        Name = "medium"
                    },
                    new Dimension()
                    {
                        Name = "orderCoupon"
                    },
                    new Dimension()
                    {
                        Name = "sourceMedium"
                    },
                    new Dimension()
                    {
                        Name = "transactionId"
                    }
                },
             
                Metrics =
                {
                    new Metric()
                    {
                        Name = "sessions"
                    },
                    new Metric()
                    {
                        Name = "totalRevenue"
                    }
                }
                
            };

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

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

            Console.ReadLine();
        }
    }
}


Thanks.

Developer technologies Visual Studio Other
Developer technologies C#
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Anonymous
    2023-05-18T03:08:47.1266667+00:00

    Hi @Robin , Welcome to Microsoft Q&A.

    This error is caused because you are missing a parameter.

    service.Properties.RunReport requires two parameters.

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

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    2 people found this answer helpful.

  2. Shahnila Fahmi 0 Reputation points
    2023-10-30T09:27:10.07+00:00

    I am having this error

    Parameter validation failed for "property" : The value did not match the regular expression ^properties/[^/]+$

    This is my Code:

    Google.Apis.AnalyticsData.v1beta.Data.RunReportResponse response = service.Properties.RunReport(request, propertyId).Execute();

    Can anybody help me to Resolve this error?


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.