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
16 changes: 16 additions & 0 deletions Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,22 @@ public function isEnabled(): bool
return true;
}

public function isEnabledBusinessVertical(): bool
{
$configValue = $this->getModuleConfigValue('business_vertical', '');

if (empty($configValue)) {
return false;
}

return true;
}

public function getBusinessVertical(): string
{
return (string)$this->getModuleConfigValue('business_vertical', '');
}

/**
* Check if lifetime value calculation is enabled in configuration
*
Expand Down
17 changes: 17 additions & 0 deletions Config/Source/BusinessVertical.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
namespace Tagging\GTM\Config\Source;

class BusinessVertical implements \Magento\Framework\Option\ArrayInterface
{
public function toOptionArray()
{
return [
['value' => '', 'label' => __('Disabled')],
['value' => 'retail', 'label' => __('Retail')],
['value' => 'flights', 'label' => __('Flights')],
['value' => 'hotel_rental', 'label' => __('Hotel Rental')],
['value' => 'jobs', 'label' => __('Jobs')],
['value' => 'real_estate', 'label' => __('Real Estate')]
];
}
}
11 changes: 8 additions & 3 deletions DataLayer/Event/AddShippingInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
use Magento\Quote\Api\ShippingMethodManagementInterface;
use Tagging\GTM\Api\Data\EventInterface;
use Tagging\GTM\DataLayer\Tag\Cart\CartItems;

use Tagging\GTM\Util\PriceFormatter;
class AddShippingInfo implements EventInterface
{
private CartItems $cartItems;
private ShippingMethodManagementInterface $shippingMethodManagement;
private CheckoutSession $checkoutSession;

private PriceFormatter $priceFormatter;
/**
* @param CartItems $cartItems
* @param ShippingMethodManagementInterface $shippingMethodManagement
Expand All @@ -26,11 +26,13 @@ class AddShippingInfo implements EventInterface
public function __construct(
CartItems $cartItems,
ShippingMethodManagementInterface $shippingMethodManagement,
CheckoutSession $checkoutSession
CheckoutSession $checkoutSession,
PriceFormatter $priceFormatter
) {
$this->cartItems = $cartItems;
$this->shippingMethodManagement = $shippingMethodManagement;
$this->checkoutSession = $checkoutSession;
$this->priceFormatter = $priceFormatter;
}

/**
Expand All @@ -56,6 +58,9 @@ public function get(): array
return [
'event' => 'trytagging_add_shipping_info',
'ecommerce' => [
'currency' => $quote->getQuoteCurrencyCode(),
'value' => $this->priceFormatter->format((float)$quote->getGrandTotal()),
'coupon' => $quote->getCouponCode(),
'shipping_tier' => $shippingMethod,
'items' => $this->cartItems->get(),
],
Expand Down
42 changes: 30 additions & 12 deletions DataLayer/Event/PurchaseWebhookEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,17 +99,38 @@ public function purchase(OrderInterface $order)
];

try {
$email = $order->getBillingAddress() ? $order->getBillingAddress()->getEmail() ?? '' : '';
$firstName = $order->getBillingAddress() ? $order->getBillingAddress()->getFirstname() ?? '' : '';
$lastName = $order->getBillingAddress() ? $order->getBillingAddress()->getLastname() ?? '' : '';
$phone = $order->getBillingAddress() ? $order->getBillingAddress()->getTelephone() ?? '' : '';
$address = $order->getBillingAddress() && $order->getBillingAddress()->getStreet() ? $order->getBillingAddress()->getStreet()[0] ?? '' : '';
$city = $order->getBillingAddress() ? $order->getBillingAddress()->getCity() ?? '' : '';
$state = $order->getBillingAddress() ? $order->getBillingAddress()->getRegion() ?? '' : '';
$postcode = $order->getBillingAddress() ? $order->getBillingAddress()->getPostcode() ?? '' : '';
$country = $order->getBillingAddress() ? $order->getBillingAddress()->getCountryId() ?? '' : '';

$data['user_data'] = [
"customer_id" => $order->getCustomerId() ?? '',
"billing_first_name" => $order->getBillingAddress() ? $order->getBillingAddress()->getFirstname() ?? '' : '',
"billing_last_name" => $order->getBillingAddress() ? $order->getBillingAddress()->getLastname() ?? '' : '',
"billing_address" => $order->getBillingAddress() && $order->getBillingAddress()->getStreet() ? $order->getBillingAddress()->getStreet()[0] ?? '' : '',
"billing_postcode" => $order->getBillingAddress() ? $order->getBillingAddress()->getPostcode() ?? '' : '',
"billing_country" => $order->getBillingAddress() ? $order->getBillingAddress()->getCountryId() ?? '' : '',
"billing_state" => $order->getBillingAddress() ? $order->getBillingAddress()->getRegion() ?? '' : '',
"billing_city" => $order->getBillingAddress() ? $order->getBillingAddress()->getCity() ?? '' : '',
"billing_email" => $order->getBillingAddress() ? $order->getBillingAddress()->getEmail() ?? '' : '',
"billing_phone" => $order->getBillingAddress() ? $order->getBillingAddress()->getTelephone() ?? '' : '',
"customer_email" => $email,
"customer_name" => $firstName . ' ' . $lastName,
"customer_first_name" => $firstName,
"customer_last_name" => $lastName,
"customer_phone" => $phone,
"customer_address" => $address,
"customer_city" => $city,
"customer_state" => $state,
"customer_zip" => $postcode,
"customer_country" => $country,

"billing_first_name" => $firstName,
"billing_last_name" => $lastName,
"billing_address" => $address,
"billing_postcode" => $postcode,
"billing_country" => $country,
"billing_state" => $state,
"billing_city" => $city,
"billing_email" => $email,
"billing_phone" => $phone,
"shipping_first_name" => $order->getShippingAddress() ? $order->getShippingAddress()->getFirstname() ?? '' : '',
"shipping_last_name" => $order->getShippingAddress() ? $order->getShippingAddress()->getLastname() ?? '' : '',
"shipping_company" => $order->getShippingAddress() ? $order->getShippingAddress()->getCompany() ?? '' : '',
Expand All @@ -119,9 +140,6 @@ public function purchase(OrderInterface $order)
"shipping_state" => $order->getShippingAddress() ? $order->getShippingAddress()->getRegion() ?? '' : '',
"shipping_city" => $order->getShippingAddress() ? $order->getShippingAddress()->getCity() ?? '' : '',
"shipping_phone" => $order->getShippingAddress() ? $order->getShippingAddress()->getTelephone() ?? '' : '',
"email" => $order->getCustomerEmail() ?? '',
"first_name" => $order->getCustomerFirstname() ?? '',
"last_name" => $order->getCustomerLastname() ?? '',
"new_customer" => (string)($order->getCustomerIsGuest() ? "true" : "false")
];
} catch (\Exception $e) {
Expand Down
19 changes: 16 additions & 3 deletions DataLayer/Event/RemoveFromCart.php
Original file line number Diff line number Diff line change
@@ -1,22 +1,33 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace Tagging\GTM\DataLayer\Event;

use Magento\Quote\Api\Data\CartItemInterface;
use Tagging\GTM\Api\Data\EventInterface;
use Tagging\GTM\DataLayer\Tag\CurrencyCode;
use Tagging\GTM\Util\PriceFormatter;
use Tagging\GTM\DataLayer\Mapper\CartItemDataMapper;

class RemoveFromCart implements EventInterface
{
private CartItemDataMapper $cartItemDataMapper;
private CartItemInterface $cartItem;
private CurrencyCode $currencyCode;
private PriceFormatter $priceFormatter;

/**
* @param CartItemDataMapper $cartItemDataMapper
*/
public function __construct(CartItemDataMapper $cartItemDataMapper)
{
public function __construct(
CartItemDataMapper $cartItemDataMapper,
CurrencyCode $currencyCode,
PriceFormatter $priceFormatter
) {
$this->cartItemDataMapper = $cartItemDataMapper;
$this->currencyCode = $currencyCode;
$this->priceFormatter = $priceFormatter;
}

/**
Expand All @@ -28,6 +39,8 @@ public function get(): array
return [
'event' => 'trytagging_remove_from_cart',
'ecommerce' => [
'currency' => $this->currencyCode->get(),
'value' => $this->priceFormatter->format((float)$cartItemData['price'] * (int)$cartItemData['quantity']),
'items' => [$cartItemData]
]
];
Expand Down
13 changes: 6 additions & 7 deletions DataLayer/Event/UserData.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,25 @@
use Magento\Framework\Exception\NoSuchEntityException;
use Tagging\GTM\Api\Data\EventInterface;
use Tagging\GTM\DataLayer\Tag\PageTitle;
use Tagging\GTM\DataLayer\Tag\PageType;
use Tagging\GTM\DataLayer\Tag\PagePath;
use Tagging\GTM\DataLayer\Tag\Store\CurrentStore;

class UserData implements EventInterface
{
private PageTitle $pageTitle;
private PageType $pageType;
private PagePath $pagePath;
private CurrentStore $currentStore;

/**
* @param Customer $cartItems
*/
public function __construct(
PageTitle $pageTitle,
PageType $pageType,
PagePath $pagePath,
CurrentStore $currentStore
) {
$this->pageTitle = $pageTitle;
$this->pageType = $pageType;
$this->pagePath = $pagePath;
$this->currentStore = $currentStore;
}

Expand All @@ -41,9 +41,8 @@ public function get(): array
'event' => 'trytagging_user_data',
'page' => [
'title' => $this->pageTitle->get(),
'type' => $this->pageType->get()
],
'store' => $this->currentStore->get(),
'location' => $this->pagePath->get()
]
];
}
}
2 changes: 1 addition & 1 deletion DataLayer/Mapper/OrderItemDataMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function mapByOrderItem(OrderItemInterface $orderItem, ?OrderInterface $o
$orderItemData = [
'item_id' => $orderItem->getSku(),
'item_name' => $orderItem->getName(),
'discount' => (float) $orderItem->getDiscountAmount(),
'discount' => $this->priceFormatter->format((float) $orderItem->getDiscountAmount()),
'quantity' => (float) $orderItem->getQtyOrdered(),
'price' => $this->getPrice($orderItem)
];
Expand Down
10 changes: 6 additions & 4 deletions DataLayer/Mapper/ProductDataMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,14 @@ public function mapByProduct(ProductInterface $product): array
$productData[$dataLayerKey] = $attributeValue;
}

