Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I retrieve Google Analytics report data using v3 of their .NET api?

I've been trying to retrieve Google analytics reports using their provided .NET api and have really been scratching my head over how I actually retrieve anything using the newest version, v3, which is available here: http://code.google.com/apis/analytics/docs/gdata/v3/gdataLibraries.html

For example, I'd like to retrieve a report query like this one: https://www.google.com/analytics/feeds/data?dimensions=ga:browser&end-date=2012-01-25&ids=ga:ACCOUNTID&metrics=ga:visits&start-date=2011-12-25

I'm able to return reports just fine using version 2 which uses GData, but was hoping to get version 3 going in case version 2 was deprecated, but having a lot of trouble seeing as meaningful documentation seems to be outdated or non-existant and I haven't been able to find any examples.

like image 794
pantryfight Avatar asked Jan 24 '12 23:01

pantryfight


People also ask

Can you recover Google Analytics data?

If all your Analytics accounts have been deleted, you'll need to create a new Analytics account in order to access the Trash Can. Click Admin. In the ACCOUNT column, click Trash Can. Locate the item you wish to restore by looking through the table, or using the search field in the table.


1 Answers

This is now possible and easy to do with the latest release of the .NET API (v1.3.0.15233). There is no example though that has been released but you can use the Task sample as a pattern to query GA data.

Here's what you need to add/change to make that sample project work for GA.

Declare an instance of the AnalyticsService

private static AnalyticsService _analyticsService;

Change the scope to Scopes.Analytics

There is a variable scope declared inside the method GetAuthorization. Change it from

string scope = TasksService.Scopes.TasksReadonly.GetStringValue();

to

string scope = AnalyticsService.Scopes.Analytics.GetStringValue();

Initialize your GA service

if (_analyticsService == null)
{
    _analyticsService = new AnalyticsService(new BaseClientService.Initializer()
    {
        Authenticator = _authenticator = CreateAuthenticator();  
    });
}

Making a Query

This is how you can query a GA profile

// make a request
var request = _analyticsService.Data.Ga.Get(
    "ga:12345678", 
    "2013-01-01",
    "2013-05-08", 
    "ga:visits,ga:bounces,ga:timeOnSite,ga:avgTimeOnSite");
// run the request and get the data                
var data = request.Fetch();

You will notice that there are four required arguments for the GetRequest similar to what is defined in the API Doc. You can visit the query explorer to know the valid metrics to use with the .NET API.

like image 152
von v. Avatar answered Sep 28 '22 11:09

von v.