Commit c098b3c0 by halweg

ga增强

parent 8c1369e2
<?php
namespace Joshine\GoogleFeed\Helper;
use \Magento\Framework\App\Helper\AbstractHelper;
class GAHelper extends AbstractHelper
{
public static function normalizeAndHash(string $hashAlgorithm, string $value): string
{
return hash($hashAlgorithm, strtolower(trim($value)));
}
public static function normalizeAndHashEmailAddress(
string $hashAlgorithm,
string $emailAddress
): string {
$normalizedEmail = strtolower($emailAddress);
$emailParts = explode("@", $normalizedEmail);
if (
count($emailParts) > 1
&& preg_match('/^(gmail|googlemail)\.com\s*/', $emailParts[1])
) {
$emailParts[0] = str_replace(".", "", $emailParts[0]);
$normalizedEmail = sprintf('%s@%s', $emailParts[0], $emailParts[1]);
}
return self::normalizeAndHash($hashAlgorithm, $normalizedEmail);
}
}
\ No newline at end of file
...@@ -7,8 +7,8 @@ ...@@ -7,8 +7,8 @@
$lid = $block->getOrderId(); $lid = $block->getOrderId();
$totalPrice = 0; $totalPrice = 0;
$googleItems = []; $googleItems = [];
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
if ($lid) { if ($lid) {
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$order = $objectManager->create('Magento\Sales\Model\Order')->loadByIncrementId($lid); $order = $objectManager->create('Magento\Sales\Model\Order')->loadByIncrementId($lid);
if ($order) { if ($order) {
$totalPrice = $order->getGrandTotal(); $totalPrice = $order->getGrandTotal();
...@@ -18,11 +18,17 @@ if ($lid) { ...@@ -18,11 +18,17 @@ if ($lid) {
$_item['id'] = $value->getSku(); $_item['id'] = $value->getSku();
$googleItems[] = $_item; $googleItems[] = $_item;
} }
$email = $order->getCustomerEmail();
} }
} }
$currency = $block->getCurrency(); $currency = $block->getCurrency();
$tracking_show = $block->getTrackingEnabled(); $tracking_show = $block->getTrackingEnabled();
$tracking_conversion = $block->getTrackingConversion(); $tracking_conversion = $block->getTrackingConversion();
$helper = $objectManager->get('Joshine\GoogleFeed\Helper\GAHelper');
?> ?>
<div class="checkout-success"> <div class="checkout-success">
...@@ -72,20 +78,33 @@ $tracking_conversion = $block->getTrackingConversion(); ...@@ -72,20 +78,33 @@ $tracking_conversion = $block->getTrackingConversion();
</style> </style>
<script> <script>
window.dataLayer = window.dataLayer || []; //window.dataLayer = window.dataLayer || [];
dataLayer.push({ //dataLayer.push({
'event': 'purchase', // 'event': 'purchase',
'value': <?= $totalPrice ?>, // 'value': <?php //= $totalPrice ?>//,
'items': <?= json_encode($googleItems, true) ?> // 'items': <?php //= json_encode($googleItems, true) ?>
}); //});
<?php if ($block->getOrderId() && $tracking_show > 0 && !empty($tracking_conversion)) :?> <?php if ($block->getOrderId() && $tracking_show > 0 && !empty($tracking_conversion)) :?>
setTimeout(function () { setTimeout(function () {
gtag('event', 'conversion', { gtag('event', 'conversion', {
'send_to': '<?= /* @noEscape */ $tracking_conversion ?>', 'send_to': '<?= /* @noEscape */ $tracking_conversion ?>',
'value': <?= /* @noEscape */ $totalPrice ?>, 'value': '<?= /* @noEscape */ $totalPrice ?>',
'currency': '<?= /* @noEscape */ $currency ?>', 'currency': '<?= /* @noEscape */ $currency ?>',
'transaction_id': '<?= /* @noEscape */ $block->getOrderId() ?>' 'transaction_id': '<?= /* @noEscape */ $block->getOrderId() ?>'
}); });
gtag('event', 'purchase', {
"transaction_id": '<?= /* @noEscape */ $block->getOrderId() ?>',
"value": '<?= /* @noEscape */ $totalPrice ?>',
"currency": '<?= /* @noEscape */ $currency ?>',,
"items": <?= json_encode($googleItems, true); ?>
});
<?php if (isset($email) && !empty($email)) :?>
gtag('set' , 'user_data' , {
"sha256_email_address" : '<?= $helper::normalizeAndHashEmailAddress("sha256", $email); ?>'
});
<?php endif; ?>
}, 1500); }, 1500);
<?php endif;?> <?php endif;?>
</script> </script>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment