Connect to the Google Analytics Reporting API

I am trying to connect to the Google Analytics Reporting API to get basic page statistics. I am trying to follow this tutorial ( http://www.arboundy.com/2012/04/getting-started-with-google-analytics-in-c/ ). I'm having trouble setting the correct bits to get a successful out, as it looks like Google changed the API lately, so the original config doesn't work.

Here's what I have:

        Service = new AnalyticsService("MyDemoApp");
        Service.setUserCredentials("user@gmail.com", "password");

        AccountQuery AccountsQuery = new AccountQuery("https://www.googleapis.com/analytics/v3/data/ga"/*Not sure what goes here this gives a 400*/);
        AccountFeed AccountsFeed = Service.Query(AccountsQuery); // 400 error here

      

Any ideas how to connect to this via the V3 api (which seems to be the one I got from NuGet)

+3


source to share


1 answer


this should work for u in c #. (i tried and worked)

    string username = "youremailuser@domain.com";
    string pass = "yourpassword";
    string gkey = "?key=YourAPIkEY";

    string dataFeedUrl = "https://www.google.com/analytics/feeds/data" + gkey;
    string accountFeedUrl = "https://www.googleapis.com/analytics/v2.4/management/accounts" + gkey;

    AnalyticsService service = new AnalyticsService("WebApp");
    service.setUserCredentials(username, pass);

    DataQuery query1 = new DataQuery(dataFeedUrl);


    query1.Ids = "ga:12345678";
    query1.Metrics = "ga:visits";
    query1.Sort = "ga:visits";

    query1.GAStartDate = new DateTime(2012, 1, 2).ToString("yyyy-MM-dd"); 
    query1.GAEndDate = DateTime.Now.ToString("yyyy-MM-dd");
    query1.StartIndex = 1;        

    DataFeed dataFeedVisits = service.Query(query1);

    foreach (DataEntry entry in dataFeedVisits.Entries)
    {
        string st = entry.Title.Text;
        string ss = entry.Metrics[0].Value;
        visits = ss;
    }

      



for more details Read data from Google Data API

+3


source







All Articles