Sunday, February 17, 2013

Read data from Google data API

When you send data to Google analytics, you might need access those data. so then first you need to create Google API console project. follow the instructions in here-
https://developers.google.com/analytics/resources/articles/gdata-migration-guide
in API access tab you can get the API key which is needed for getting data from Analytics. In addition to that you need following dll.
  • Google.GData.Analytics.dll
  • Google.GData.Client.dll
you can download those dll from following link-
http://code.google.com/p/google-gdata/downloads/list 

once you install this you can get the dll's from 
C:\Users\xyz\Documents\Google Data API SDK\Sources\Library\analytics\bin\Debug folder

 private void RefreshFeed()   
   {   
    string userName = "abc@gmail.com";   
    string passWord = "pwdabc";   
    string gkey = "?key=AIzaSyACrV2kbnPl9ybZZh8-sla_sPnLIKYKbxA";   
    string dataFeedUrl = "https://www.google.com/analytics/feeds/data" + gkey;   
    AnalyticsService service = new AnalyticsService("WebAppTest");   
    service.setUserCredentials(userName, passWord);   
    DataQuery query2 = new DataQuery(dataFeedUrl);   
    query2.Ids = "ga:12345678";   
    query2.Metrics = "ga:pageviews";   
    query2.Sort = "ga:pageviews";   
    query2.GAStartDate = new DateTime(2012, 1, 2).ToString("yyyy-MM-dd");   
    query2.GAEndDate = DateTime.Now.ToString("yyyy-MM-dd");   
    query2.StartIndex = 1;   
    DataFeed data = service.Query(query2);   
    foreach (DataEntry entry in data.Entries)   
    {   
       string st = entry.Title.Text;   
       string ss = entry.Metrics[0].Value;   
    }   
  }   

You can get Id as shown in following photo