Click here to Skip to main content
12,896,108 members (49,312 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


3 bookmarked
Posted 14 Jul 2011

Fetch Data from Google Analytics Service

, 15 Jul 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
Fetch Data from Google Analytics Service

Google analytics is a service where all our website statistics stored at Google server. If we want 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

To install the solution in your current project:

  1. Download the .cs file

  2. Add into your project

    • Add the following application settings to the web.config file.

    <add key="AnalyticsUrlSite" value=""/>
    <add key="uName" value="Username"/>
    <add key="pwd" value="Password"/>
    <add key="dFeedUrl" value=""/>
    <add key="google_map_api_key" value="Get your website key from google map api"/>

    • Once you configure the settings, now we can utilize the service in our application.

How does the code actually work?

The code is nothing but a simple service, 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 returns the value as a collection of DataEntry objects.
Once we got the return collection, we can manipulate the data as per our reporting manner.

What is going on inside the code snippets?

//Instantiate the service request
GoogleAnalyticsResult objGA = new GoogleAnalyticsResult(ConfigurationManager.AppSettings["AnalyticsUrlSite"].ToString());
//Fetch the data from the date
var dtStartdate = DateTime.Now.AddMonths(-1);
//Fetch the data till
var dtEnddate = DateTime.Now;
objGA.RefreshFeed(dtStartdate, dtEnddate, strUsersitename);
//Get the unique number of visitors based on country
//Average time spent on website
//Top zip codes by country wide
objGA.GetTopZipCodesByCountryWide(strCountry, 3);
//Top citied by country wide
objGA.GetTopCitiesByCountryWide(strCountry, 3);
//Top pages viewed.

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 a bit difficult for everybody to understand what are metrics and dimensions. This solution contains only frequent methods used in the routine. But this will gives a heads-up on how to fetch the data from the service. For more information about the Google metrics and dimensions, please visit the link.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

You may also be interested in...


Comments and Discussions

QuestionGoogle Analytics error Pin
Ajay Kumar5-Sep-12 1:52
memberAjay Kumar5-Sep-12 1:52 
AnswerRe: Google Analytics error Pin
jeffb429-Oct-12 13:48
memberjeffb429-Oct-12 13:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170424.1 | Last Updated 15 Jul 2011
Article Copyright 2011 by Manoj Kumar Kota
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid