php Google Analytics数据API(GA4)

velaa5lx  于 2023-09-29  发布在  PHP
关注(0)|答案(1)|浏览(115)

Google Analytics Data API(GA4)API for php
Google Analytics 4的事件发送如下:

gtag('event', 'asc_cta_interaction', {
    'page_type': 'widget',
    'element_text': 'widget_open_close',
    'event_action_result': 'close',
    'link_url' : '',
    'item_number': '',
    'item_condition' : '',
});

因此,我想检索所有element_text参数等于“widget_open_close”的事件。所有事件名称均为asc_cta_interaction,仅更改参数。
我怎么能这么做?
我的php代码:

$property_id = 'test';
    
$client = new BetaAnalyticsDataClient([
   'credentials' => 'test' 
]);

        $filterExpression = new FilterExpression([
            'filter' => new Filter([
                'field_name'    => 'eventName',
                'string_filter' => new StringFilter([
                    'value' => 'widget_open_close',
                ]),
            ]),
        ]);

        // Make an API call.
        $response = $client->runReport([
            'property' => 'properties/' . $property_id,
            'dateRanges' => [
                new DateRange([
                    'start_date' => '2020-03-31',
                    'end_date' => 'today',
                ]),
            ],
            'dimensions' => [
                new Dimension(
                    [
                        'name' => 'eventName',
                    ],
                ),
                new Dimension(
                    [
                        'name' => 'streamId',
                    ],
                ),
            ],
            'metrics' => [
                new Metric(
                    [
                    'name' => 'eventCount',
                    ],
                ),
              
            ],
            'dimensionFilter' => $filterExpression,
        ]);

     
       
        foreach ($response->getRows() as $row) {
            $eventName = $row->getDimensionValues()[0]->getValue();
            $pageType = $row->getDimensionValues()[1]->getValue();

       
            echo "<pre>"; var_dump($row->getDimensionValues()[0], $row->getMetricValues()[0]);
        }
    }
a8jjtwal

a8jjtwal1#

假设您已经正确设置了所有设置,包括Custom Dimensions,您应该执行以下操作:

$property_id = 'YOUR_PROPERTY_ID';
$client = new BetaAnalyticsDataClient([
   'credentials' => 'YOUR_CREDENTIALS' 
]);

$filterExpression = new FilterExpression([
    'filter' => new Filter([
        'field_name'    => 'element_text',
        'string_filter' => new StringFilter([
            'value' => 'widget_open_close',
        ]),
    ]),
]);

如果您需要多个过滤器或需要合并过滤器:
例如,在以下场景中:

  • 名称为asc_cta_interaction的事件AND
  • element_text等于widget_open_close
  • link_url等于example.comitem_number等于123

你可以使用一些东西:

$property_id = 'YOUR_PROPERTY_ID';
    
$client = new BetaAnalyticsDataClient([
   'credentials' => 'YOUR_CREDENTIALS' 
]);

// Event name filter
$eventNameFilter = new Filter([
    'field_name'    => 'eventName',
    'string_filter' => new StringFilter([
        'value' => 'asc_cta_interaction',
    ]),
]);

// Element text filter
$elementTextFilter = new Filter([
    'field_name'    => 'element_text',
    'string_filter' => new StringFilter([
        'value' => 'widget_open_close',
    ]),
]);

// Link URL filter for OR condition
$linkUrlFilter = new Filter([
    'field_name'    => 'link_url',
    'string_filter' => new StringFilter([
        'value' => 'example.com',
    ]),
]);

// Item number filter for OR condition
$itemNumberFilter = new Filter([
    'field_name'    => 'item_number',
    'string_filter' => new StringFilter([
        'value' => '123',
    ]),
]);

// Combine linkUrlFilter and itemNumberFilter with OR logic
$orFilterExpression = new FilterExpression([
    'or_filters' => new FilterExpressionList([
        'expressions' => [$linkUrlFilter, $itemNumberFilter],
    ]),
]);

// Combine eventNameFilter, elementTextFilter, and orFilterExpression with AND logic
$andFilterExpression = new FilterExpression([
    'and_filters' => new FilterExpressionList([
        'expressions' => [$eventNameFilter, $elementTextFilter, $orFilterExpression],
    ]),
]);

我通常在JAVA代码和有一个类似的逻辑,请看看链接:GA4 Data API - How to set multiple dimensions and metrics
FilterExpression

相关问题