Giter VIP home page Giter VIP logo

Comments (2)

jrosell avatar jrosell commented on August 16, 2024

I upgraded rto v0.3 and I still get same error.

Error calling POST https://analyticsreporting.googleapis.com/v4/reports:batchGet: (400) Invalid JSON payload received. Unknown name "comparison_value" at 'report_requests[0].metric_filter_clauses': Cannot find field.
Invalid JSON payload received. Unknown name "metric_name" at 'report_requests[0].metric_filter_clauses': Cannot find field.
Invalid value at 'report_requests[0].metric_filter_clauses.operator' (TYPE_ENUM), "GREATER_THAN"

My code:

$metricFilterSessions = new \Google_Service_AnalyticsReporting_MetricFilter();
$metricFilterSessions->setMetricName("ga:sessions");
$metricFilterSessions->setOperator("GREATER_THAN"); 
$metricFilterSessions->setComparisonValue("399");
...
$request = new \Google_Service_AnalyticsReporting_ReportRequest();
...
$request->setMetricFilterClauses($metricFilterSessions);

from google-api-php-client-services.

bshaffer avatar bshaffer commented on August 16, 2024

The issue is not with this library. If you look at the raw HTTP request you're making, it accurately reflects the expected JSON values:

{
    "reportRequests": [{
        "metricFilterClauses": {
            "comparisonValue": "399",
            "metricName": "ga:sessions",
            "operator": "GREATER_THAN"
        }
    }]
}

However, if you use the Analytics Reporting API Explorer, you'll see the structure should actually look like this:

{
    "reportRequests": [{
        "viewId": 1,
        "metricFilterClauses": [{
            "filters": [{
                "comparisonValue": "399",
                "metricName": "ga:sessions",
                "operator": "GREATER_THAN"
            }]
        }]
    }]
}

The difference here is the filter clause, the viewId, and some nested arrays. This is an issue with the library, that we are not type-checking these. I'll look into fixing that. However, the error you're seeing using the snake-cased variables are coming from the Analytics API, so the formatting difference is in the error, and not the request.

Try something like this instead:

// create the client
$client = new Google_Client();
$client->setAuthConfig($credentialsFile);
$client->setApplicationName("Client_Library_Examples");
$client->setScopes([Google_Service_AnalyticsReporting::ANALYTICS]);

// create the metric filter
$metricFilterSessions = new Google_Service_AnalyticsReporting_MetricFilter();
$metricFilterSessions->setMetricName("ga:sessions");
$metricFilterSessions->setOperator("GREATER_THAN");
$metricFilterSessions->setComparisonValue("399");
$metricFilterClause = new Google_Service_AnalyticsReporting_MetricFilterClause();
$metricFilterClause->setFilters([$metricFilterSessions]);

// create the report requests
$reportRequest = new Google_Service_AnalyticsReporting_ReportRequest();
$reportRequest->setMetricFilterClauses([$metricFilterClause]);
$reportRequest->setViewId($yourViewId); // you have to set your own View ID here!!
$getReportsRequest = new Google_Service_AnalyticsReporting_GetReportsRequest();
$getReportsRequest->setReportRequests([$reportRequest]);

// make the call
$service = new Google_Service_AnalyticsReporting($client);
$results = $service->reports->batchGet($getReportsRequest);

I hope this helps!

from google-api-php-client-services.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.