Wednesday, September 25, 2013

Google Analytics API integration with ASP.Net

// set username and password of your google analytics account account.

string userName = "";

string passWord = "";

const string dataFeedUrl = "https://www.google.com/analytics/feeds/data";

AccountQuery query = new AccountQuery();

AnalyticsService service = new AnalyticsService("AnalyticsSampleApp");

if (!string.IsNullOrEmpty(userName))

{

         service.setUserCredentials(userName, passWord);

}

string str = "";

AccountFeed accountFeed = service.Query(query);

foreach (AccountEntry entry in accountFeed.Entries)

{

     str = entry.ProfileId.Value;

}

Once you get through with GA credential process you can start doing your stuff.

The following code snippet helps you to get the total number of visits of your site; you can set the date range to retrieve the data in-between.

DataQuery query1 = new DataQuery(dataFeedUrl);

query1.Ids = str;

query1.Metrics = "ga:visits";

query1.Sort = "ga:visits";

query1.GAStartDate = DateTime.Now.AddMonths(-1).AddDays(-2).ToString("yyyy-MM-dd");

query1.GAEndDate = DateTime.Now.AddDays(-3).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;

            _intVisists = Int32.Parse(ss);

            Response.Write("<br/>");

            Response.Write("Total Visits : " + ss);

            Response.Write("<br/>");

}



Output 



BomceRe:   35.82

Total PqeView  5944

Total 90

Total New Visits 253

 Toal New Visit(°.) : 3203%

PagesVis* 7 52%

Total iois. 299


If you are searching life partner. your searching end with kpmarriage.com. now kpmarriage.com offer free matrimonial website which offer free message, free chat, free view contact information. so register here : kpmarriage.com- Free matrimonial website