$productData['item_id'] = $product->getSku();
$productData['item_sku'] = $product->getSku();
$productData['magento_sku'] = $product->getSku();
$productData['item_id'] = strval($product->getSku());
$productData['magento_id'] = $product->getId();

$parentIds = $this->configurableType->getParentIdsByChild($product->getId());

if (!empty($parentIds)) {
$parentProduct = $this->productRepository->getById($parentIds[0]);
$productData['item_id'] = $parentProduct->getSku();
$productData['item_id'] = strval($parentProduct->getSku());
$productData['item_variant'] = $product->getSku();
}

Expand All @@ -101,6 +99,10 @@ public function mapByProduct(ProductInterface $product): array
$productData = $this->parseDataLayerMapping($product, $productData);
$productData['index'] = $this->counter++;

if ($this->config->isEnabledBusinessVertical()) {
$productData['google_business_vertical'] = $this->config->getBusinessVertical();
}

// @todo: Add "variant" reference to Configurable Product

return $productData;
Expand Down
1 change: 0 additions & 1 deletion Test/Integration/Page/CategoryPageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ public function testValidDataLayerWithOneCategory()
$this->assertNotEmpty($event['ecommerce']['items'], var_export($event, true));
foreach ($event['ecommerce']['items'] as $productData) {
$this->assertNotEmpty($productData['item_id']);
$this->assertNotEmpty($productData['item_sku']);
$this->assertNotEmpty($productData['item_list_name']);
}
}
Expand Down
2 changes: 0 additions & 2 deletions Test/Integration/Util/ProductDataMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ public function testMapByProduct()

