Skip to content

Commit

Permalink
Fixing Time Metric Issue #1
Browse files Browse the repository at this point in the history
  • Loading branch information
rmostafa committed Mar 12, 2014
1 parent 67e728f commit 68d79dd
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 17 deletions.
34 changes: 20 additions & 14 deletions SimpleAnalytics/Analytics/AnalyticsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ public class AnalyticsManager
public Google.Apis.Analytics.v3.AnalyticsService analyticsService;
bool IsInitialized = false;

public List<Google.Apis.Analytics.v3.Data.Profile> Profiles {get; set;}
public Google.Apis.Analytics.v3.Data.Profile DefaultProfile { set; get; }
public List<Google.Apis.Analytics.v3.Data.Profile> Profiles {get; set;}
public Google.Apis.Analytics.v3.Data.Profile DefaultProfile { set; get; }
public bool InitFailed { get; set; }
public Exception FailureException { get; set; }

Expand Down Expand Up @@ -100,6 +100,10 @@ public void LoadAnalyticsProfiles()
}
}

public bool HasProfile()
{
return DefaultProfile != null;
}
public void SetDefaultAnalyticProfile(string profileId)
{
var profile = (from p in Profiles where p.Id == profileId select p).FirstOrDefault();
Expand Down Expand Up @@ -166,27 +170,31 @@ public Google.Apis.Analytics.v3.Data.GaData GetGaData(string profileid, DateTime

public System.Data.DataTable GetGaDataTable(DateTime startDate, DateTime endDate, List<Data.DataItem> metricsList)
{
return GetGaDataTable(startDate, endDate, metricsList, null, null, null, null, null, null, null, null, null);
return GetGaDataTable(startDate, endDate, metricsList, null, null, null, null, null, null, null,false, null, null);
}
public System.Data.DataTable GetGaDataTable(DateTime startDate, DateTime endDate, List<Data.DataItem> metricsList, List<Data.DataItem> sortList)
public System.Data.DataTable GetGaDataTable(DateTime startDate, DateTime endDate, List<Data.DataItem> metricsList, List<Data.DataItem> sortList,bool ascending)
{
return GetGaDataTable(startDate, endDate, metricsList, null, null, null, null, null, null, sortList, null, null);
return GetGaDataTable(startDate, endDate, metricsList, null, null, null, null, null, null, sortList,ascending, null, null);
}
public System.Data.DataTable GetGaDataTable(DateTime startDate, DateTime endDate, List<Data.DataItem> metricsList, List<Data.DataItem> dimensionsList, List<Data.DataItem> filtersList, List<Data.DataItem> sortList)
public System.Data.DataTable GetGaDataTable(DateTime startDate, DateTime endDate, List<Data.DataItem> metricsList, List<Data.DataItem> dimensionsList, List<Data.DataItem> filtersList, List<Data.DataItem> sortList,bool ascending)
{
return GetGaDataTable(startDate, endDate, metricsList, dimensionsList, filtersList, null, null, null, null, sortList, null, null);
return GetGaDataTable(startDate, endDate, metricsList, dimensionsList, filtersList, null, null, null, null, sortList,ascending, null, null);
}
public System.Data.DataTable GetGaDataTable(DateTime startDate, DateTime endDate, List<Data.DataItem> metricsList, List<Data.DataItem> dimensionsList, List<Data.DataItem> filtersList, Google.Apis.Analytics.v3.DataResource.GaResource.GetRequest.SamplingLevelEnum? samplingLevel, List<Data.DataItem> sortList, List<Data.DataItem> fields)
public System.Data.DataTable GetGaDataTable(DateTime startDate, DateTime endDate, List<Data.DataItem> metricsList, List<Data.DataItem> dimensionsList, List<Data.DataItem> filtersList, Google.Apis.Analytics.v3.DataResource.GaResource.GetRequest.SamplingLevelEnum? samplingLevel, List<Data.DataItem> sortList,bool ascending, List<Data.DataItem> fields)
{
return GetGaDataTable(startDate, endDate, metricsList, dimensionsList, filtersList, null, null, samplingLevel, null, sortList, null, fields);
return GetGaDataTable(startDate, endDate, metricsList, dimensionsList, filtersList, null, null, samplingLevel, null, sortList,ascending, null, fields);
}

public System.Data.DataTable GetGaDataTable(DateTime startDate, DateTime endDate, List<Data.DataItem> metricsList, List<Data.DataItem> dimensionsList, List<Data.DataItem> filtersList, int? maxResults, Google.Apis.Analytics.v3.DataResource.GaResource.GetRequest.OutputEnum? output, Google.Apis.Analytics.v3.DataResource.GaResource.GetRequest.SamplingLevelEnum? samplingLevel, List<Data.DataItem> segmentList, List<Data.DataItem> sortList, int? startIndex, List<Data.DataItem> fieldsList)
public System.Data.DataTable GetGaDataTable(DateTime startDate, DateTime endDate, List<Data.DataItem> metricsList, List<Data.DataItem> dimensionsList, List<Data.DataItem> filtersList, int? maxResults, Google.Apis.Analytics.v3.DataResource.GaResource.GetRequest.OutputEnum? output, Google.Apis.Analytics.v3.DataResource.GaResource.GetRequest.SamplingLevelEnum? samplingLevel, List<Data.DataItem> segmentList, List<Data.DataItem> sortList,bool ascending, int? startIndex, List<Data.DataItem> fieldsList)
{
if (DefaultProfile == null)
throw new Exception("Please set a default profile first using SetDefaultAnalyticProfile method");

Google.Apis.Analytics.v3.Data.GaData gaData = GetGaData("ga:" + DefaultProfile.Id, startDate, endDate, Data.DataItem.GetString(metricsList), Data.DataItem.GetString(dimensionsList), Data.DataItem.GetString(filtersList), maxResults, output, samplingLevel, Data.DataItem.GetString(segmentList), Data.DataItem.GetString(sortList), startIndex, Data.DataItem.GetString(fieldsList));
string sort = "";
if(ascending)
sort = Data.DataItem.GetString(sortList);
else
sort = Data.DataItem.GetString(sortList, "-");
Google.Apis.Analytics.v3.Data.GaData gaData = GetGaData("ga:" + DefaultProfile.Id, startDate, endDate, Data.DataItem.GetString(metricsList), Data.DataItem.GetString(dimensionsList), Data.DataItem.GetString(filtersList), maxResults, output, samplingLevel, Data.DataItem.GetString(segmentList), sort , startIndex, Data.DataItem.GetString(fieldsList));
System.Data.DataTable table = BuildTableColumns(metricsList, dimensionsList);
if(gaData != null)
table = BuildTableRows(gaData, table);
Expand Down Expand Up @@ -219,7 +227,6 @@ private System.Data.DataTable BuildTableColumns(List<Data.DataItem> metricsList,

private System.Data.DataTable BuildTableRows(Google.Apis.Analytics.v3.Data.GaData gaData, System.Data.DataTable table)
{

foreach (var ls in gaData.Rows)
{
System.Data.DataRow row = table.NewRow();
Expand All @@ -233,6 +240,5 @@ private System.Data.DataTable BuildTableRows(Google.Apis.Analytics.v3.Data.GaDat
}
#endregion


}
}
19 changes: 18 additions & 1 deletion SimpleAnalytics/Analytics/Data/DataItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,17 @@ public Type Type
{
case "STRING":
return typeof(string);
case "TIME":
case "DATE":
case "DATETIME":
return typeof(DateTime);
case "INTEGER":
return typeof(int);
case "TIME":
case "PERCENT":
case "FLOAT":
case "CURRENCY":
return typeof(decimal);

}
return typeof(string);
}
Expand Down Expand Up @@ -147,5 +150,19 @@ public static string GetString(List<Data.DataItem> list)
return result;

}

public static string GetString(List<Data.DataItem> list,string specialChar)
{
string result = "";
if (list == null) return result;
foreach (DataItem item in list)
{
result += specialChar + item.APICommand + ",";
}
if (!string.IsNullOrEmpty(result))
result = result.Remove(result.Length - 1, 1);
return result;

}
}
}
4 changes: 2 additions & 2 deletions SimpleAnalytics/AnalyticsTest/Default.aspx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ protected void Page_Load(object sender, EventArgs e)
manager.SetDefaultAnalyticProfile("80425770");
List<Analytics.Data.DataItem> metrics = new List<Analytics.Data.DataItem>();
metrics.Add(Analytics.Data.Session.Metrics.visits);


metrics.Add(Analytics.Data.Session.Metrics.timeOnSite);
metrics.Add(Analytics.Data.Adsense.Metrics.adsenseRevenue);
List<Analytics.Data.DataItem> dimensions = new List<Analytics.Data.DataItem>();
dimensions.Add(Analytics.Data.GeoNetwork.Dimensions.country);

Expand Down

0 comments on commit 68d79dd

Please sign in to comment.