scre_www
scre_www

Reputation: 2702

C# Google AnalyticsReporting v4 API - analyticsreporting function not found in Google Example Snippet

Using this code snippet from google I'm presented with this error. I can't figure out what I'm going wrong since I copy-pasted the snippet and installed the API trough nuget.

CS0103 - The name 'analyticsreporting' does not exist in the current context on line 50.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Google.Apis.AnalyticsReporting.v4;
using Google.Apis.AnalyticsReporting.v4.Data;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Services;
using Google.Apis.Util.Store;


namespace GoogleAnalytics
{
    internal class Analytics
    {
        public Analytics(Dictionary<string, dynamic> analyticsConfig)
        {
            try
            {

                // Content here
                Console.WriteLine("Hello from Google Analytics. Starting..");

                // Create the DateRange object.
                DateRange dateRange = new DateRange() { StartDate = "2016-02-02", EndDate = "2016-01-01" };

                // Create the Metrics object.
                Metric sessions = new Metric { Expression = "ga:sessions", Alias = "Sessions" };

                //Create the Dimensions object.
                Dimension browser = new Dimension { Name = "ga:browser" };

                // Create the ReportRequest object.
                ReportRequest reportRequest = new ReportRequest
                {
                    ViewId = "my_view_id_here",
                    DateRanges = new List<DateRange>() { dateRange },
                    Dimensions = new List<Dimension>() { browser },
                    Metrics = new List<Metric>() { sessions }
                };

                List<ReportRequest> requests = new List<ReportRequest>();
                requests.Add(reportRequest);

                // Create the GetReportsRequest object.
                GetReportsRequest getReport = new GetReportsRequest() { ReportRequests = requests };

                // Call the batchGet method.
                GetReportsResponse response = analyticsreporting.Reports.BatchGet(getReport).Execute();

            }

            catch (Exception e)
            {

                Console.WriteLine(e.Message);

            }
        }
    }
}

Upvotes: 0

Views: 1221

Answers (1)

scre_www
scre_www

Reputation: 2702

I found the solution tho I think it can be better documented by the Google team than it is now. The following code demonstrates one working c# example with oauth2.

 private static async Task Run()
    {
        var credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
            new ClientSecrets
            {
                ClientId = Settings.Default.ClientId,
                ClientSecret = Settings.Default.ClientSecret
            },
            new[] {AnalyticsReportingService.Scope.AnalyticsReadonly},
            "user",
            CancellationToken.None);

        var service = new AnalyticsReportingService(new BaseClientService.Initializer
        {
            ApplicationName = "GAReportDownloader",
            HttpClientInitializer = credential
        });

        // Content here
        Console.WriteLine("Hello from Google Analytics. Starting..");

        // Create the DateRange object.
        var dateRange = new DateRange {StartDate = "2017-07-01", EndDate = "2017-07-10"};

        // Create the Metrics object.
        var sessions = new Metric {Expression = "ga:sessions", Alias = "Sessions"};

        //Create the Dimensions object.
        var browser = new Dimension {Name = "ga:browser"};

        // Create the ReportRequest object.
        var reportRequest = new ReportRequest
        {
            ViewId = "YOURVIEWIDHERE",
            DateRanges = new List<DateRange> {dateRange},
            Dimensions = new List<Dimension> {browser},
            Metrics = new List<Metric> {sessions}
        };

        var requests = new List<ReportRequest> {reportRequest};

        // Create the GetReportsRequest object.
        var getReport = new GetReportsRequest {ReportRequests = requests};

        // Call the batchGet method.
        var response = service.Reports.BatchGet(getReport).Execute();

        Console.WriteLine();
    }
}

Upvotes: 1

Related Questions