Codehelp
Codehelp

Reputation: 4747

Implementing Google Analytics in ASP.NET MVC 4

I am trying to implement GA in a MVC 4 application. I got the .NET API's from Google and created an instance of AnalyticsService like so:

AnalyticsService service = new AnalyticsService("WebSiteAnalytics");

I have also configured the Google account credentials in the config file and they are read from there. Now my question is what's a ProfileId and what's a Table Id?

This link uses Table ID. Where do I get it from. This link uses Profile ID. Where do I get it from. I am totally confused about these two!!!

The only id GA gave after registering at GA is a tracking id in this format UA-XXXXXXXX-X

Can some one point to a resource where I can get started with this?

Regards.

Upvotes: 1

Views: 3977

Answers (1)

Andrey Gubal
Andrey Gubal

Reputation: 3479

Profile ID you can find in your account in Google Analytics:

sign in -> Admin -> Profile Settings Here is a video on youtube. It uses old design, but path is the same

If it helps i'm using following Action to get statistics (Page views) of news page by newsID:

public ActionResult Stats(int id)
        {
            string userName = "myusername";
            string passWord = "mypassword";
            string profileId = "ga:12345678";
            string pagepath = "~/Home/Details/" + id;

            AnalyticsService asv = new AnalyticsService("GData-Version: 1.8");
            asv.setUserCredentials(userName, passWord);

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

            var service = new AnalyticsService("WebSiteAnalytics");

            service.setUserCredentials(userName, passWord);

            DataQuery query = new DataQuery(dataFeedUrl);
            query.Ids = profileId;
            query.Metrics = "ga:pageviews";
            query.GAStartDate = "2011-05-01";
            query.GAEndDate = DateTime.Now.ToString("yyyy-MM-dd");
            query.Filters = "ga:pagePath=" + pagepath;
            Uri url = query.Uri;



            // Send our request to the Analytics API and wait for the results to
            // come back.

            var feed = asv.Query(query);
            var totalEntrys = feed.Entries[0];
            ViewData["Total"] = ((DataEntry)(totalEntrys)).Metrics[0].Value;
            return PartialView(feed.Entries);

        }

Upvotes: 2

Related Questions