Closed mslepko closed 10 months ago
Hello!
I am terribly sorry for the late reply; I saw it quickly on the phone and forgot all about it due to life... 👎🏼
I will load up a codespace and have a look.
No problem at all.
I've just inherited Item class in my project and added a few additional methods, including a custom item-scoped parameter “weight”.
final class Item extends \AlexWestergaard\PhpGa4\Item {
protected null|string $weight;
protected null|string $item_category2;
protected null|string $item_category3;
protected null|string $item_category4;
protected null|string $item_category5;
public function setWeight( string $weight ) {
$this->weight = $weight;
return $this;
}
public function addItemCategories( array $categories ) {
$index = '';
if ( count( $categories ) > 0 ) {
foreach ( $categories as $category ) {
$param = 'item_category' . $index;
$this->$param = $category;
$index ? $index++ : $index = 2;
// GA supports up to 5 categories.
if ( $index >= 5 ) {
break;
}
}
}
return $this;
}
public function getParams(): array {
$params = parent::getParams();
$custom_params = array(
'item_category2',
'item_category3',
'item_category4',
'item_category5',
'weight',
);
return array_merge( $params, $custom_params );
}
}
I am going to retag it dynamically so that people do not need to worry about it in existing projects and it should have no effect on your weight addition.
I think there might be an issue with item_category
Item.php
I'm adding a category name like this
But then I get a validation error
Formatted jsonBody
Response code: 200 URL: https://www.google-analytics.com/debug/mp/collect?measurement_id=G-123&api_secret=XYZ
PHP Fatal error
According to https://developers.google.com/analytics/devguides/collection/ga4/ecommerce?client_type=gtag#view_item_details item_category should be a string and it allows to add up to 5 indexed item_categoryINDEX params
I'm happy to update the code to work properly.
Thanks Michal