Google AnalyticsAPI BatchRequest - 过滤/回拨

时间:2016-03-08 17:19:58

标签: c# asp.net google-analytics google-api google-analytics-api

我正在尝试提交BatchRequest,并且不确定如何应用过滤器或如何处理回调。

class Program
{
    static void Main(string[] args)
    {
        try
        {
            new Program().Run().Wait();
        }
        catch (Exception exc)
        {
            Console.WriteLine(exc.Message);
        }
        Console.Read();
    }

    private async Task Run()
    {
        var privatekey = "private key";
        var accountEmailAddress = "email address";

        var credentials = new ServiceAccountCredential(
            new ServiceAccountCredential.Initializer(accountEmailAddress) {
                Scopes = new[] { AnalyticsService.Scope.AnalyticsReadonly }
            }.FromPrivateKey(privatekey));

        var service = new AnalyticsService(new BaseClientService.Initializer() {
            HttpClientInitializer = credentials,
            ApplicationName = "Test"
        });

        var request = new BatchRequest(service);

        request.Queue<DataResource.GaResource.GetRequest>(service.Data.Ga.Get("ga:XXXXXX", "30daysAgo", "yesterday", "ga:sessions"),
            (content, error, i, message) =>
            {
                //callback code
            });

        await request.ExecuteAsync();
    }
}

两个问题:

  1. 如何将以下过滤器应用于请求?

    ga:pagePath==/page1.html
    
  2. 如何处理回调并查看返回的数据?

  3. 更新

    我相信我已使用以下代码解决了过滤器问题:

    var req = service.Data.Ga.Get("ga:XXXXXX", "30daysAgo", "yesterday", "ga:sessions");
    req.Filters = "ga:pagePath==/page1.html";
    
    request.Queue<DataResource.GaResource.GetRequest>(req,
        (content, error, i, message) =>
        {
            //callback code
        });
    

    我仍然不确定如何处理回调。 “content”参数作为类返回:Google.Apis.Analytics.v3.DataResource.GaResource.GetRequest

2 个答案:

答案 0 :(得分:2)

我终于能够正常工作了。

以下是任何人的未来参考资料,这是一个有效的.Net示例,用于向Analytics API提交BatchRequest。

"query":{
    "pages":[
        {
            "pageid":2511,
            "ns":0,
            "title":"Alexanderplatz",
            "extract":"Alexanderplatz (pronounced [ʔalɛkˈsandɐˌplats]) is a large public square and transport hub in the central Mitte district of Berlin, near the Fernsehturm. Berliners often call it simply Alex, referring to a larger neighbourhood stretching from Mollstraße in the northeast to Spandauer Straße and the Rotes Rathaus in the southwest.",
            "coordinates":[
                {
                    "lat":52.52166748,
                    "lon":13.41333294,
                    "primary":"",
                    "type":"landmark",
                    "dim":"1000",
                    "globe":"earth",
                    "dist":355.3
                }
            ],
            "thumbnail":{
                "source":"https://upload.wikimedia.org/wikipedia/commons/thumb/d/da/Alexanderplatz_by_the_night_-_ProtoplasmaKid.webm/400px--Alexanderplatz_by_the_night_-_ProtoplasmaKid.webm.jpg",
                "width":400,
                "height":225
            }
        },
        ...
    ]
}

答案 1 :(得分:0)

所以我不熟悉C#,但我确实查看了source code的get方法。并且GetRequest对象具有您可以设置的过滤器属性。

var request = service.Data.Ga.Get("ga:XXXXXX",
    "30daysAgo", "yesterday", "ga:sessions");
request.dimensions("ga:pagePath");
request.filters("ga:pagePath==/page1.html");

我建议您也查看Core Reporting API reference的完整参数集。

我认为回调方法中的内容对象将对应GaData response object

我希望有所帮助。