Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 20 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,23 +129,27 @@ $ga4Service->send($baseRequest);
```

At the moment, the library contains the defined structures of the following events:

| Event name | Structure | Documentation |
| ---------- | --------- | --------------|
| add_payment_info | AddPaymentInfoEvent | [see documentation](https://developers.google.com/analytics/devguides/collection/protocol/ga4/reference/events#add_payment_info)
| add_shipping_info | AddShippingInfoEvent | [see documentation](https://developers.google.com/analytics/devguides/collection/protocol/ga4/reference/events#add_shipping_info)
| add_to_cart | AddToCartEvent | [see documentation](https://developers.google.com/analytics/devguides/collection/protocol/ga4/reference/events#add_to_cart)
| add_to_wishlist | AddToWishlistEvent | [see documentation](https://developers.google.com/analytics/devguides/collection/ga4/reference/events?hl=fr&client_type=gtag#add_to_wishlist)
| begin_checkout | BeginCheckoutEvent | [see documentation](https://developers.google.com/analytics/devguides/collection/protocol/ga4/reference/events#begin_checkout)
| login | LoginEvent | [see documentation](https://developers.google.com/analytics/devguides/collection/protocol/ga4/reference/events#login)
| purchase | PurchaseEvent | [see documentation](https://developers.google.com/analytics/devguides/collection/protocol/ga4/reference/events#purchase)
| refund | RefundEvent | [see documentation](https://developers.google.com/analytics/devguides/collection/protocol/ga4/reference/events#refund)
| remove_from_cart | RemoveFromCartEvent | [see documentation](https://developers.google.com/analytics/devguides/collection/protocol/ga4/reference/events#remove_from_cart)
| search | SearchEvent | [see documentation](https://developers.google.com/analytics/devguides/collection/protocol/ga4/reference/events#search)
| select_item | SelectItemEvent | [see documentation](https://developers.google.com/analytics/devguides/collection/protocol/ga4/reference/events#select_item)
| sign_up | SignUpEvent | [see documentation](https://developers.google.com/analytics/devguides/collection/protocol/ga4/reference/events#sign_up)
| view_cart | ViewCartEvent | [see documentation](https://developers.google.com/analytics/devguides/collection/protocol/ga4/reference/events#view_cart)
| view_item | ViewItemEvent | [see documentation](https://developers.google.com/analytics/devguides/collection/protocol/ga4/reference/events#view_item)
| view_search_results | ViewSearchResultsEvent | [see documentation](https://developers.google.com/analytics/devguides/collection/protocol/ga4/reference/events#view_search_results)
| add_payment_info | AddPaymentInfoEvent | [see documentation](https://developers.google.com/analytics/devguides/collection/ga4/reference/events#add_payment_info) |
| add_shipping_info | AddShippingInfoEvent | [see documentation](https://developers.google.com/analytics/devguides/collection/ga4/reference/events#add_shipping_info) |
| add_to_cart | AddToCartEvent | [see documentation](https://developers.google.com/analytics/devguides/collection/ga4/reference/events#add_to_cart) |
| add_to_wishlist | AddToWishlistEvent | [see documentation](https://developers.google.com/analytics/devguides/collection/ga4/reference/events#add_to_wishlist) |
| begin_checkout | BeginCheckoutEvent | [see documentation](https://developers.google.com/analytics/devguides/collection/ga4/reference/events#begin_checkout) |
| login | LoginEvent | [see documentation](https://developers.google.com/analytics/devguides/collection/ga4/reference/events#login) |
| purchase | PurchaseEvent | [see documentation](https://developers.google.com/analytics/devguides/collection/ga4/reference/events#purchase) |
| refund | RefundEvent | [see documentation](https://developers.google.com/analytics/devguides/collection/ga4/reference/events#refund) |
| remove_from_cart | RemoveFromCartEvent | [see documentation](https://developers.google.com/analytics/devguides/collection/ga4/reference/events#remove_from_cart) |
| search | SearchEvent | [see documentation](https://developers.google.com/analytics/devguides/collection/ga4/reference/events#search) |
| select_item | SelectItemEvent | [see documentation](https://developers.google.com/analytics/devguides/collection/ga4/reference/events#select_item) |
| sign_up | SignUpEvent | [see documentation](https://developers.google.com/analytics/devguides/collection/ga4/reference/events#sign_up) |
| view_cart | ViewCartEvent | [see documentation](https://developers.google.com/analytics/devguides/collection/ga4/reference/events#view_cart) |
| view_item | ViewItemEvent | [see documentation](https://developers.google.com/analytics/devguides/collection/ga4/reference/events#view_item) |
| view_item_list | ViewItemListEvent | [see documentation](https://developers.google.com/analytics/devguides/collection/ga4/reference/events#view_item_list) |
| view_search_results | ViewSearchResultsEvent | [see documentation](https://developers.google.com/analytics/devguides/collection/ga4/reference/events#view_search_results) |
| select_promotion | SelectPromotionEvent | [see documentation](https://developers.google.com/analytics/devguides/collection/ga4/reference/events#select_promotion) |
| view_promotion | ViewPromotionEvent | [see documentation](https://developers.google.com/analytics/devguides/collection/ga4/reference/events#view_promotion) |

These events are sent analogously to the examples presented above.

Expand All @@ -160,7 +164,7 @@ use Br33f\Ga4\MeasurementProtocol\Dto\Event\BaseEvent;
// Create Service and request same as above
// ...

// Create Base Event Data (for example: 'share' event - https://developers.google.com/analytics/devguides/collection/protocol/ga4/reference/events#share)
// Create Base Event Data (for example: 'share' event - https://developers.google.com/analytics/devguides/collection/ga4/reference/events?sjid=17491993992317121377-NA&client_type=gtag#share)
$eventName = 'share';
$anyEventData = new BaseEvent($eventName);
$anyEventData
Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"name": "br33f/php-ga4-mp",
"name": "ldani3l/php-ga4-mp",
"description": "PHP GoogleAnalytics4 Measurement Protocol Library",
"minimum-stability": "stable",
"license": "MIT",
"authors": [
{
"name": "Damian Zamojski",
"email": "damian.zamojski1@gmail.com"
"name": "Daniel Alvarez",
"email": "ingenierodaniel7@gmail.com"
}
],
"require": {
Expand Down
32 changes: 32 additions & 0 deletions src/Dto/Event/SelectPromotionEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
/**
* User: https://github.com/ldani3l
* Date: 09.12.2025
* Time: 15:22
*/

namespace Br33f\Ga4\MeasurementProtocol\Dto\Event;

use Br33f\Ga4\MeasurementProtocol\Dto\Parameter\AbstractParameter;

/**
* Class SelectPromotionEvent
* @package Br33f\Ga4\MeasurementProtocol\Dto\Event
* @method string getPromotionId()
* @method ItemParameter setPromotionId(string $promotionId)
* @method string getPromotionName()
* @method ItemParameter setPromotionName(string $itemListName)
*/
class SelectPromotionEvent extends ItemBaseEvent
{
private $eventName = 'select_promotion';

/**
* ViewItemEvent constructor.
* @param AbstractParameter[] $paramList
*/
public function __construct(array $paramList = [])
{
parent::__construct($this->eventName, $paramList);
}
}
32 changes: 32 additions & 0 deletions src/Dto/Event/ViewPromotionEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
/**
* User: https://github.com/ldani3l
* Date: 09.12.2025
* Time: 15:22
*/

namespace Br33f\Ga4\MeasurementProtocol\Dto\Event;

use Br33f\Ga4\MeasurementProtocol\Dto\Parameter\AbstractParameter;

/**
* Class ViewPromotionEvent
* @package Br33f\Ga4\MeasurementProtocol\Dto\Event
* @method string getPromotionId()
* @method ItemParameter setPromotionId(string $promotionId)
* @method string getPromotionName()
* @method ItemParameter setPromotionName(string $itemListName)
*/
class ViewPromotionEvent extends ItemBaseEvent
{
private $eventName = 'view_promotion';

/**
* ViewItemEvent constructor.
* @param AbstractParameter[] $paramList
*/
public function __construct(array $paramList = [])
{
parent::__construct($this->eventName, $paramList);
}
}
136 changes: 79 additions & 57 deletions src/Dto/Parameter/ItemParameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,20 @@ class ItemParameter extends AbstractParameter implements HydratableInterface
*/
protected $itemListName;

/**
* The name of the list in which the item was presented to the user
* Not required
* @var string|null
*/
protected $promotionId;

/**
* The name of the list in which the item was presented to the user
* Not required
* @var string|null
*/
protected $promotionName;

/**
* The item variant or unique code or description for additional item details/options.
* Not required
Expand Down Expand Up @@ -171,62 +185,34 @@ public function __construct(?array $blueprint = null)
*/
public function hydrate($blueprint)
{
if (array_key_exists('item_id', $blueprint)) {
$this->setItemId($blueprint['item_id']);
}
if (array_key_exists('item_name', $blueprint)) {
$this->setItemName($blueprint['item_name']);
}
if (array_key_exists('affiliation', $blueprint)) {
$this->setAffiliation($blueprint['affiliation']);
}
if (array_key_exists('coupon', $blueprint)) {
$this->setCoupon($blueprint['coupon']);
}
if (array_key_exists('currency', $blueprint)) {
$this->setCurrency($blueprint['currency']);
}
if (array_key_exists('discount', $blueprint)) {
$this->setDiscount($blueprint['discount']);
}
if (array_key_exists('index', $blueprint)) {
$this->setIndex($blueprint['index']);
}
if (array_key_exists('item_brand', $blueprint)) {
$this->setItemBrand($blueprint['item_brand']);
}
if (array_key_exists('item_category', $blueprint)) {
$this->setItemCategory($blueprint['item_category']);
}
if (array_key_exists('item_category2', $blueprint)) {
$this->setItemCategory2($blueprint['item_category2']);
}
if (array_key_exists('item_category3', $blueprint)) {
$this->setItemCategory3($blueprint['item_category3']);
}
if (array_key_exists('item_category4', $blueprint)) {
$this->setItemCategory4($blueprint['item_category4']);
}
if (array_key_exists('item_category5', $blueprint)) {
$this->setItemCategory5($blueprint['item_category5']);
}
if (array_key_exists('item_list_id', $blueprint)) {
$this->setItemListId($blueprint['item_list_id']);
}
if (array_key_exists('item_list_name', $blueprint)) {
$this->setItemListName($blueprint['item_list_name']);
}
if (array_key_exists('item_variant', $blueprint)) {
$this->setItemVariant($blueprint['item_variant']);
}
if (array_key_exists('location_id', $blueprint)) {
$this->setLocationId($blueprint['location_id']);
}
if (array_key_exists('price', $blueprint)) {
$this->setPrice($blueprint['price']);
}
if (array_key_exists('quantity', $blueprint)) {
$this->setQuantity($blueprint['quantity']);
$mapping = [
'item_id' => 'setItemId',
'item_name' => 'setItemName',
'affiliation' => 'setAffiliation',
'coupon' => 'setCoupon',
'currency' => 'setCurrency',
'discount' => 'setDiscount',
'index' => 'setIndex',
'item_brand' => 'setItemBrand',
'item_category' => 'setItemCategory',
'item_category2' => 'setItemCategory2',
'item_category3' => 'setItemCategory3',
'item_category4' => 'setItemCategory4',
'item_category5' => 'setItemCategory5',
'promotion_id' => 'setPromotionId',
'promotion_name' => 'setPromotionName',
'item_list_id' => 'setItemListId',
'item_list_name' => 'setItemListName',
'item_variant' => 'setItemVariant',
'location_id' => 'setLocationId',
'price' => 'setPrice',
'quantity' => 'setQuantity',
];

foreach ($mapping as $key => $method) {
if (array_key_exists($key, $blueprint)) {
$this->$method($blueprint[$key]);
}
}
}

Expand Down Expand Up @@ -298,6 +284,8 @@ public function export()
'item_category3' => $this->getItemCategory3(),
'item_category4' => $this->getItemCategory4(),
'item_category5' => $this->getItemCategory5(),
'promotion_id' => $this->getPromotionId(),
'promotion_name' => $this->getPromotionName(),
'item_list_id' => $this->getItemListId(),
'item_list_name' => $this->getItemListName(),
'item_variant' => $this->getItemVariant(),
Expand Down Expand Up @@ -541,6 +529,40 @@ public function getItemListName(): ?string
return $this->itemListName;
}

/**
* @param string|null $promotionName
* @return ItemParameter
*/
public function setPromotionName(?string $promotionName): ItemParameter
{
$this->promotionName = $promotionName;
return $this;
}
/**
* @return string|null
*/
public function getPromotionName(): ?string
{
return $this->promotionName;
}

/**
* @param string|null $promotionId
* @return ItemParameter
*/
public function setPromotionId(?string $promotionId): ItemParameter
{
$this->promotionId = $promotionId;
return $this;
}
/**
* @return string|null
*/
public function getPromotionId(): ?string
{
return $this->promotionId;
}

/**
* @param string|null $itemListName
* @return ItemParameter
Expand Down Expand Up @@ -623,4 +645,4 @@ public function setQuantity(?float $quantity): ItemParameter
return $this;
}

}
}