Learn business growth with Google Analytics 4 Forums Google Analytics 4 Implementing Metric Filtering in GA4 API with PHP Library

  • Implementing Metric Filtering in GA4 API with PHP Library

    Posted by George on 29 January 2023 at 12:45 pm

    Hey folks, I’m messing around with the GA4 api php library and trying to learn how to filter dimensions. But I’m stuck as it’s currently in beta and the php examples I need are nowhere to be found.

    Right now when I run my code, I get “Expect GoogleAnalyticsDataV1betaNumericValue.” popping up.

    If anyone can point me in the right direction, that’d be amazing.

    
    $response = $client->runReport([
                'property' => 'properties/' . $property_id,
                'dateRanges' => [
                    new DateRange([
                        'start_date' => 'yesterday',
                        'end_date' => 'yesterday',
                    ]),
                ],
                'dimensions' => [
                    new Dimension([
                        'name' => 'eventName',
                    ]),
                ],
                'metrics' => [
                    new Metric([
                        'name' => 'eventCount',
                    ])
                ],
                
                'metricFilter' => new FilterExpression([
                    'filter' => new Filter([
                        'field_name' => 'eventCount',
                        'numeric_filter' => new FilterNumericFilter([
                            'operation' => FilterNumericFilterOperation::GREATER_THAN,
                            'value' => '10000',
                        ])
                    ]),
                ]),
                
            ]);
    

    Oh, and here’s the JSON version of the request from the api explorer, in case it helps:

    
    {
      "dimensions": [
        {
          "name": "eventName"
        }
      ],
      "metrics": [
        {
          "name": "eventCount"
        }
      ],
      "dateRanges": [
        {
          "startDate": "yesterday",
          "endDate": "yesterday"
        }
      ],
      "metricFilter": {
        "filter": {
          "fieldName": "eventCount",
          "numericFilter": {
            "operation": "GREATER_THAN",
            "value": {
              "int64Value": "10000"
            }
          }
        }
      }
    }
    
    Daniel replied 12 months ago 3 Members · 2 Replies
  • 2 Replies
  • Elijah

    Member
    21 February 2023 at 5:26 pm

    Hey, I got it sorted out and thought I’d share the fix as it could help others dealing with the GA4 API. Here’s how I altered the ‘metricFilter’ section:

    'metricFilter' => new FilterExpression([
                    'filter' => new Filter([
                        'field_name' => 'eventCount',
                        'numeric_filter' => new FilterNumericFilter([
                            'operation' => FilterNumericFilterOperation::GREATER_THAN,
                            'value' => new NumericValue([
                                'int64_value'  =>  '10000'
                            ]),
                        ])
                    ]),
                ]),
    

    Turns out, you need to use a ‘NumericValue’ object for the ‘value’ field. Once I did that, it worked perfectly. Hope this helps!

  • Daniel

    Member
    25 March 2023 at 6:05 am

    It appears the code is expecting a GoogleAnalyticsDataV1betaNumericValue instance in the ‘value’ field of numeric_filter, not a string or integer as currently passed (‘10000’).

    Take a look at the JSON equivalent provided, the value is an object:
    `
    “value”: {
    “int64Value”: “10000”
    }
    `
    Translating this to PHP, it likely expects an equivalent object:
    `
    ‘numeric_filter’ => new FilterNumericFilter([
    ‘operation’ => FilterNumericFilterOperation::GREATER_THAN,
    ‘value’ => new GoogleAnalyticsDataV1betaNumericValue([
    ‘int64_value’ => ‘10000’,
    ])
    ])
    `
    Please make sure to replace ‘GoogleAnalyticsDataV1betaNumericValue’ with the actual class path in your case.

    Also, make sure that you’ve added necessary class initializations for the classes used like DateRange, Dimension, Metric, FilterExpression, FILTER, NumericValue etc., at the top of your PHP file per your namespace structure.

    If the issue still persists, you could refer to Google’s official PHP library repository or contact their support for further assistance as the GA4 library is in beta and may contain bugs or lack of documentations.

Log in to reply.