Exporting Google Analytics Report in Excel Using DataExport API
Google analytics is a service where all our website statistics are stored at Google server. If we want to get the reports like TopPages viewed in my website, Top cities my website viewed, Top zip codes, Average time spent on my website, etc... this solution will be very helpful. The main aim of the solution is how to get data from Google Analytics Server.
Using the Code
This code is helpful to get the report from google analytics between specific dataes and we can export the results to Excel or CSV.
The report consists of Pagename, Pageviews, Unique Pageviews, Avg. Time on Page, Bounce Rate, Exit Rate
query1.Dimensions = "ga:pagePath";
query1.Metrics = "ga:pageviews,ga:uniquePageviews,ga:timeonPage,ga:visits,ga:bounces,ga:exits";
query1.Sort = "-ga:timeonPage,ga:pagePath";
query1.Filters = "ga:pagePath!@gia;ga:pagePath!@aspx;ga:pagePath!@htm;ga:pagePath!@ismobile;ga:pagePath!@?;ga:pagePath!@mydesk;ga:timeonPage>=10;ga:country==United States";
query1.NumberToRetrieve = 10000;
query1.GAStartDate = startdate.ToString("yyyy-MM-dd");
query1.GAEndDate = enddate.ToString("yyyy-MM-dd");
How does the code actually work?
The code is nothing but a simple
API, which requests the data from Google analytics service. First when we send out user credentials to the service, it will fetch the account profile data from the service. Once we got the profile id, then we can send requests to the service for top pages, top visits, etc. The Google service will return the value as a collection of
objects. Once we get the return collection, we can manipulate the data as per our reporting manner.
Points of Interest
I have learned a new thing about the Google metrics and dimensions to fetch specified data related to me. At the start, it will be bit difficult for everybody to understand what are metrics and dimensions. This solution contains only frequent methods used in routine. But this will give a heads-up how to fetch the data from the service. For more information about the Google metrics and dimensions, please visit the link