aawnu / php-ga4

PHP Wrapper for Google Analytics 4 with Server Side Tracking

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Item -> item_category - Validation error [VALUE_INVALID]

mslepko opened this issue · comments

I think there might be an issue with item_category

Item.php

    public function addItemCategory(string $category)
    {
        $this->item_category[] = $category;
        return $this;
    }

I'm adding a category name like this

$item->addItemCategory( $category->name );

But then I get a validation error

jsonBody: {"non_personalized_ads":false,"client_id":"GA1.1123123123","events":[{"name":"view_item","params":{"currency":"GBP","value":2.6000000000000001,"items":[{"item_id":"443","item_name":"Complete Food Meal","currency":"GBP","item_category":["Food"],"price":2.6000000000000001,"quantity":1}]}}]}

Formatted jsonBody

{
    "non_personalized_ads": false,
    "client_id": "GA1.1123123123",
    "events": [
        {
            "name": "view_item",
            "params": {
                "currency": "GBP",
                "value": 2.6,
                "items": [
                    {
                        "item_id": "443",
                        "item_name": "Complete Food Meal",
                        "currency": "GBP",
                        "item_category": [
                            "Food"
                        ],
                        "price": 2.6,
                        "quantity": 1
                    }
                ]
            }
        }
    ]
}

Response code: 200
URL: https://www.google-analytics.com/debug/mp/collect?measurement_id=G-123&api_secret=XYZ

PHP Fatal error

Uncaught AlexWestergaard\PhpGa4\Exception\Ga4Exception: Validation Message > VALUE_INVALID [events.params.items]: Item param [item_category] has unsupported value. Value [list_value     { values { string_value: "Food" } }] is unsupported list value. in /vendor/alexwestergaard/php-ga4/src/Exception/Ga4Exception.php:77

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

      item_category: "Apparel",
      item_category2: "Adult",
      item_category3: "Shirts",
      item_category4: "Crew",
      item_category5: "Short sleeve",

I'm happy to update the code to work properly.

Thanks
Michal

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.

#77