Skip to content

Commit

Permalink
MageWire support
Browse files Browse the repository at this point in the history
  • Loading branch information
jissereitsma committed Dec 7, 2023
1 parent d87b343 commit 267eaf6
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [3.7.0] - 7 December 2023
### Added
- Hyva Checkout support (@hans-vereyken)

## [3.6.5] - 28 November 2023
### Fixed
- Make sure to use `gtm-checkout` in the checkout, not customerData `cart`
Expand Down
42 changes: 42 additions & 0 deletions MageWire/Checkout.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
declare(strict_types=1);

namespace Yireo\GoogleTagManager2\MageWire;

use Magento\Checkout\Model\Session as CheckoutSession;
use Yireo\GoogleTagManager2\DataLayer\Event\AddPaymentInfo;
use Yireo\GoogleTagManager2\DataLayer\Event\AddShippingInfo;
use Yireo\GoogleTagManager2\DataLayer\Event\BeginCheckout;

class Checkout extends Component
{
protected $listeners = [
'shipping_method_selected' => 'triggerShippingMethod',
'payment_method_selected' => 'triggerPaymentMethod',
];

public function __construct(
private readonly CheckoutSession $checkoutSession,

Check failure on line 19 in MageWire/Checkout.php

View workflow job for this annotation

GitHub Actions / PHPStan

Syntax error, unexpected T_STRING, expecting T_VARIABLE on line 19

Check failure on line 19 in MageWire/Checkout.php

View workflow job for this annotation

GitHub Actions / PHPStan

Syntax error, unexpected T_STRING, expecting T_VARIABLE on line 19
private readonly BeginCheckout $beginCheckout,
private readonly AddShippingInfo $addShippingInfo,
private readonly AddPaymentInfo $addPaymentInfo,
) {
}

public function triggerBeginCheckout()
{
$this->dispatchBrowserEvent('ga:trigger-event', $this->beginCheckout->get());
}

public function triggerShippingMethod()
{
$this->dispatchBrowserEvent('ga:trigger-event', $this->addShippingInfo->get());
}

public function triggerPaymentMethod()
{
$this->addPaymentInfo->setCartId((int) $this->checkoutSession->getQuote()->getId());
$this->addPaymentInfo->setPaymentMethod((string) $this->checkoutSession->getQuote()->getPayment()->getMethod());
$this->dispatchBrowserEvent('ga:trigger-event', $this->addPaymentInfo->get());
}
}
9 changes: 9 additions & 0 deletions MageWire/Component.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php declare(strict_types=1);

namespace Yireo\GoogleTagManager2\MageWire;

if (class_exists('\Magewirephp\Magewire\Component')) {
class Component extends \Magewirephp\Magewire\Component {}
} else {
class Component {}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "yireo/magento2-googletagmanager2",
"version": "3.6.5",
"version": "3.7.0",
"license": "OSL-3.0",
"type": "magento2-module",
"homepage": "https://www.yireo.com/software/magento-extensions/googletagmanager2",
Expand Down
16 changes: 16 additions & 0 deletions view/frontend/layout/hyva_checkout_index_index.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
layout="checkout"
xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="main">
<block template="Yireo_GoogleTagManager2::hyva_checkout/data-layer.phtml"
ifconfig="googletagmanager2/settings/enabled">
<arguments>
<argument name="magewire" xsi:type="object">Yireo\GoogleTagManager2\MageWire\Checkout</argument>
<argument name="begin_checkout_event" xsi:type="object">Yireo\GoogleTagManager2\DataLayer\Event\BeginCheckout</argument>
</arguments>
</block>
</referenceContainer>
</body>
</page>
32 changes: 32 additions & 0 deletions view/frontend/templates/hyva_checkout/data-layer.phtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
declare(strict_types=1);

use Magento\Framework\View\Element\Template;
use Yireo\GoogleTagManager2\DataLayer\Event\BeginCheckout;

/** @var Template $block */
/** @var BeginCheckout $beginCheckoutEvent */

$beginCheckoutEvent = $block->getData('begin_checkout_event');
?>
<script>
function triggerEvent(event) {
yireoGoogleTagManager2Pusher(event.detail);
}

function initYireoGoogleTagManager() {
"use strict";

triggerEvent(new CustomEvent('ga:trigger-event', {detail:<?= json_encode($beginCheckoutEvent->get()) ?>}));

return {
triggerEvent
}
}
</script>
<div id="ga-trigger"
x-data="initYireoGoogleTagManager()"
class="hidden"
@ga:trigger-event.window="triggerEvent($event)"
></div>

0 comments on commit 267eaf6

Please sign in to comment.