I'm creating a "statistics" page in asp.Net (code behind c#). I decided to use Google Apis.

I'd like to get the sum of visits and calculate the relative percentage in a google Analytics array with browsers data in... Using Google Api v3, I can obtain correctly the array: row[0] is the browser name, row[1] and row[2] visits count...

I used this code (my original one), but I stuck the mind...I think I have some problems in the "logic"...I tried with a double foreach...but, I missed something and it didn't work!

            DataResource.GaResource.GetRequest request = service.Data.Ga.Get(SiteCode, DateFrom, DateTo, Metrics);
            request.Dimensions = "ga:browser";
            request.Sort = "-ga:visits";
            var data = request.Execute();

            StringBuilder sb = new StringBuilder();

            int i = 0;
            int total = 0;
            int percent = 0;
            int number = 0;
            foreach (var row in data.Rows)
            {
                string badge = "";
                string progressBar = "";
                number = int.Parse(row[1]);
                total = int.Parse(row[2]);
                percent = number * 100 / total;

                if (percent > 70) { badge = "bg-green"; progressBar = "progress-bar-success"; }
                if (percent < 40) { badge = "bg-red"; progressBar = "progress-bar-danger"; }
                if (percent < 70 && percent > 40) { badge = "bg-yellow"; progressBar = "progress-bar-warning"; }
                i++;
                sb.Append("<tr><td>" + i.ToString() + "</td><td>" + row[0] + "</td><td><div class=\"progress progress-xs progress-striped active\">"
               + "<div class=\"progress-bar "+ progressBar +"\" style=\"width:" + percent + "%\"></div></div></td>"
               + "<td><span class=\"badge " + badge + "\">" + row[1] + " %</span></td></tr>");
            }

            litGoogleBrowsers.Text = sb.ToString();

The result is a table substituted with an asp:literal object on the client side...

Could you help me in solving this? Thank you all in advance...and please, forgive my stupid question!

Related posts

Recent Viewed