$this->assertNonEmptyValueInArray('item_id', $productData);
$this->assertSame(1, $productData['magento_id']);
$this->assertSame('product1', $productData['magento_sku']);
$this->assertSame('product1', $productData['item_id']);
$this->assertSame('product1', $productData['item_sku']);
$this->assertSame('Product 1', $productData['item_name']);
$this->assertSame(1.42, $productData['price']);
}
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tagginggroup/gtm",
"version": "1.0.13",
"version": "2.0.0",
"license": "OSL-3.0",
"type": "magento2-module",
"description": "AdPage tagging integration for Magento 2",
Expand Down
30 changes: 21 additions & 9 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,7 @@
<field id="enabled">1</field>
</depends>
</field>
<field id="lifetime_value" type="select" translate="label" sortOrder="3" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Include lifetime value</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<depends>
<field id="enabled">1</field>
</depends>
</field>

<field id="serverside_gtm_url" type="text" translate="label" sortOrder="3" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Container URL</label>
<comment><![CDATA[<span>Enter the container url without https://<span><br/>
Expand All @@ -54,8 +48,26 @@
<field id="enabled">1</field>
</depends>
</field>
<field id="choose_script_placement" type="select" translate="label" sortOrder="5" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Choose script placement</label>
<field id="business_vertical" type="select" translate="label" sortOrder="5" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Business Vertical</label>
<source_model>Tagging\GTM\Config\Source\BusinessVertical</source_model>
<comment><![CDATA[<span>This will add the google_business_vertical field to the data layer. ]]></comment>
<depends>
<field id="enabled">1</field>
</depends>
</field>


<field id="lifetime_value" type="select" translate="label" sortOrder="6" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Include lifetime value (Advanced)</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<comment><![CDATA[<span>Disable the lifetime value variable in the data layer. ]]></comment>
<depends>
<field id="enabled">1</field>
</depends>
</field>
<field id="choose_script_placement" type="select" translate="label" sortOrder="7" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Choose script placement (Advanced)</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<comment><![CDATA[<span>If you want to control on where the scripts are placed you can enable this option. Then the plugin will not load the files and you have to do it on your own.<span><br/>
<b>NOTE</b>; If all necessary scripts are not included, tracking will not function. ]]></comment>
Expand Down
1 change: 1 addition & 0 deletions view/frontend/layout/catalog_category_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<item name="view_item_list_event" xsi:type="array">
<item name="event" xsi:type="string">trytagging_view_item_list</item>
<item name="ecommerce" xsi:type="array">
<item name="currency" xsi:type="object">Tagging\GTM\DataLayer\Tag\CurrencyCode</item>
<item name="items" xsi:type="object">Tagging\GTM\DataLayer\Tag\Category\Products</item>
</item>
</item>
Expand Down
6 changes: 5 additions & 1 deletion view/frontend/templates/hyva/script-pusher.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ declare(strict_types=1);

if (window.taggingHelpers) {
eventData.marketing = window.taggingHelpers.getMarketingObject();
eventData.device = window.taggingHelpers.getDeviceInfo();

if (eventData.event === 'trytagging_user_data') {
eventData.device = window.taggingHelpers.getDeviceInfo();
// eventData.cart.total = 0.00; TODO: Needs to be implemented
}
}

if (eventData.marketing) {
Expand Down
8 changes: 3 additions & 5 deletions view/frontend/templates/script.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,8 @@ $config = $block->getConfig();
})
})
})(['load']);
</script>

<?php if ($config->isDebug()) : ?>
<script>
<?php if ($config->isDebug()) : ?>
window.Tagging_GTM_DEBUG = true;
</script>
<?php endif; ?>
<?php endif; ?>
</script>
1 change: 1 addition & 0 deletions view/frontend/web/js/mixins/catalog-add-to-cart-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ define([
const eventData = {
'event': 'trytagging_add_to_cart',
'ecommerce': {
'value': productData.price * productData.quantity,
'items': [productData]
}
};
Expand Down
Loading