Recently I had a chance to work on Google Analytics API integration with Asp.net application.
I searched a lot through the internet but I was not satisfied with the result and finally after doing lots of R&D and taking ideas and collecting information from different sites I decided to put that kind of code under one roof. So here is the code for those who want GA data in their application.
You must have a Google account to login to let Google track your web site visits and traffics [such a kind of job has been handled by SEO team for the page ranking procedure].
Instead of taking much time I am directly shifting to coding part for the same. Just start with the initial code snippet.
// 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

This is the output for your kind consideration that I have tested for my site.
The attached ZIP is a full test solution where you can find complete source code for finding overall pageviews, bounce rate, countrywise visitors etc.. and also attached required DLL for the same.


Vaishnavi KulkarniPosted Apr 15, 2019, 9:12 PM EDT
The remote server returned an error: (404) Not Found.
Kiran VemasaniPosted Aug 29, 2017, 1:49 AM EDT
Execution of request failed: https://www.google.com/analytics/feeds/accounts/default
kamlesh singhPosted Mar 7, 2017, 7:31 AM EST
How to get data ga:adsenseRevenue in C#
Vishal GargPosted Dec 10, 2015, 1:09 AM EST
This is not worked correctly.can anyone help me for this ?
imran ansariPosted Sep 20, 2015, 2:37 AM EDT
can anyone help me out of this issue i m suffering since 5 days
imran ansariPosted Sep 20, 2015, 2:23 AM EDT
how to resolve this error "remote server returned an error: (403) Forbidden."
Kamal KantPosted Jan 13, 2015, 9:19 PM EST
this is not worked correctly
Venkat KumarPosted Dec 11, 2014, 5:41 PM EST
I'm getting this error "The remote server returned an error: (403) Forbidden."
Mauricio HenaoPosted Dec 4, 2014, 5:29 AM EST
And as I implement Google Analytics in native ASP?
MAHESH KORITALAPosted Aug 14, 2014, 4:34 AM EDT
Eventhough using the new url getting an error like this ....."Execution of request failed: https://www.googleapis.com/analytics/v2.4/data".
mohamed arafathPosted Jun 12, 2014, 12:49 AM EDT
hi i am getting the error "Execution of request failed: https://www.google.com/analytics/feeds/accounts/default" , help me out
Ranjit MondalPosted Jul 31, 2013, 9:15 PM EDT
please help me out!
Ranjit MondalPosted Jul 31, 2013, 9:15 PM EDT
Geting error like 'Execution of request failed: https://www.google.com/analytics/feeds/accounts/default'
Ajay KatrodiyaPosted Feb 21, 2013, 10:49 PM EST
Thanks, It works for me!!
chinna babuPosted Dec 26, 2012, 9:20 PM EST
I AM GETTNG THIS ERROR PLS SUGGEST ME ..ITS URGENT Execution of request failed: https://www.google.com/analytics/feeds/accounts/default
Ravi PanaraPosted Dec 11, 2012, 1:22 AM EST
Friends, The feed has been shut down by Google. please check from URL : https://groups.google.com/forum/?fromgroups=#!topic/google-analytics-api-notify/dcdz3LzKnpA. for more details check link below https://developers.google.com/analytics/resources/articles/gdata-migration-guide
Ergin celikPosted Dec 8, 2012, 10:28 AM EST
Compilation Error AccountFeed accountFeed = service.Query(query);
Friends HiPosted Dec 3, 2012, 5:45 AM EST
I AM GETTNG THIS ERROR PLS SUGGEST ME ..ITS URGENT Execution of request failed: https://www.google.com/analytics/feeds/accounts/default
jatin suryavanshiiPosted Nov 21, 2012, 2:04 AM EST
no its not working "developers.google.com/analytics/devguides/config/mgmt/v3"
jatin suryavanshiieditedPosted Nov 21, 2012, 2:04 AM ESTEdited Nov 21, 2012, 2:10 AM EST
its not working , google stop supporting 'www.google.com/analytics/feeds/accounts/default' for more details visit 'developers.google.com/analytics/devguides/config/mgmt/v3'
Ravi PanaraPosted Oct 11, 2012, 2:11 AM EDT
I would like to take an opportunity to say thanks to all. for any kind of exception you face while running the attached code please add reference of given DLL (from Bin folder) properly in your project. let me know if I can help further.
anil babuPosted Aug 31, 2012, 12:51 AM EDT
Its not working, ERROR is:Execution of request failed: https://www.google.com/analytics/feeds/accounts/default I am using my Google Analytics Cridentials Also Its not working
sheraz khaneditedPosted Aug 16, 2012, 4:17 AM EDTEdited Aug 16, 2012, 8:07 AM EDT
Its getting authenticated, not getting any error but not getting any stats either, Please help me out.
Magni HansenPosted Aug 2, 2012, 3:44 AM EDT
Im keep getting this exception: The remote server returned an error: (403) Forbidden. My username and password works fine. It throws an exception on this line: AccountFeed accountFeed = service.Query(query); Hope you can help me. [email protected]
LeonardoPosted Jul 10, 2012, 7:56 AM EDT
Thanks! Worked here. Couldn't be any easier. To anyone trying to run it, remember to reference the included dlls on your project.
bhargav patelPosted Feb 13, 2012, 6:48 PM EST
How to trace visitors ip address in my site? i am working with asp.net project with c# please help me!!
Manvendra AgrahariPosted Jan 11, 2012, 9:24 PM EST
Please help me i am getting the following error: Exception Details: System.Net.WebException: The remote server returned an error: (403) Forbidden. Email id: [email protected]
surender kumarPosted Jan 10, 2012, 10:31 PM EST
Hi Ravi, I am gerring error on below line. AccountFeed accountFeed = service.Query(query); Error is : GDataRequestException was unhandled by user code. Execution of request failed: https://www.google.com/analytics/feeds/accounts/default Please Provide solution ASAP. Surender Kumar [email protected]
Nalini APosted Aug 26, 2011, 3:59 AM EDT
Hi ravi, im new to this concept. Im developin website with google analytics is it possible to connect my db with it ? Pls im working on it for past 5 days.
Prashant KhandelwalPosted Feb 9, 2011, 2:24 AM EST
worked like a charm!!!!! I have been looking for this for long. Thanks.
Suraj DhandalPosted Feb 7, 2011, 11:18 AM EST
Nice article Ravi!! But I want page names with their visit count. Can please reply soon on this?
Dorababu MekaPosted Dec 15, 2010, 2:34 AM EST
Hi i am getting this at this point can you tell why AccountFeed accountFeed = service.Query(query);
Satish MadasueditedPosted Dec 9, 2010, 2:46 PM ESTEdited Dec 9, 2010, 2:47 PM EST
Ravi thanks for the post. Really helpful... I'm using the same code and getting this issue "GDataRequestException was unhandled by user code" I am pretty much new to this Google API and would really be helpful if u can let me know where I'm commiting a mistake. Exception is thrown in this line of code "AccountFeed accountFeed = service.Query(query);"
Chad MusgrovePosted Nov 24, 2010, 7:45 AM EST
Hi, Just want to thank you for this. I am a rookie (learning) developer and this will definately help me with my project. I downloaded the file - placed the reference (.dll) in the right place - and this worked. Now I just need to figure out how to create a form, request username/password, choose from a list of analytics accounts, then run the query for all of them, place the data into a db and create reports I want...... Piece of cake! lmao... Should have this done by my 40th Birthday - :) Thanks again for pointing me in the right direction. Chad
Sukhdeep AroraPosted Oct 27, 2010, 6:30 PM EDT
Thanks Ravi for this post...you had made my day :)
Mohsen EslamifarPosted Oct 24, 2010, 5:56 AM EDT
i convert it to vb.net and im use it for my specifie website i found my specify website from my many profiles (entry.ProfileId.Value) with this: entry.Title.Text thank you....
Mahesh ChandTOP 10Posted Oct 7, 2010, 4:22 AM EDT
Thanks for sharing Ravi. This is great to see this kind of work.
Ravi PanaraPosted Oct 1, 2010, 8:00 PM EDT
@laksh, @ashish, @akbar, @crr please check your mail i have already sent you some suggestions for your query. and let me know if problem remains same. @harvinder in fact one more person has requested for the same to display data graphically but frankly speaking i dint tried to work on it. will update this article once i get some output regarding this, and for the pages display which are visited by users then its pretty simple use dimension and metrices from http://code.google.com/apis/analytics/docs/gdata/gdataReferenceDimensionsMetrics.html#queryValidation let me know if you still face the same problem. Thanks Ravi
sanjay srivastavaPosted Sep 30, 2010, 1:31 AM EDT
Hi ravi, Good Article, Thanks a lot....... Regards Sanjay
lakshPosted Sep 23, 2010, 1:11 AM EDT
send me correct working code to my mail id [email protected]
Ashish MavadiyaeditedPosted Sep 21, 2010, 1:44 AM EDTEdited Sep 21, 2010, 4:31 AM EDT
@ shashank I have multiple sites in my google analytics account. I want to know total view count on particular page. I have coded AnalyticsService service = new AnalyticsService("ejewelv35.diatree.com"); where ejewelv35.diatree.com is one of my website. But i get following error. "The remote server returned an error: (403) Forbidden." Please help me out. can you help me with solution you got ? My id is [email protected] thanks
Akbar NawazPosted Sep 20, 2010, 9:46 AM EDT
getting exception in code gdata.requestexception was unhandeled by user code! plz help
Harvinder SyanPosted Aug 12, 2010, 12:29 AM EDT
i want to show the data grahically and also want to display name of pages visited by user can you help me [email protected],,,Harvinder Syan
crr giiPosted Jul 17, 2010, 6:21 AM EDT
hi i dnloaded ur code and i m gettin this Google.GData.Client.GDataRequestException unhandled...pls help!
pooja dixitPosted Jul 14, 2010, 4:06 AM EDT
Hi, The problem was with Datetime.Once i changed i could see the code working.Can i retrieve data according to day,week,month.If yes please help me. Thanks
pooja dixiteditedPosted Jul 12, 2010, 11:03 PM EDTEdited Jul 12, 2010, 11:05 PM EDT
Hi, I downloaded the application and running it in VS2008 envt.But im not able to see the statistics in asp.net page when i run the application where as in google analytics page im able to view it. Please reply ASAP.
MarcinPosted Apr 20, 2010, 8:16 AM EDT
Keep it up...
jaimin bhagatPosted Mar 31, 2010, 9:03 PM EDT
Nice share...i was looking for this.
nekro nekroPosted Mar 28, 2010, 9:43 AM EDT
It`s not working, please send me the solution [email protected] Thanks!
aung hlaingPosted Mar 27, 2010, 12:27 AM EDT
hey hi i like your article and i download your code. i try to use your code but is have error in your dll i think did u fixed already ? if u fiexd pls send to my mail [email protected] i need to used man thx a lot pls help me
Murray ChangPosted Mar 23, 2010, 11:28 PM EDT
It's not working Please help me!!!! something wrong with Google.GData.Client [email protected]
shashank kalePosted Mar 6, 2010, 2:19 AM EST
It's not working ..... plz help [email protected]
viral UpadhyayPosted Mar 2, 2010, 7:31 PM EST
Good Article Ravi keep it up.