From b3acd402c7faaaa3af9fc69901ed39b9b29d2012 Mon Sep 17 00:00:00 2001 From: Micah Wittman Date: Thu, 19 Feb 2026 19:22:32 -0400 Subject: [PATCH 1/8] Remove Google Analytics (legacy and GA4) --- Store/StoreAnalyticsOrderTracker.php | 20 -------------------- 1 file changed, 20 deletions(-) diff --git a/Store/StoreAnalyticsOrderTracker.php b/Store/StoreAnalyticsOrderTracker.php index 45e736db..2a838849 100644 --- a/Store/StoreAnalyticsOrderTracker.php +++ b/Store/StoreAnalyticsOrderTracker.php @@ -28,26 +28,6 @@ public function __construct(StoreOrder $order, $affiliation = null) $this->affiliation = $affiliation; } - public function getGoogleAnalyticsCommands() - { - $commands = [$this->getGoogleAnalyticsOrderCommand()]; - foreach ($this->order->items as $item) { - $commands[] = $this->getGoogleAnalyticsOrderItemCommand($item); - } - - $commands[] = '_trackTrans'; - - return $commands; - } - - public function getGoogleAnalytics4Commands(): array - { - return [ - $this->getGoogleAnalytics4PurchaseCommand(), - $this->getGoogleAnalytics4ShippingCommand(), - ]; - } - public function getFacebookPixelCommands() { $command = [ From ba702187c7e46cbf854fe0afed70fd0df642e3a8 Mon Sep 17 00:00:00 2001 From: Micah Wittman Date: Thu, 19 Feb 2026 19:23:06 -0400 Subject: [PATCH 2/8] Remove Bing (BingUET) --- Store/StoreAnalyticsOrderTracker.php | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/Store/StoreAnalyticsOrderTracker.php b/Store/StoreAnalyticsOrderTracker.php index 2a838849..346ca83f 100644 --- a/Store/StoreAnalyticsOrderTracker.php +++ b/Store/StoreAnalyticsOrderTracker.php @@ -42,22 +42,6 @@ public function getFacebookPixelCommands() return [$command]; } - public function getBingUETCommands() - { - $command = [ - 'ec' => 'conversion', - 'ea' => 'purchase', - 'gv' => $this->getOrderTotal(), - ]; - - $event_label = $this->getBingUETEventLabel(); - if ($event_label != '') { - $command['el'] = $event_label; - } - - return [$command]; - } - public function getTwitterPixelCommands() { return [ From 4d34ef36c978074cbea8f3136d5d2a5e201fdfc9 Mon Sep 17 00:00:00 2001 From: Micah Wittman Date: Tue, 24 Feb 2026 21:25:46 -0400 Subject: [PATCH 3/8] Google Tag Manager add --- Store/StoreAnalyticsCartDto.php | 23 ++++ Store/StoreAnalyticsCartItemDto.php | 22 ++++ Store/StoreAnalyticsCartTracker.php | 75 +++++++++++++ Store/StoreAnalyticsOrderTracker.php | 158 +++++++++------------------ 4 files changed, 171 insertions(+), 107 deletions(-) create mode 100644 Store/StoreAnalyticsCartDto.php create mode 100644 Store/StoreAnalyticsCartItemDto.php create mode 100644 Store/StoreAnalyticsCartTracker.php diff --git a/Store/StoreAnalyticsCartDto.php b/Store/StoreAnalyticsCartDto.php new file mode 100644 index 00000000..295939c1 --- /dev/null +++ b/Store/StoreAnalyticsCartDto.php @@ -0,0 +1,23 @@ + $items + */ + public function __construct( + public float $value, + public string $currency, + public string $affiliation, + public array $items, + ) { + } +} diff --git a/Store/StoreAnalyticsCartItemDto.php b/Store/StoreAnalyticsCartItemDto.php new file mode 100644 index 00000000..594cd80e --- /dev/null +++ b/Store/StoreAnalyticsCartItemDto.php @@ -0,0 +1,22 @@ +getGoogleTagManagerAddToCartCommand(), + ]; + } + + /** + * @return list + */ + protected function getGoogleTagManagerItemsParameter(): array + { + return array_map( + fn(StoreAnalyticsCartItemDto $item): array => [ + 'item_id' => $item->id, + 'item_name' => $item->name, + 'item_category' => $item->category, + 'item_category2' => $item->category2, + 'affiliation' => $this->add_to_cart->affiliation, + 'price' => $item->price, + 'quantity' => $item->quantity, + ], + $this->add_to_cart->items, + ); + } + + /** + * @return array{ + * event: string, + * ecommerce: array{ + * value: float, + * currency: string, + * items: list, + * }, + * } + */ + protected function getGoogleTagManagerAddToCartCommand(): array + { + return [ + 'event' => 'add_to_cart', + 'ecommerce' => [ + 'value' => $this->add_to_cart->value, + 'currency' => $this->add_to_cart->currency, + 'items' => $this->getGoogleTagManagerItemsParameter(), + ], + ]; + } +} \ No newline at end of file diff --git a/Store/StoreAnalyticsOrderTracker.php b/Store/StoreAnalyticsOrderTracker.php index 346ca83f..dd8df7d8 100644 --- a/Store/StoreAnalyticsOrderTracker.php +++ b/Store/StoreAnalyticsOrderTracker.php @@ -1,17 +1,13 @@ affiliation = $affiliation; } - public function getFacebookPixelCommands() - { - $command = [ - 'track', - 'Purchase', - [ - 'value' => $this->getOrderTotal(), - 'currency' => 'USD', - ], - ]; - - return [$command]; - } + // Google - public function getTwitterPixelCommands() + public function getGoogleTagManagerCommands(): array { return [ - 'tw_sale_amount' => $this->getOrderTotal(), - 'tw_order_quantity' => $this->getOrderQuantity(), + $this->getGoogleTagManagerPurchaseCommand(), + $this->getGoogleTagManagerShippingCommand(), ]; } - protected function getGoogleAnalyticsOrderCommand() - { - $address = $this->getAddress(); - $city = $this->getCity($address); - $provstate_title = $this->getProvStateTitle($address); - $country_title = $this->getCountryTitle($address); - $order_total = $this->getOrderTotal(); - - /* - * Shipping and tax fields cannot be 0 according to Google Analytics - * support article: - * http://www.google.com/support/analytics/bin/answer.py?answer=72291 - * These methods include a workaround. - */ - $tax_total = $this->getTaxTotal(); - $shipping_total = $this->getShippingTotal(); - - return [ - '_addTrans', - $this->order->id, - $this->affiliation, - $order_total, - $tax_total, - $shipping_total, - $city, - $provstate_title, - $country_title, - ]; - } - - protected function getGoogleAnalytics4ItemsParameter(): array + protected function getGoogleTagManagerItemsParameter(): array { $items = []; foreach ($this->order->items as $item) { @@ -96,33 +50,44 @@ protected function getGoogleAnalytics4ItemsParameter(): array return $items; } - protected function getGoogleAnalytics4ShippingCommand(): array + protected function getGoogleTagManagerPurchaseCommand(): array { return [ - 'event' => 'add_shipping_info', - 'event_params' => [ - 'currency' => 'USD', - 'value' => $this->getShippingTotal(), + 'event' => 'purchase', + 'ecommerce' => [ + 'transaction_id' => strval($this->order->id), + 'value' => $this->getOrderTotal(), + 'currency' => 'USD', + 'items' => $this->getGoogleTagManagerItemsParameter(), ], ]; } - protected function getGoogleAnalytics4PurchaseCommand(): array + protected function getGoogleTagManagerShippingCommand(): array { return [ - 'event' => 'purchase', - 'event_params' => [ - 'transaction_id' => strval($this->order->id), - 'value' => $this->getOrderTotal(), - 'currency' => 'USD', - 'items' => $this->getGoogleAnalytics4ItemsParameter(), + 'event' => 'add_shipping_info', + 'ecommerce' => [ + 'currency' => 'USD', + 'value' => $this->getShippingTotal(), ], ]; } - protected function getBingUETEventLabel() + // Facebook + + public function getFacebookPixelCommands() { - return ''; + $command = [ + 'track', + 'Purchase', + [ + 'value' => $this->getOrderTotal(), + 'currency' => 'USD', + ], + ]; + + return [$command]; } protected function getAddress() @@ -130,42 +95,34 @@ protected function getAddress() return $this->order->billing_address; } - protected function getCity(?StoreOrderAddress $address = null) + protected function getCity(?StoreOrderAddress $address = null) : string { - $city = ''; - - if ($address instanceof StoreOrderAddress) { - $city = $address->city; - } - - return $city; + return $address instanceof StoreOrderAddress + ? $address->city + : ''; } - protected function getProvStateTitle(?StoreOrderAddress $address = null) + protected function getProvStateTitle(?StoreOrderAddress $address = null) : string { $title = ''; if ($address instanceof StoreOrderAddress) { - $title = ($address->provstate === null) - ? $address->provstate_other - : $address->provstate->title; + $title = $address->provstate === null + ? $address->provstate_other ?? '' + : $address->provstate->title ?? ''; } return $title; } - protected function getCountryTitle(?StoreOrderAddress $address = null) + protected function getCountryTitle(?StoreOrderAddress $address = null) : string { - $title = ''; - - if ($address instanceof StoreOrderAddress) { - $title = $address->country->title; - } - - return $title; + return $address instanceof StoreOrderAddress + ? $address->country->title + : ''; } - protected function getOrderTotal() + protected function getOrderTotal(): float { return $this->order->total; } @@ -180,37 +137,24 @@ protected function getOrderQuantity() return $quantity; } - protected function getTaxTotal() + protected function getTaxTotal() : float|string { return ($this->order->tax_total == 0) ? '' : $this->order->tax_total; } - protected function getShippingTotal() + protected function getShippingTotal() : float|string { return ($this->order->shipping_total == 0) ? '' : $this->order->shipping_total; } - protected function getGoogleAnalyticsOrderItemCommand(StoreOrderItem $item) - { - return [ - '_addItem', - $this->order->id, - $this->getSku($item), - $this->getProductTitle($item), - $this->getCategoryTitle($item), - $item->price, - $item->quantity, - ]; - } - - protected function getSku(StoreOrderItem $item) + protected function getSku(StoreOrderItem $item) : ?string { return $item->sku; } - protected function getProductTitle(StoreOrderItem $item) + protected function getProductTitle(StoreOrderItem $item) : ?string { return $item->product_title; } From ad94b0f5bc5bc053cdbf5107e29aeb9e722dd319 Mon Sep 17 00:00:00 2001 From: Micah Wittman Date: Fri, 27 Feb 2026 20:28:44 -0400 Subject: [PATCH 4/8] Order tracker add optional coupon (promotion code) --- Store/StoreAnalyticsOrderTracker.php | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/Store/StoreAnalyticsOrderTracker.php b/Store/StoreAnalyticsOrderTracker.php index dd8df7d8..14ca0597 100644 --- a/Store/StoreAnalyticsOrderTracker.php +++ b/Store/StoreAnalyticsOrderTracker.php @@ -11,17 +11,11 @@ */ class StoreAnalyticsOrderTracker { - /** - * @var StoreOrder - */ - protected $order; - - protected $affiliation; - - public function __construct(StoreOrder $order, $affiliation = null) - { - $this->order = $order; - $this->affiliation = $affiliation; + public function __construct( + protected StoreOrder $order, + protected ?string $promotion_code = null, + protected ?string $affiliation = null, + ) { } // Google @@ -52,7 +46,8 @@ protected function getGoogleTagManagerItemsParameter(): array protected function getGoogleTagManagerPurchaseCommand(): array { - return [ + + $data = [ 'event' => 'purchase', 'ecommerce' => [ 'transaction_id' => strval($this->order->id), @@ -61,6 +56,12 @@ protected function getGoogleTagManagerPurchaseCommand(): array 'items' => $this->getGoogleTagManagerItemsParameter(), ], ]; + + if($this->promotion_code !== null) { + $data['ecommerce']['coupon'] = $this->promotion_code; + } + + return $data; } protected function getGoogleTagManagerShippingCommand(): array From 8a8a122e6fff02e981b2361727229eff9fd88c57 Mon Sep 17 00:00:00 2001 From: Micah Wittman Date: Tue, 3 Mar 2026 17:39:52 -0400 Subject: [PATCH 5/8] Add type annotations --- Store/StoreAnalyticsOrderTracker.php | 36 ++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/Store/StoreAnalyticsOrderTracker.php b/Store/StoreAnalyticsOrderTracker.php index 14ca0597..c14c0e9b 100644 --- a/Store/StoreAnalyticsOrderTracker.php +++ b/Store/StoreAnalyticsOrderTracker.php @@ -28,6 +28,15 @@ public function getGoogleTagManagerCommands(): array ]; } + /** + * @return list + */ protected function getGoogleTagManagerItemsParameter(): array { $items = []; @@ -44,6 +53,24 @@ protected function getGoogleTagManagerItemsParameter(): array return $items; } + /** + * @return array{ + * event: 'purchase', + * ecommerce: array{ + * transaction_id: string, + * value: float, + * currency: 'USD', + * items: list, + * coupon?: string, + * }, + * } + */ protected function getGoogleTagManagerPurchaseCommand(): array { @@ -64,6 +91,15 @@ protected function getGoogleTagManagerPurchaseCommand(): array return $data; } + /** + * @return array{ + * event: 'add_shipping_info', + * ecommerce: array{ + * currency: 'USD', + * value: float, + * }, + * } + */ protected function getGoogleTagManagerShippingCommand(): array { return [ From 8c2cb5a8f6b44d20c2e68ecc1a8c772af1345b9c Mon Sep 17 00:00:00 2001 From: Micah Wittman Date: Thu, 5 Mar 2026 14:34:04 -0400 Subject: [PATCH 6/8] Admin Analytics Order Refund added --- Store/StoreAnalyticsOrderRefundDto.php | 24 +++++++ Store/StoreAnalyticsOrderRefundTracker.php | 80 ++++++++++++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 Store/StoreAnalyticsOrderRefundDto.php create mode 100644 Store/StoreAnalyticsOrderRefundTracker.php diff --git a/Store/StoreAnalyticsOrderRefundDto.php b/Store/StoreAnalyticsOrderRefundDto.php new file mode 100644 index 00000000..d3d3a338 --- /dev/null +++ b/Store/StoreAnalyticsOrderRefundDto.php @@ -0,0 +1,24 @@ + $items + */ + public function __construct( + public string $transaction_id, + public float $value, + public float $shipping, + public string $currency, + public array $items, + ) { + } +} diff --git a/Store/StoreAnalyticsOrderRefundTracker.php b/Store/StoreAnalyticsOrderRefundTracker.php new file mode 100644 index 00000000..e3c20668 --- /dev/null +++ b/Store/StoreAnalyticsOrderRefundTracker.php @@ -0,0 +1,80 @@ +getGoogleTagManagerRefundCommand(), + ]; + } + + /** + * @return list + */ + private function getGoogleTagManagerItemsParameter(): array + { + return array_map( + fn(StoreAnalyticsCartItemDto $item): array => [ + 'item_id' => $item->id, + 'item_name' => $item->name, + 'item_category' => $item->category, + 'item_category2' => $item->category2, + 'price' => $item->price, + 'quantity' => $item->quantity, + ], + $this->refund->items, + ); + } + + /** + * @return array{ + * event: string, + * ecommerce: array{ + * transaction_id: string, + * value: float, + * currency: string, + * items: list, + * }, + * } + */ + protected function getGoogleTagManagerRefundCommand(): array + { + return [ + 'event' => 'refund', + 'ecommerce' => [ + 'transaction_id' => $this->refund->transaction_id, + 'value' => $this->refund->value, + 'currency' => $this->refund->currency, + 'items' => $this->getGoogleTagManagerItemsParameter(), + ], + ]; + } +} From 92315cb056d0f3fa799cb3de6b0c62c4086c4370 Mon Sep 17 00:00:00 2001 From: Micah Wittman Date: Fri, 6 Mar 2026 19:58:11 -0400 Subject: [PATCH 7/8] Analytics Trackers and Dto moved down to specific site repo (course-host) --- Store/StoreAnalyticsCartDto.php | 23 --- Store/StoreAnalyticsCartItemDto.php | 22 --- Store/StoreAnalyticsCartTracker.php | 75 -------- Store/StoreAnalyticsOrderRefundDto.php | 24 --- Store/StoreAnalyticsOrderRefundTracker.php | 80 -------- Store/StoreAnalyticsOrderTracker.php | 203 --------------------- 6 files changed, 427 deletions(-) delete mode 100644 Store/StoreAnalyticsCartDto.php delete mode 100644 Store/StoreAnalyticsCartItemDto.php delete mode 100644 Store/StoreAnalyticsCartTracker.php delete mode 100644 Store/StoreAnalyticsOrderRefundDto.php delete mode 100644 Store/StoreAnalyticsOrderRefundTracker.php delete mode 100644 Store/StoreAnalyticsOrderTracker.php diff --git a/Store/StoreAnalyticsCartDto.php b/Store/StoreAnalyticsCartDto.php deleted file mode 100644 index 295939c1..00000000 --- a/Store/StoreAnalyticsCartDto.php +++ /dev/null @@ -1,23 +0,0 @@ - $items - */ - public function __construct( - public float $value, - public string $currency, - public string $affiliation, - public array $items, - ) { - } -} diff --git a/Store/StoreAnalyticsCartItemDto.php b/Store/StoreAnalyticsCartItemDto.php deleted file mode 100644 index 594cd80e..00000000 --- a/Store/StoreAnalyticsCartItemDto.php +++ /dev/null @@ -1,22 +0,0 @@ -getGoogleTagManagerAddToCartCommand(), - ]; - } - - /** - * @return list - */ - protected function getGoogleTagManagerItemsParameter(): array - { - return array_map( - fn(StoreAnalyticsCartItemDto $item): array => [ - 'item_id' => $item->id, - 'item_name' => $item->name, - 'item_category' => $item->category, - 'item_category2' => $item->category2, - 'affiliation' => $this->add_to_cart->affiliation, - 'price' => $item->price, - 'quantity' => $item->quantity, - ], - $this->add_to_cart->items, - ); - } - - /** - * @return array{ - * event: string, - * ecommerce: array{ - * value: float, - * currency: string, - * items: list, - * }, - * } - */ - protected function getGoogleTagManagerAddToCartCommand(): array - { - return [ - 'event' => 'add_to_cart', - 'ecommerce' => [ - 'value' => $this->add_to_cart->value, - 'currency' => $this->add_to_cart->currency, - 'items' => $this->getGoogleTagManagerItemsParameter(), - ], - ]; - } -} \ No newline at end of file diff --git a/Store/StoreAnalyticsOrderRefundDto.php b/Store/StoreAnalyticsOrderRefundDto.php deleted file mode 100644 index d3d3a338..00000000 --- a/Store/StoreAnalyticsOrderRefundDto.php +++ /dev/null @@ -1,24 +0,0 @@ - $items - */ - public function __construct( - public string $transaction_id, - public float $value, - public float $shipping, - public string $currency, - public array $items, - ) { - } -} diff --git a/Store/StoreAnalyticsOrderRefundTracker.php b/Store/StoreAnalyticsOrderRefundTracker.php deleted file mode 100644 index e3c20668..00000000 --- a/Store/StoreAnalyticsOrderRefundTracker.php +++ /dev/null @@ -1,80 +0,0 @@ -getGoogleTagManagerRefundCommand(), - ]; - } - - /** - * @return list - */ - private function getGoogleTagManagerItemsParameter(): array - { - return array_map( - fn(StoreAnalyticsCartItemDto $item): array => [ - 'item_id' => $item->id, - 'item_name' => $item->name, - 'item_category' => $item->category, - 'item_category2' => $item->category2, - 'price' => $item->price, - 'quantity' => $item->quantity, - ], - $this->refund->items, - ); - } - - /** - * @return array{ - * event: string, - * ecommerce: array{ - * transaction_id: string, - * value: float, - * currency: string, - * items: list, - * }, - * } - */ - protected function getGoogleTagManagerRefundCommand(): array - { - return [ - 'event' => 'refund', - 'ecommerce' => [ - 'transaction_id' => $this->refund->transaction_id, - 'value' => $this->refund->value, - 'currency' => $this->refund->currency, - 'items' => $this->getGoogleTagManagerItemsParameter(), - ], - ]; - } -} diff --git a/Store/StoreAnalyticsOrderTracker.php b/Store/StoreAnalyticsOrderTracker.php deleted file mode 100644 index c14c0e9b..00000000 --- a/Store/StoreAnalyticsOrderTracker.php +++ /dev/null @@ -1,203 +0,0 @@ -getGoogleTagManagerPurchaseCommand(), - $this->getGoogleTagManagerShippingCommand(), - ]; - } - - /** - * @return list - */ - protected function getGoogleTagManagerItemsParameter(): array - { - $items = []; - foreach ($this->order->items as $item) { - $items[] = [ - 'item_id' => $this->getSku($item), - 'item_name' => $this->getProductTitle($item), - 'item_category' => $this->getCategoryTitle($item), - 'affiliation' => $this->affiliation, - 'price' => $item->price, - ]; - } - - return $items; - } - - /** - * @return array{ - * event: 'purchase', - * ecommerce: array{ - * transaction_id: string, - * value: float, - * currency: 'USD', - * items: list, - * coupon?: string, - * }, - * } - */ - protected function getGoogleTagManagerPurchaseCommand(): array - { - - $data = [ - 'event' => 'purchase', - 'ecommerce' => [ - 'transaction_id' => strval($this->order->id), - 'value' => $this->getOrderTotal(), - 'currency' => 'USD', - 'items' => $this->getGoogleTagManagerItemsParameter(), - ], - ]; - - if($this->promotion_code !== null) { - $data['ecommerce']['coupon'] = $this->promotion_code; - } - - return $data; - } - - /** - * @return array{ - * event: 'add_shipping_info', - * ecommerce: array{ - * currency: 'USD', - * value: float, - * }, - * } - */ - protected function getGoogleTagManagerShippingCommand(): array - { - return [ - 'event' => 'add_shipping_info', - 'ecommerce' => [ - 'currency' => 'USD', - 'value' => $this->getShippingTotal(), - ], - ]; - } - - // Facebook - - public function getFacebookPixelCommands() - { - $command = [ - 'track', - 'Purchase', - [ - 'value' => $this->getOrderTotal(), - 'currency' => 'USD', - ], - ]; - - return [$command]; - } - - protected function getAddress() - { - return $this->order->billing_address; - } - - protected function getCity(?StoreOrderAddress $address = null) : string - { - return $address instanceof StoreOrderAddress - ? $address->city - : ''; - } - - protected function getProvStateTitle(?StoreOrderAddress $address = null) : string - { - $title = ''; - - if ($address instanceof StoreOrderAddress) { - $title = $address->provstate === null - ? $address->provstate_other ?? '' - : $address->provstate->title ?? ''; - } - - return $title; - } - - protected function getCountryTitle(?StoreOrderAddress $address = null) : string - { - return $address instanceof StoreOrderAddress - ? $address->country->title - : ''; - } - - protected function getOrderTotal(): float - { - return $this->order->total; - } - - protected function getOrderQuantity() - { - $quantity = 0; - foreach ($this->order->items as $item) { - $quantity += $item->quantity; - } - - return $quantity; - } - - protected function getTaxTotal() : float|string - { - return ($this->order->tax_total == 0) ? '' : $this->order->tax_total; - } - - protected function getShippingTotal() : float|string - { - return ($this->order->shipping_total == 0) - ? '' - : $this->order->shipping_total; - } - - protected function getSku(StoreOrderItem $item) : ?string - { - return $item->sku; - } - - protected function getProductTitle(StoreOrderItem $item) : ?string - { - return $item->product_title; - } - - protected function getCategoryTitle(StoreOrderItem $item) - { - return $item->getSourceCategoryTitle(); - } -} From b1e60056db3110cf46cab5e4fc333521ddecb3c8 Mon Sep 17 00:00:00 2001 From: Micah Wittman Date: Sat, 14 Mar 2026 00:34:43 -0300 Subject: [PATCH 8/8] Restore StoreAnalyticsOrderTracker.php, still referenced by emrap --- Store/StoreAnalyticsOrderTracker.php | 258 +++++++++++++++++++++++++++ 1 file changed, 258 insertions(+) create mode 100644 Store/StoreAnalyticsOrderTracker.php diff --git a/Store/StoreAnalyticsOrderTracker.php b/Store/StoreAnalyticsOrderTracker.php new file mode 100644 index 00000000..45e736db --- /dev/null +++ b/Store/StoreAnalyticsOrderTracker.php @@ -0,0 +1,258 @@ +order = $order; + $this->affiliation = $affiliation; + } + + public function getGoogleAnalyticsCommands() + { + $commands = [$this->getGoogleAnalyticsOrderCommand()]; + foreach ($this->order->items as $item) { + $commands[] = $this->getGoogleAnalyticsOrderItemCommand($item); + } + + $commands[] = '_trackTrans'; + + return $commands; + } + + public function getGoogleAnalytics4Commands(): array + { + return [ + $this->getGoogleAnalytics4PurchaseCommand(), + $this->getGoogleAnalytics4ShippingCommand(), + ]; + } + + public function getFacebookPixelCommands() + { + $command = [ + 'track', + 'Purchase', + [ + 'value' => $this->getOrderTotal(), + 'currency' => 'USD', + ], + ]; + + return [$command]; + } + + public function getBingUETCommands() + { + $command = [ + 'ec' => 'conversion', + 'ea' => 'purchase', + 'gv' => $this->getOrderTotal(), + ]; + + $event_label = $this->getBingUETEventLabel(); + if ($event_label != '') { + $command['el'] = $event_label; + } + + return [$command]; + } + + public function getTwitterPixelCommands() + { + return [ + 'tw_sale_amount' => $this->getOrderTotal(), + 'tw_order_quantity' => $this->getOrderQuantity(), + ]; + } + + protected function getGoogleAnalyticsOrderCommand() + { + $address = $this->getAddress(); + $city = $this->getCity($address); + $provstate_title = $this->getProvStateTitle($address); + $country_title = $this->getCountryTitle($address); + $order_total = $this->getOrderTotal(); + + /* + * Shipping and tax fields cannot be 0 according to Google Analytics + * support article: + * http://www.google.com/support/analytics/bin/answer.py?answer=72291 + * These methods include a workaround. + */ + $tax_total = $this->getTaxTotal(); + $shipping_total = $this->getShippingTotal(); + + return [ + '_addTrans', + $this->order->id, + $this->affiliation, + $order_total, + $tax_total, + $shipping_total, + $city, + $provstate_title, + $country_title, + ]; + } + + protected function getGoogleAnalytics4ItemsParameter(): array + { + $items = []; + foreach ($this->order->items as $item) { + $items[] = [ + 'item_id' => $this->getSku($item), + 'item_name' => $this->getProductTitle($item), + 'item_category' => $this->getCategoryTitle($item), + 'affiliation' => $this->affiliation, + 'price' => $item->price, + ]; + } + + return $items; + } + + protected function getGoogleAnalytics4ShippingCommand(): array + { + return [ + 'event' => 'add_shipping_info', + 'event_params' => [ + 'currency' => 'USD', + 'value' => $this->getShippingTotal(), + ], + ]; + } + + protected function getGoogleAnalytics4PurchaseCommand(): array + { + return [ + 'event' => 'purchase', + 'event_params' => [ + 'transaction_id' => strval($this->order->id), + 'value' => $this->getOrderTotal(), + 'currency' => 'USD', + 'items' => $this->getGoogleAnalytics4ItemsParameter(), + ], + ]; + } + + protected function getBingUETEventLabel() + { + return ''; + } + + protected function getAddress() + { + return $this->order->billing_address; + } + + protected function getCity(?StoreOrderAddress $address = null) + { + $city = ''; + + if ($address instanceof StoreOrderAddress) { + $city = $address->city; + } + + return $city; + } + + protected function getProvStateTitle(?StoreOrderAddress $address = null) + { + $title = ''; + + if ($address instanceof StoreOrderAddress) { + $title = ($address->provstate === null) + ? $address->provstate_other + : $address->provstate->title; + } + + return $title; + } + + protected function getCountryTitle(?StoreOrderAddress $address = null) + { + $title = ''; + + if ($address instanceof StoreOrderAddress) { + $title = $address->country->title; + } + + return $title; + } + + protected function getOrderTotal() + { + return $this->order->total; + } + + protected function getOrderQuantity() + { + $quantity = 0; + foreach ($this->order->items as $item) { + $quantity += $item->quantity; + } + + return $quantity; + } + + protected function getTaxTotal() + { + return ($this->order->tax_total == 0) ? '' : $this->order->tax_total; + } + + protected function getShippingTotal() + { + return ($this->order->shipping_total == 0) + ? '' + : $this->order->shipping_total; + } + + protected function getGoogleAnalyticsOrderItemCommand(StoreOrderItem $item) + { + return [ + '_addItem', + $this->order->id, + $this->getSku($item), + $this->getProductTitle($item), + $this->getCategoryTitle($item), + $item->price, + $item->quantity, + ]; + } + + protected function getSku(StoreOrderItem $item) + { + return $item->sku; + } + + protected function getProductTitle(StoreOrderItem $item) + { + return $item->product_title; + } + + protected function getCategoryTitle(StoreOrderItem $item) + { + return $item->getSourceCategoryTitle(); + } +}