The ADO.NET provider for Google Analytics supports schema discovery using ADO.NET classes or SQL statements to the system tables. This is done through the GetSchema method of the C1GoogleAnalyticsConnection class, which optionally specify the schema name and restriction values.
In the following code example, the ReadOperation method demonstrates schema discovery using the ADO.NET provider for Google Analytics. The GetSchema method is used to retrieve information about tables and columns in the database. The code retrieves the list of tables in the database by calling con.GetSchema("Tables") and then iterates over the rows to display the table names. It further retrieves column names in each table by calling con.GetSchema("Columns", new string[] { tableName }). The method also fetches foreign key information using con.GetSchema("foreignkeys", new string[] { tableName }) and populates the "References" column in the dtColumnsSchema DataTable. Additionally, the ToCSV extension method is provided to write the DataTable to a CSV file.
C# |
コードのコピー
|
---|---|
class Program { const string KeyFile = "*********"; const string ViewId = "********"; static void Main(string[] args) { ReadOperation(); } static void ReadOperation() { string gaConnection = string.Format("Key File={0};View Id={1}", KeyFile, ViewId); using (var con = new C1GoogleAnalyticsConnection(gaConnection)) { con.Open(); DataTable databaseTables = con.GetSchema("Tables"); Console.WriteLine("List of Tables in database: \n"); foreach (DataRow row in databaseTables.Rows) { //テーブル名を表示します。 string tableName = row["TableName"].ToString(); Console.WriteLine(tableName);//Table Name //テーブル内の列名を取得します。 DataTable dtColumnsSchema = con.GetSchema("Columns", new string[] { tableName }); //列 DbType を削除します。 dtColumnsSchema.Columns.Remove("DataType"); //列参照を追加します。 var fkCol = dtColumnsSchema.Columns.Add(); fkCol.ColumnName = "References"; //列参照のデータを入力します。 DataTable dtForignKeys = con.GetSchema("foreignkeys", new string[] { tableName }); foreach (DataRow fkRow in dtForignKeys.Rows) { //外部キーである列の名前。 var fkColName = fkRow["ColumnName"]; // dtColumnsSchema で対応するデータ行を検索します。 var dtRow = dtColumnsSchema.Rows.Cast<DataRow>().Where(r => r["ColumnName"].ToString().Equals(fkColName)).Select(r => r).First(); //値を埋めます。 dtRow["References"] = $"{fkRow["ForeignKeyTableName"]}.{fkRow["ForeignKeyColumnName"]}"; } //テーブルファイルが保存されるフォルダーへのパスを追加します。 dtColumnsSchema.ToCSV($@"E:\temp\GoogleAnalytics\{tableName}.csv"); } } } } static class CsvHelper { public static void ToCSV(this DataTable dtDataTable, string strFilePath) { using (StreamWriter sw = new StreamWriter(strFilePath, false)) { //ヘッダ for (int i = 0; i < dtDataTable.Columns.Count; i++) { sw.Write(dtDataTable.Columns[i]); if (i < dtDataTable.Columns.Count - 1) { sw.Write(","); } } sw.Write(sw.NewLine); foreach (DataRow dr in dtDataTable.Rows) { for (int i = 0; i < dtDataTable.Columns.Count; i++) { if (!Convert.IsDBNull(dr[i])) { string value = dr[i].ToString(); if (value.Contains(',')) { value = String.Format("\"{0}\"", value); sw.Write(value); } else { sw.Write(dr[i].ToString()); } } if (i < dtDataTable.Columns.Count - 1) { sw.Write(","); } } sw.Write(sw.NewLine); } sw.Close(); } } } |
Alternatively to the GetSchema method, the GetSchemaTable method of the DbDataReader class can be used, which returns a DataTable that defines the column metadata.
The ADO.NET provider models the data in Google Analytics into a list of tables that can be queried using standard SQL statements. Following is the list of ADO.NET Provider for Google Analytics Tables.
Table Name | Description |
---|---|
Accounts | Create, update and query Google Analytics Accounts. |
Goals | A Goals resource that describes a goal for a user's profile. |
Profiles | All the profiles to which the user has access. |
Segments | All the segments to which the user has access. |
WebProperties | All the Web Properties to which the user has access. |
The ADO.NET provider models the Google Analytics entities in views as well. These Views are provided based on the Google analytics reports. They comprise of columns and pseudo columns and do not support updates. Here is the list of ADO.NET Provider for Google Analytics Views.
View Name | Description |
---|---|
AdSense | The view that retrieves the AdSense data. |
AdWords | The view that retrieves the Adwords data. |
Ecommerce | The view that retrieves the Ecommerce data. |
Events | The view that retrieves data of the Event. |
GoalCompletions | The view that retrieves data of Goal Completion. |
SiteContent | The view that retrieves the internal site content data. |
SiteSearch | The view that retrieves the internal site search data. |
SiteSpeed | The view that retrieves internal site speed data. |
Traffic | The view that retrieves all the traffic data. |