Reputation: 2025
I'm trying to use the Google Analytics API C# to retrieve custom varaibles's from google analytics. I try this http://www.c-sharpcorner.com/UploadFile/f9f215/how-to-fetch-google-analytics-statistics-and-display-it-in-y and I can read custom dimension data, but I want to read custom variable's data, which is always return null. My code is given below :
// path of my .p12 file
string keyFilePath = System.Web.HttpContext.Current.Server.MapPath("path/of/my/.p12");
string serviceAccountEmail = "[email protected]";
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
Thanks in Advance.
Upvotes: 0
Views: 708
Reputation: 538
In request.Dimensions = "ga:dimension1";
you use your dimentoin, not your custom variable. You just change the that line to the following code
request.Dimensions = "ga:customVarName1";
// or you can use request.Dimensions = "ga:customVarValue1";
For more about custom variable read 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, hope your problem will be solved.
Upvotes: 1