Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7863613
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T23:32:21+00:00 2026-06-02T23:32:21+00:00

I have been looking for a good solution all day, but Google evolves so

  • 0

I have been looking for a good solution all day, but Google evolves so fast that I can’t find something working. What I want to do is that, I have a Web app that has an admin section where user need to be logged in to see the information. In this section I want to show some data from GA, like pageviews for some specific urls. Since it’s not the user information that I’m showing but the google analytics’user I want to connect passing information (username/password or APIKey) but I can’t find out how. All the sample I found use OAuth2 (witch, if I understand, will ask the visitor to log in using google).

What I found so far :

  • Google official Client Library for .Net : http://code.google.com/p/google-api-dotnet-client/, no sample for GA
  • official developers help : https://developers.google.com/analytics/
  • an other question with code on SO : Google Analytics API – Programmatically fetch page views in server side but I get a 403 when I try to authenticate
  • some source that access the API : http://www.reimers.dk/jacob-reimers-blog/added-google-analytics-reader-for-net downloaded the source but I can’t figure out how it works
  • this other question on SO : Google Analytics Access with C# but it does not help
  • while writing this they suggest me this 09 old post Google Analytics API and .Net.

Maybe I’m just tired and that tomorrow it will be easy to find a solution but right now I need help!

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T23:32:23+00:00Added an answer on June 2, 2026 at 11:32 pm

    I did a lot of search and finally either looking up code from multiple places and then wrapping my own interface around it i came up with the following solution. Not sure if people paste their whole code here, but i guess why not save everyone else time 🙂

    Pre-requisites, you will need to install Google.GData.Client and google.gdata.analytics package/dll.

    This is the main class that does the work.

    namespace Utilities.Google
    {
        public class Analytics
        {
            private readonly String ClientUserName;
            private readonly String ClientPassword;
            private readonly String TableID;
            private AnalyticsService analyticsService;
    
            public Analytics(string user, string password, string table)
            {
                this.ClientUserName = user;
                this.ClientPassword = password;
                this.TableID = table;
    
                // Configure GA API.
                analyticsService = new AnalyticsService("gaExportAPI_acctSample_v2.0");
                // Client Login Authorization.
                analyticsService.setUserCredentials(ClientUserName, ClientPassword);
            }
    
            /// <summary>
            /// Get the page views for a particular page path
            /// </summary>
            /// <param name="pagePath"></param>
            /// <param name="startDate"></param>
            /// <param name="endDate"></param>
            /// <param name="isPathAbsolute">make this false if the pagePath is a regular expression</param>
            /// <returns></returns>
            public int GetPageViewsForPagePath(string pagePath, DateTime startDate, DateTime endDate, bool isPathAbsolute = true)
            {
                int output = 0;
    
                // GA Data Feed query uri.
                String baseUrl = "https://www.google.com/analytics/feeds/data";
    
                DataQuery query = new DataQuery(baseUrl);
                query.Ids = TableID;
                //query.Dimensions = "ga:source,ga:medium";
                query.Metrics = "ga:pageviews";
                //query.Segment = "gaid::-11";
                var filterPrefix = isPathAbsolute ? "ga:pagepath==" : "ga:pagepath=~";
                query.Filters = filterPrefix + pagePath;
                //query.Sort = "-ga:visits";
                //query.NumberToRetrieve = 5;
                query.GAStartDate = startDate.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture);
                query.GAEndDate = endDate.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture);
                Uri url = query.Uri;
                DataFeed feed = analyticsService.Query(query);
                output = Int32.Parse(feed.Aggregates.Metrics[0].Value);
    
                return output;
            }
    
            public Dictionary<string, int> PageViewCounts(string pagePathRegEx, DateTime startDate, DateTime endDate)
            {
                // GA Data Feed query uri.
                String baseUrl = "https://www.google.com/analytics/feeds/data";
    
                DataQuery query = new DataQuery(baseUrl);
                query.Ids = TableID;
                query.Dimensions = "ga:pagePath";
                query.Metrics = "ga:pageviews";
                //query.Segment = "gaid::-11";
                var filterPrefix = "ga:pagepath=~";
                query.Filters = filterPrefix + pagePathRegEx;
                //query.Sort = "-ga:visits";
                //query.NumberToRetrieve = 5;
                query.GAStartDate = startDate.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture);
                query.GAEndDate = endDate.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture);
                Uri url = query.Uri;
                DataFeed feed = analyticsService.Query(query);
    
                var returnDictionary = new Dictionary<string, int>();
                foreach (var entry in feed.Entries)
                    returnDictionary.Add(((DataEntry)entry).Dimensions[0].Value, Int32.Parse(((DataEntry)entry).Metrics[0].Value));
    
                return returnDictionary;
            }
        }
    }
    

    And this is the interface and implementation that i use to wrap it up with.

    namespace Utilities
    {
        public interface IPageViewCounter
        {
            int GetPageViewCount(string relativeUrl, DateTime startDate, DateTime endDate, bool isPathAbsolute = true);
            Dictionary<string, int> PageViewCounts(string pagePathRegEx, DateTime startDate, DateTime endDate);
        }
    
        public class GooglePageViewCounter : IPageViewCounter
        {
            private string GoogleUserName
            {
                get
                {
                    return ConfigurationManager.AppSettings["googleUserName"];
                }
            }
    
            private string GooglePassword
            {
                get
                {
                    return ConfigurationManager.AppSettings["googlePassword"];
                }
            }
    
            private string GoogleAnalyticsTableName
            {
                get
                {
                    return ConfigurationManager.AppSettings["googleAnalyticsTableName"];
                }
            }
    
            private Analytics analytics;
    
            public GooglePageViewCounter()
            {
                analytics = new Analytics(GoogleUserName, GooglePassword, GoogleAnalyticsTableName);
            }
    
            #region IPageViewCounter Members
    
            public int GetPageViewCount(string relativeUrl, DateTime startDate, DateTime endDate, bool isPathAbsolute = true)
            {
                int output = 0;
                try
                {
                    output = analytics.GetPageViewsForPagePath(relativeUrl, startDate, endDate, isPathAbsolute);
                }
                catch (Exception ex)
                {
                    Logger.Error(ex);
                }
    
                return output;
            }
    
            public Dictionary<string, int> PageViewCounts(string pagePathRegEx, DateTime startDate, DateTime endDate)
            {
                var input = analytics.PageViewCounts(pagePathRegEx, startDate, endDate);
                var output = new Dictionary<string, int>();
    
                foreach (var item in input)
                {
                    if (item.Key.Contains('&'))
                    {
                        string[] key = item.Key.Split(new char[] { '?', '&' });
                        string newKey = key[0] + "?" + key.FirstOrDefault(k => k.StartsWith("p="));
    
                        if (output.ContainsKey(newKey))
                            output[newKey] += item.Value;
                        else
                            output[newKey] = item.Value;
                    }
                    else
                        output.Add(item.Key, item.Value);
                }
                return output;
            }
    
            #endregion
        }
    }
    

    And now the rest is the obvious stuff – you will have to add the web.config values to your application config or webconfig and call IPageViewCounter.GetPageViewCount

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been looking everywhere but can't seem to find a good solution for
I have tried looking all over the place and couldn't find a good solution
I have been looking around ocn Google and Stackoverflow but haven't found what I
I've been looking for a good vector solution, and have heard good things about
I've been looking all over the net for a good, quick solution to this,
Have been looking on some tutorials for drawing canvas using SurfaceView, but the only
I have been looking at LLVM lately, and I find it to be quite
I have been looking into different systems for creating a fast cache in a
I'm sorry, but I haven't been able to find a working solution from any
Good Morning All, I've been a PHP programmer for quite some time, but I've

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.