Reputation: 11
I used Google Analytics Data API (GA4) API for php.
The events to Google Analytics 4 has sent like this:
gtag('event', 'asc_cta_interaction', {
'page_type': 'widget',
'element_text': 'widget_open_close',
'event_action_result': 'close',
'link_url' : '',
'item_number': '',
'item_condition' : '',
});
So, I want to retrive all events that has element_text parameter equal with "widget_open_close". All events name are asc_cta_interaction, only parameters is changed.
How could I do this?
My php code:
$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]);
}
}
Upvotes: 1
Views: 865
Reputation: 1077
Supposing that you have set all the settings correctly including the Custom Dimensions
, you should do something like:
$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',
]),
]),
]);
In case you need to have multiple filters or you need to combine filters:
For example in the following scenario:
asc_cta_interaction
ANDelement_text
equal to widget_open_close
ANDlink_url
equal to example.com
OR item_number
equal to 123
)Then you might use something:
$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],
]),
]);
I usually code in JAVA
and there is a similar logic
please have a look at links:
GA4 Data API - How to set multiple dimensions and metrics
Upvotes: 0