将Google Analytics分析数据导入Asp.net Web应用程序

时间:2013-11-11 12:27:04

标签: c# asp.net google-analytics

我是Asp.net的新手,并且任何人都有一个工作解决方案将数据从谷歌分析中提取到我的网络应用程序中,我已经获得了以下代码,但它给了我一个IO错误。

这是我的方法:

public void Main()
        {
            //This is the API url which we're storing to a string
            string scope = AnalyticsService.Scopes.AnalyticsReadonly.GetStringValue();

            //For whatever reason, this is labelled wrong. It is the email address
            //that you have added as a user to your Analytics account

            string clientId = "xxxxxxxx@gmail.com";


            //This is the physical path to the file we downloaded earlier
            //To demonstrate this, I've kept the full path to my key file.
            //Obviously, you will need to change this to match where you've
            //stored yours.
            string keyFile = @"C:\Users\Jacob\Documents\Adwords and analytics\katoona\793c6491a4ad1118dda49d6bf6824a975edd485a-privatekey.p12";

            //The password Google gives you, probably the same as the one below
            string keyPassword = "notasecret";


            //Store the authentication description
            AuthorizationServerDescription desc = GoogleAuthenticationServer.Description;

            //Create a certificate object to use when authenticating
            X509Certificate2 key = new X509Certificate2(keyFile, keyPassword, X509KeyStorageFlags.Exportable);

            //Now, we will log in and authenticate, passing in the description
            //and key from above, then setting the accountId and scope
            AssertionFlowClient client = new AssertionFlowClient(desc, key)
            {
                ServiceAccountId = clientId,
                Scope = scope
            };

            //Finally, complete the authentication process
            //NOTE: This is the first change from the update above
            OAuth2Authenticator<AssertionFlowClient> auth =
                new OAuth2Authenticator<AssertionFlowClient>(client, AssertionFlowClient.GetState);

            //First, create a new service object
            //NOTE: this is the second change from the update
            //above. Thanks to James for pointing this out
            AnalyticsService gas = new AnalyticsService(new BaseClientService.Initializer() { Authenticator = auth });

            //Create our query
            //The Data.Ga.Get needs the parameters:
            //Analytics account id, starting with ga:
            //Start date in format YYYY-MM-DD
            //End date in format YYYY-MM-DD
            //A string specifying the metrics
            DataResource.GaResource.GetRequest r = gas.Data.Ga.Get("ga:9475327", "2013-09-09", "2013-09-23", "ga:visitors");

            //Specify some addition query parameters
            r.Dimensions = "ga:visitorType";
            r.Sort = "-ga:visitors";
            r.MaxResults = 5;

            //Execute and fetch the results of our query
            try
            {
                //Write the column headers
                GaData d = r.Execute();

                foreach (var h in d.ColumnHeaders)
                {
                    ListBox1.Items.Add(h.Name);
                }

                //Write the data
                foreach (var row in d.Rows)
                {
                    ListBox1.Items.Add(row[0] + " ------ " + row[1]);
                }
            }
            catch (Exception webEx)
            {

                throw;
            }

1 个答案:

答案 0 :(得分:0)

收到确切的错误会有所帮助 - 然后我们可以看到它是OAuth问题还是您发送的用于查询GA数据API的问题。

此外,首先使用Google Analytics Query Explorer构建并测试您的查询,然后再将其转换为C#代码。

相关问题