Search code examples
google-analytics-api

Cannot Query Google Analytics API for Just The Home Page


This is strange, but the Google Analytics API will not let me filter a query when I need data for just the home page. If I put in a filter on page path and specify the value as "/" or just blank "", then it returns all results. Is this a known bug, or is there some other way I should query the API to get just the results I need?

    DataResource.GaResource.GetRequest rq = analytics.Data.Ga.Get(
        "ga:" + account_id,
        start_date.Year.ToString() + "-" + ((start_date.Month < 10) ? "0" : "") + start_date.Month.ToString() + "-" + ((start_date.Day < 10) ? "0" : "") + start_date.Day.ToString(),
        end_date.Year.ToString() + "-" + ((end_date.Month < 10) ? "0" : "") + end_date.Month.ToString() + "-" + ((end_date.Day < 10) ? "0" : "") + end_date.Day.ToString(),
        "ga:totalEvents");
    rq.Dimensions = "ga:pagePath,ga:eventAction,ga:eventLabel";
    rq.Filters = "ga:eventAction==Click,ga:pagePath=='" + page_path + "'";
    GaData d = await rq.ExecuteAsync();
    data = toList(d);

Solution

  • Well, this is not at all clear from Google's documentation, but on the following page https://developers.google.com/analytics/devguides/reporting/core/v3/reference it states that:

    "The OR operator is defined using a comma (,). It takes precedence over the AND operator and may NOT be used to combine dimensions and metrics in the same expression."

    You have to read between the lines in the code sample to figure it out, but the AND operator is a semi-colon, and the OR operator is a comma.