You are here

public function CommerceEventsSubscriber::trackProductView in Commerce Google Tag Manager 8.2

Same name and namespace in other branches
  1. 8 src/EventSubscriber/CommerceEventsSubscriber.php \Drupal\commerce_google_tag_manager\EventSubscriber\CommerceEventsSubscriber::trackProductView()

Track the "productView" event.

Parameters

\Symfony\Component\HttpKernel\Event\GetResponseEvent $event: The event to view the product.

File

src/EventSubscriber/CommerceEventsSubscriber.php, line 78

Class

CommerceEventsSubscriber
Event handler for commerce related events.

Namespace

Drupal\commerce_google_tag_manager\EventSubscriber

Code

public function trackProductView(GetResponseEvent $event) {
  $product = $this->routeMatch
    ->getParameter('commerce_product');
  if ($event
    ->getRequest()
    ->getMethod() === 'GET' && !empty($product) && $this->routeMatch
    ->getRouteName() === 'entity.commerce_product.canonical') {
    $default_variation = $product
      ->getDefaultVariation();
    if ($default_variation) {
      $this->eventTracker
        ->productDetailViews([
        $default_variation,
      ]);
    }
  }
}