Google Analytics V3 API 不在 ASP MVC 中执行请求
Google Analytics V3 API doesn't execute request in ASP MVC
我想在 ASP MVC 项目中实施 Google Analytics API v3。因为我从来没有这样做过,所以我制作了一个示例控制台应用程序,我在其中学习了如何使用这个 API 这是代码:
static void Main(string[] args)
{
var certByteArray = Properties.Resources.key;
var serviceAccountEmail = "IdOfMyAccount@developer.gserviceaccount.com";
var certificate = new X509Certificate2(certByteArray, "pswd", X509KeyStorageFlags.Exportable);
ServiceAccountCredential credential = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(serviceAccountEmail)
{
Scopes = new[] { AnalyticsService.Scope.Analytics }
}.FromCertificate(certificate));
var service = new AnalyticsService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
});
var profileId = "ga:MyID";
var startDate = DateTime.Now.AddDays(-7).ToShortDateString();
var endDate = DateTime.Now.ToShortDateString();
var metrics = "ga:visits,ga:pageviews,ga:users,ga:sessionDuration,ga:bounceRate";
DataResource.GaResource.GetRequest request = service.Data.Ga.Get(profileId, startDate, endDate, metrics);
request.Dimensions = "ga:country";
GaData data = request.Execute();
foreach (var row in data.Rows)
{
int i = 0;
foreach (var h in data.ColumnHeaders)
{
Console.WriteLine(h.Name + " " + row[i].ToString());
// Here get the column name and its values
i++;
}
}
Console.ReadKey();
}
在这个应用程序中一切正常,我很快就得到了数据。当我尝试在 MVC 应用程序中使用此代码时,问题就出现了。在调试模式下,一切正常,直到我到达
GaData data = request.Execute();
当我按下 F10 键向前迈进时,似乎应用程序正在运行并且浏览器正在等待完成操作,但它并没有发生。 Application_Error 在这种情况下根本不会调用方法。在这两个应用程序中,我都安装了相同的 NuGet 包。
有人知道为什么它在 ASP MVC 中不起作用吗?
经过几次尝试后,它开始使用应请求调用的 ExecuteAsync 方法。这是实际的代码,但我仍然不知道如何同步。
public async Task<GaData> GetAnalysisData()
{
var certByteArray = Properties.Resources.key;
var serviceAccountEmail = "MyId@developer.gserviceaccount.com";
var certificate = new X509Certificate2(certByteArray, "notasecret", X509KeyStorageFlags.Exportable);
ServiceAccountCredential credential = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(serviceAccountEmail)
{
Scopes = new[] { AnalyticsService.Scope.Analytics }
}.FromCertificate(certificate));
var service = new AnalyticsService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
});
var profileId = "ga:********";
var startDate = DateTime.Now.AddDays(-7).ToShortDateString();
var endDate = DateTime.Now.ToShortDateString();
var metrics = "ga:visits,ga:pageviews,ga:users,ga:sessionDuration,ga:bounceRate";
DataResource.GaResource.GetRequest request = service.Data.Ga.Get(profileId, startDate, endDate, metrics);
request.Dimensions = "ga:country";
var data = await request.ExecuteAsync();
return data;
}
我想在 ASP MVC 项目中实施 Google Analytics API v3。因为我从来没有这样做过,所以我制作了一个示例控制台应用程序,我在其中学习了如何使用这个 API 这是代码:
static void Main(string[] args)
{
var certByteArray = Properties.Resources.key;
var serviceAccountEmail = "IdOfMyAccount@developer.gserviceaccount.com";
var certificate = new X509Certificate2(certByteArray, "pswd", X509KeyStorageFlags.Exportable);
ServiceAccountCredential credential = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(serviceAccountEmail)
{
Scopes = new[] { AnalyticsService.Scope.Analytics }
}.FromCertificate(certificate));
var service = new AnalyticsService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
});
var profileId = "ga:MyID";
var startDate = DateTime.Now.AddDays(-7).ToShortDateString();
var endDate = DateTime.Now.ToShortDateString();
var metrics = "ga:visits,ga:pageviews,ga:users,ga:sessionDuration,ga:bounceRate";
DataResource.GaResource.GetRequest request = service.Data.Ga.Get(profileId, startDate, endDate, metrics);
request.Dimensions = "ga:country";
GaData data = request.Execute();
foreach (var row in data.Rows)
{
int i = 0;
foreach (var h in data.ColumnHeaders)
{
Console.WriteLine(h.Name + " " + row[i].ToString());
// Here get the column name and its values
i++;
}
}
Console.ReadKey();
}
在这个应用程序中一切正常,我很快就得到了数据。当我尝试在 MVC 应用程序中使用此代码时,问题就出现了。在调试模式下,一切正常,直到我到达
GaData data = request.Execute();
当我按下 F10 键向前迈进时,似乎应用程序正在运行并且浏览器正在等待完成操作,但它并没有发生。 Application_Error 在这种情况下根本不会调用方法。在这两个应用程序中,我都安装了相同的 NuGet 包。
有人知道为什么它在 ASP MVC 中不起作用吗?
经过几次尝试后,它开始使用应请求调用的 ExecuteAsync 方法。这是实际的代码,但我仍然不知道如何同步。
public async Task<GaData> GetAnalysisData()
{
var certByteArray = Properties.Resources.key;
var serviceAccountEmail = "MyId@developer.gserviceaccount.com";
var certificate = new X509Certificate2(certByteArray, "notasecret", X509KeyStorageFlags.Exportable);
ServiceAccountCredential credential = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(serviceAccountEmail)
{
Scopes = new[] { AnalyticsService.Scope.Analytics }
}.FromCertificate(certificate));
var service = new AnalyticsService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
});
var profileId = "ga:********";
var startDate = DateTime.Now.AddDays(-7).ToShortDateString();
var endDate = DateTime.Now.ToShortDateString();
var metrics = "ga:visits,ga:pageviews,ga:users,ga:sessionDuration,ga:bounceRate";
DataResource.GaResource.GetRequest request = service.Data.Ga.Get(profileId, startDate, endDate, metrics);
request.Dimensions = "ga:country";
var data = await request.ExecuteAsync();
return data;
}