You are here

public function EventTrackerService::purchase in Commerce Google Tag Manager 8.2

Same name and namespace in other branches
  1. 8 src/EventTrackerService.php \Drupal\commerce_google_tag_manager\EventTrackerService::purchase()

Track a purchase of the given order entity.

Parameters

\Drupal\commerce_order\Entity\OrderInterface $order: A commerce order entity.

File

src/EventTrackerService.php, line 276

Class

EventTrackerService
Track different events from Google's Enhanced Ecommerce.

Namespace

Drupal\commerce_google_tag_manager

Code

public function purchase(OrderInterface $order) {
  $data = [
    'event' => self::EVENT_PURCHASE,
    'ecommerce' => [
      'purchase' => [
        'actionField' => [
          'id' => $order
            ->getOrderNumber(),
          'affiliation' => $order
            ->getStore()
            ->getName(),
          // The revenu should be the total value (incl. tax and shipping).
          'revenue' => self::formatPrice((double) $order
            ->getTotalPrice()
            ->getNumber()),
          'shipping' => self::formatPrice($this
            ->calculateShipping($order)),
          'tax' => $this
            ->formatPrice($this
            ->calculateTax($order)),
          'coupon' => $this
            ->getCouponCode($order),
        ],
        'products' => $this
          ->buildProductsFromOrderItems($order
          ->getItems()),
      ],
    ],
  ];
  $this->eventStorage
    ->addEvent($data);
}