Обработчик яндекс метрики и гугл аналитики для отправки транзакций при оформлении заказа
**
* @param int $orderId
* @param $arOrder
* @param $arParams
*/
function AddGoogleAnalyticsEcommerceCode($orderId, $arOrder, $arParams)
{
global $APPLICATION;
if (isset($_SESSION['GA_ORDER_ID']) && $_SESSION['GA_ORDER_ID'] == $orderId) {
return;
}
if (!($order = CSaleOrder::GetByID($orderId))) {
return;
}
$output = array();
$site = CSite::GetByID($order['LID'])->Fetch();
$output[] = "ga('require', 'ecommerce', 'ecommerce.js');";
$output[] = sprintf("ga('ecommerce:addTransaction', %s);",
CUtil::PhpToJSObject(
array(
'id' => (int)$orderId,
'affiliation' => (string)$site['NAME'],
'revenue' => (float)$order['PRICE'],
'shipping' => (float)$order['PRICE_DELIVERY'],
'tax' => '',
),
false,
true,
true
)
);
$basketIterator = CSaleBasket::GetList(
array(
'NAME' => 'ASC',
),
array(
'ORDER_ID' => $orderId,
),
false,
false,
array(
'PRODUCT_ID',
'NAME',
'PRICE',
'QUANTITY',
)
);
$basketItems = array();
$productsIds = array();
$productsData = array();
while ($basketItem = $basketIterator->fetch()) {
$basketItems[] = $basketItem;
$productsIds[] = $basketItem['PRODUCT_ID'];
}
unset($basketItem);
$resProducts = CIBlockElement::GetList(
array(),
array(
'ID' => array_unique($productsIds)
),
false,
false,
array(
'ID',
'IBLOCK_ID',
'IBLOCK_SECTION_ID',
)
);
while ($arProduct = $resProducts->Fetch()) {
$arProduct['SECTION_NAME'] = '';
if (intval($arProduct['IBLOCK_SECTION_ID']) > 0) {
$sectionIterator = \CIBlockSection::GetList(
array(),
array(
'ID' => $arProduct['IBLOCK_SECTION_ID'],
),
false,
array(
'NAME',
)
);
if ($arSection = $sectionIterator->Fetch()) {
$arProduct['SECTION_NAME'] = $arSection['NAME'];
}
}
$productsData[$arProduct['ID']] = $arProduct;
}
$outputYandex[] = "window.dataLayer = window.dataLayer || [];";
$outputYandex[] = "window.dataProxy = window.dataProxy || [];";
$outputYandex[] = 'dataProxy[10]={
"ecommerce": {
"purchase": {
"actionField": {
"id" : ' . (int)$orderId . ',
"goal_id" : "27833884"
},
"products": [
]
}
}
};';
foreach ($basketItems as $basketItem) {
$output[] = sprintf(
"ga('ecommerce:addItem', %s);",
CUtil::PhpToJSObject(
array(
'id' => (int)$orderId,
'name' => (string)$basketItem['NAME'],
'sku' => (string)$basketItem['PRODUCT_ID'],
'category' => (string)(isset($productsData[$basketItem['PRODUCT_ID']])
? $productsData[$basketItem['PRODUCT_ID']]['SECTION_NAME']
: ''),
'price' => (float)$basketItem['PRICE'],
'quantity' => (int)$basketItem['QUANTITY'],
),
false,
true,
true
)
);
$outputYandex[] = sprintf(
'dataProxy[10].ecommerce.purchase.products.push(%s);',
CUtil::PhpToJSObject(
array(
'id' => (string)$basketItem['PRODUCT_ID'],
'name' => (string)$basketItem['NAME'],
'category' => (string)(isset($productsData[$basketItem['PRODUCT_ID']])
? $productsData[$basketItem['PRODUCT_ID']]['SECTION_NAME']
: ''),
'price' => (float)$basketItem['PRICE'],
'quantity' => (int)$basketItem['QUANTITY'],
),
false,
true,
true
)
);
}
$output[] = "ga('ecommerce:send');";
$outputYandex[] = 'dataLayer.push(dataProxy[10]);';
echo sprintf('<script>%s</script>', implode("\n", $output));
echo sprintf('<script>%s</script>', implode("\n", $outputYandex));
$_SESSION['GA_ORDER_ID'] = $orderId;
}