在视图中显示谷歌Analytics(分析)数据 [英] Display Google Analytics Data in View
问题描述
我工作的一个谷歌分析客户端,从分析API收集数据的MVC应用程序,我想在一个视图中显示的数据。最终的目标是与谷歌图表显示谷歌Analytics(分析)数据。
I'm working on a Google Analytics Client for an MVC application that collects data from Analytics API and I'm trying display the data in a view. The ultimate goal is to display the Google Analytics Data with Google Charts.
不过我不知道如何正确地结构中的数据。每个指标是一个字典中的 KeyValuePair </ code>其中指标名称为键和值是实际值。例如
GA:游客,3000
。我需要组织各指标与 KeyValuePair </ code>中,我可以返回到视图的字典。
However I'm not sure how to properly structure the Data. Each metric is a dictionary with a KeyValuePair
where metric name is key and value is the actual value. For example ga:visitors, 3000
. I need to organize each metric with its KeyValuePair
in to a dictionary that I can return to the view.
例如,首先我写了3款指标的控制台应用程序:
For example, first I wrote a console application that returned 3 metrics:
-
GA:游客
-
GA:newVisits
-
GA:percentNewVisits
ga:visitors
ga:newVisits
ga:percentNewVisits
请注意:每本指标是一个单独的字典, KeyValuePair </ code>。例如:[
GA:游客,3000
]
Note: each of this metrics is a separate dictionary with KeyValuePair
. Ex: [ga:visitors, 3000
]
在控制台中显示的数据中,code是这样的:
When displaying the data in the console, the code looks like this:
Console.WriteLine("VisitorStatistics" + " " +
d.Query.StartDate + " " + "-" + " " + d.Query.EndDate + "\r\n" +
"------------------------------------------" + "\r\n" +
"Visitors:" + " " + d.TotalsForAllResults["ga:visitors"] + "\r\n" +
"New Visitors:" + " " + d.TotalsForAllResults["ga:newVisits"] + "\r\n" +
"Percent New Visitors:" + " " + d.TotalsForAllResults["ga:percentNewVisits"] +"%");
我需要在我的MVC 4 /asp.net应用程序来显示这个数据,但我不知道如何实现这一点。所有code设在这里我的控制器:
I need to display this data in my MVC 4 /asp.net application but I'm not sure how to achieve this. All code is located here in my controller:
public void GAnalyticsService()
{
var serviceAccountEmail = "xxxxx.gserviceaccount.com";
var certificate = new x509Certificate2(@"C:\Users\User\Desktop\MyApp\Presentation\Nop.Web\key.p12", "notasecret", X509KeyStorageFlags.Exportable);
var credential = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(serviceAccountEmail) {
Scopes = new[] { AnalyticsService.Scope.Analytics }
}.FromCertificate(certificate));
// Create the service.
//NopCommerce
var GoogleAnalyticsService = new AnalyticsService(new BaseClientService.Initializer() {
HttpClientInitializer = credential,
ApplicationName = "MyApp",
});
var request = GoogleAnalyticsService.Data.Ga.Get("ProfileID", "2010-02-24", "2014-02-24", "ga:visitors,ga:newVisits,ga:percentNewVisits");
//Specify some addition query parameters
request.Dimensions = "ga:visitCount";
request.Sort = "-ga:visitors";
request.MaxResults = 10000;
//Execute and fetch the results of our query
Google.Apis.Analytics.v3.Data.GaData d = request.Execute();
}
public ActionResult GAStatistics() {
//GAnalyticsService();
return View(new GAStatisticsListModel());
}
}
下面是我在从谷歌Analytics(分析)API recieving数据:
Here's the data i'm recieving from Google Analytics API:
3个指标总成绩(每KeyValuePair字典)
Total results with 3 metrics (each a dictionary with KeyValuePair)
该KeyValuePair:
The KeyValuePair:
我只需要组织这些数据,并在视图中显示它,只是简单的数据(文本)的罚款。我该怎么做呢?
I just need to organise this data and display it in the view, just plain data (text) is fine. How do i do this?
任何建议将是很大的帮助。
Any suggestions would be of great help.
推荐答案
做了一个临时的解决办法只是为了显示数据。它提供它的目的时beeing。
Did a temporary fix just to display the data. It serves it's purpose for the time beeing.
从空白到字符串方法更改GAnalyticsService。
Changed GAnalyticsService from a void to a string method.
返回此:
return "Besöksstatistik" + " " +
d.Query.StartDate + " " + "-" + " " + d.Query.EndDate + "<br/>" +
"------------------------------------------" + "<br/>" +
"Antal besökare:" + " " + d.TotalsForAllResults["ga:visitors"] + "<br/>" +
"Antal nya besökare:" + " " + d.TotalsForAllResults["ga:newVisits"] + "<br/>" +
"Procent nya besökare:" + " " + d.TotalsForAllResults["ga:percentNewVisits"] + "%".ToString();
得到了在视图下面的输出:
Got the following output in the view:
Besöksstatistik 2010-02-24 - 2014-02-24
------------------------------------------
Antal besökare: 343272
Antal nya besökare: 147693
Procent nya besökare: 42.54700702044485%
这篇关于在视图中显示谷歌Analytics(分析)数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!