我有两个课程,我用它来获取谷歌分析。我有控制台应用程序的代码。我想在Web表单中编写代码。如何在Web表单中添加此代码?
如何以网络形式添加此完整代码?
我添加了.cs类并编写了以下代码
namespace GoogleAnalyticsAPI_Sample
{
class GoogleConnector
{
public AnalyticsService Service { get; set; }
public GoogleConnector(string keyPath, string accountEmailAddress)
{
var certificate = new X509Certificate2(keyPath, "notasecret", X509KeyStorageFlags.Exportable);
var credentials = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(accountEmailAddress)
{
Scopes = new[] { AnalyticsService.Scope.AnalyticsReadonly }
}.FromCertificate(certificate));
Service = new AnalyticsService(new BaseClientService.Initializer
{
HttpClientInitializer = credentials,
ApplicationName = "NewDemoProject"
});
}
public AnalyticDataPoint GetAnalyticsData(string profileId, string[] metrics, DateTime startDate, DateTime endDate)
{
// My Profile ID is : 98196912
AnalyticDataPoint data = new AnalyticDataPoint();
if (!profileId.Contains("ga:"))
profileId = string.Format("ga:{0}", profileId);
GaData response = null;
do
{
int startIndex = 1;
if (response != null && !string.IsNullOrEmpty(response.NextLink))
{
Uri uri = new Uri(response.NextLink);
var paramerters = uri.Query.Split('&');
string s = paramerters.First(i => i.Contains("start-index")).Split('=')[1];
startIndex = int.Parse(s);
}
var request = BuildAnalyticRequest(profileId, metrics, startDate, endDate, startIndex);
response = request.Execute();
data.ColumnHeaders = response.ColumnHeaders;
data.Rows.AddRange(response.Rows);
}
while (!string.IsNullOrEmpty(response.NextLink));
return data;
}
private DataResource.GaResource.GetRequest BuildAnalyticRequest(string profileId, string[] metrics,
DateTime startDate, DateTime endDate, int startIndex)
{
DataResource.GaResource.GetRequest request = Service.Data.Ga.Get(profileId, startDate.ToString("yyyy-MM-dd"),
endDate.ToString("yyyy-MM-dd"), string.Join(",", metrics));
request.StartIndex = startIndex;
return request;
}
public class AnalyticDataPoint
{
public AnalyticDataPoint()
{
Rows = new List<IList<string>>();
}
public IList<GaData.ColumnHeadersData> ColumnHeaders { get; set; }
public List<IList<string>> Rows { get; set; }
}
}
}
在另一个班级我打电话给那个班级
namespace GoogleAnalyticsAPI_Sample
{
class StartPoint
{
static void Main(string[] args)
{
GoogleConnector ga = new GoogleConnector(@"C:\Users\Sun\Desktop\infibeam\NewDemoProject-199399fasfaa.p12", "554818524279-g0erg68fvle5unijrup71efsdfasfd@developer.gserviceaccount.com");
var nuberOfPageViews = ga.GetAnalyticsData("ga:9819111", new string[] { "ga:pageviews" },
DateTime.Now.AddDays(-100), DateTime.Now).Rows[0][0];
Console.WriteLine("page views : {0}\n Session Duration : {1} \n Avg Session Duration : {2}\n Bounce Rate :{3}\n hits: {4}\n Sessions:{5} ", nuberOfPageViews, sessionDuration, avgSessionDuration, bounceRate, sessions, bounces, hits, sessions);
Console.Read();
}
}
}
答案 0 :(得分:1)
您可以将新的proyect作为.dll库启动,然后设置您在控制台中使用的代码并编译,您将获得.dll
然后打开web表单proyect并添加对该.dll的引用
P.D:记得在你的.dll库中将类设置为public以授予访问权限。