使用 google analytics api c# 读取自定义变量的数据
Read Custom variable's data using google analytics api c#
我正在尝试使用 Google 分析 API C# 从 google 分析中检索自定义变量。我试试这个 http://www.c-sharpcorner.com/UploadFile/f9f215/how-to-fetch-google-analytics-statistics-and-display-it-in-y 并且我可以读取自定义维度数据,但我想读取自定义变量的数据,它总是 return 空。我的代码如下:
// path of my .p12 file
string keyFilePath = System.Web.HttpContext.Current.Server.MapPath("path/of/my/.p12");
string serviceAccountEmail = "xxx-xxxxx@developer.gserviceaccount.com";
string websiteCode = "xxxxxxxxx";
string keyPassword = "notasecret";
AnalyticsService service = null;
var scopes =
new string[] {
AnalyticsService.Scope.Analytics, // view and manage your analytics data
AnalyticsService.Scope.AnalyticsEdit, // edit management actives
AnalyticsService.Scope.AnalyticsManageUsers, // manage users
AnalyticsService.Scope.AnalyticsReadonly}; // View analytics data
var credential = new ServiceAccountCredential(new ServiceAccountCredential.Initializer(serviceAccountEmail)
{
Scopes = scopes
}.FromCertificate(certificate));
service = new AnalyticsService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential
});
DataResource.GaResource.GetRequest request = service.Data.Ga.Get(
"ga:" + websiteCode,
DateTime.Today.AddDays(-15).ToString("yyyy-MM-dd"),
DateTime.Today.ToString("yyyy-MM-dd"),
"ga:sessions,ga:users");
request.Dimensions = "ga:dimension1";
// is it possible to add custom variable here
// return exception bcz no such dimension which named: "CustomVar1"
// request.Dimensions = "ga:CustomVar1";
var data = request.Execute();
// data.Row is always null
提前致谢。
在 request.Dimensions = "ga:dimension1";
中,您使用的是 dimentoin,而不是您的自定义变量。您只需将该行更改为以下代码
request.Dimensions = "ga:customVarName1";
// or you can use request.Dimensions = "ga:customVarValue1";
有关自定义变量的更多信息,请阅读 google custom variable reference and to retrieve custom variable read https://developers.google.com/analytics/devguides/reporting/core/dimsmets#view=detail&group=custom_variables_or_columns,希望您的问题得到解决。
我正在尝试使用 Google 分析 API C# 从 google 分析中检索自定义变量。我试试这个 http://www.c-sharpcorner.com/UploadFile/f9f215/how-to-fetch-google-analytics-statistics-and-display-it-in-y 并且我可以读取自定义维度数据,但我想读取自定义变量的数据,它总是 return 空。我的代码如下:
// path of my .p12 file
string keyFilePath = System.Web.HttpContext.Current.Server.MapPath("path/of/my/.p12");
string serviceAccountEmail = "xxx-xxxxx@developer.gserviceaccount.com";
string websiteCode = "xxxxxxxxx";
string keyPassword = "notasecret";
AnalyticsService service = null;
var scopes =
new string[] {
AnalyticsService.Scope.Analytics, // view and manage your analytics data
AnalyticsService.Scope.AnalyticsEdit, // edit management actives
AnalyticsService.Scope.AnalyticsManageUsers, // manage users
AnalyticsService.Scope.AnalyticsReadonly}; // View analytics data
var credential = new ServiceAccountCredential(new ServiceAccountCredential.Initializer(serviceAccountEmail)
{
Scopes = scopes
}.FromCertificate(certificate));
service = new AnalyticsService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential
});
DataResource.GaResource.GetRequest request = service.Data.Ga.Get(
"ga:" + websiteCode,
DateTime.Today.AddDays(-15).ToString("yyyy-MM-dd"),
DateTime.Today.ToString("yyyy-MM-dd"),
"ga:sessions,ga:users");
request.Dimensions = "ga:dimension1";
// is it possible to add custom variable here
// return exception bcz no such dimension which named: "CustomVar1"
// request.Dimensions = "ga:CustomVar1";
var data = request.Execute();
// data.Row is always null
提前致谢。
在 request.Dimensions = "ga:dimension1";
中,您使用的是 dimentoin,而不是您的自定义变量。您只需将该行更改为以下代码
request.Dimensions = "ga:customVarName1";
// or you can use request.Dimensions = "ga:customVarValue1";
有关自定义变量的更多信息,请阅读 google custom variable reference and to retrieve custom variable read https://developers.google.com/analytics/devguides/reporting/core/dimsmets#view=detail&group=custom_variables_or_columns,希望您的问题得到解决。