DataConnector | ComponentOne
ADO.NET provider for Google Analytics / Creating Connection
In This Topic
    Creating Connection
    In This Topic

    To establish a connection to a Google Analytics data source, the ADO.NET Provider can be used through the C1GoogleAnalyticsConnection class. This class requires a connection string to be provided as an argument, which can be created in either of the following ways:

    The following code shows how to use the C1GoogleAnalyticsConnectionStringBuilder class to configure the connection string for Google Analytics, which can then be consumed by the C1GoogleAnalyticsConnection class to establish a connection to Google Analytics server. Through that connection, the data source can be queried.

    C#
    Copy Code
    static void ReadData()
    {
        //Define connection string using C1GoogleAnalyticsConnectionStringBuilder
        C1GoogleAnalyticsConnectionStringBuilder builder = new C1GoogleAnalyticsConnectionStringBuilder();
        builder.KeyFile = KeyFile;
        builder.ViewId = ViewId;
    
        //Define command
        string sql = "SELECT Source, Sessions FROM Traffic";
        //Fetch data
        using (var con = new C1GoogleAnalyticsConnection(builder))
        {
            con.Open();
            var command = con.CreateCommand();
            command.CommandText = sql;
            var reader = command.ExecuteReader();
            while (reader.Read())
            {
                Console.WriteLine(String.Format("{0} --> \t\t{1}", reader["Source"], reader["Sessions"]));
            }
        }
    }