Совместная работа GA4 и GU в 1С Битрикс

Совместная работа GA4 и GU в 1С Битрикс

35
Подключить в header.php






На странице окончания оформления заказа отправка данных в обе аналитики


\Bitrix\Main\Loader::includeModule('sale');
\Bitrix\Main\Loader::includeModule('iblock');

$ga4_data = [];
if( isset($_GET['order_id']) && intval($_GET['order_id'])>0 ){
	$arResult = array();
	$arResult['ORDER_ECOM_DATA'] = array();

	$order = \Bitrix\Sale\Order::load($_GET['order_id']);
	if($order) {
		$id_order = $order->getId();
		$basket = \Bitrix\Sale\Order::load($id_order)->getBasket();
		$basketItems = $basket->getBasketItems();

		$coupon = '';
		$coupon_f = \Bitrix\Sale\Internals\OrderCouponsTable::getList(array(
			'select' => array('COUPON'),
			'filter' => array('=ORDER_ID' => $id_order)
		))->fetch();
		if($coupon_f) {
			$coupon = $coupon_f['COUPON'];
		}

		$arResult['ORDER_ECOM_DATA'] = array(
			'id' => (string)$id_order,
			'revenue' => (string)number_format($order->getPrice(),2,'.', ''),
			'tax' => (string)number_format(0,2,'.', ''),
			'shipping' => (string)number_format($order->getDeliveryPrice(),2,'.', ''),
			'coupon' => $coupon,
		);
        $ga4_data = [
            'currency' => 'RUB',
            'transaction_id' => (string)$id_order,
            'value' => (string)number_format($order->getPrice(),2,'.', ''),
            'tax' => (string)number_format(0,2,'.', ''),
            'shipping' => (string)number_format($order->getDeliveryPrice(),2,'.', ''),
            'coupon' => $coupon,
            'affiliation' => 'МПР - Магазин постоянных распродаж'
        ];


	}

	$arrProducts = array();
	foreach ($basketItems as $basketItem) {
		$product_id = $basketItem->getProductId();
		$arrProducts[$product_id]['PRODUCT_ID'] = $product_id;  //$basketItem['PRODUCT_ID'];
		$arrProducts[$product_id]['QUANTITY'] = $basketItem->getQuantity();    //$basketItem['QUANTITY'];
		$arrProducts[$product_id]['PRICE'] = $basketItem->getPrice();       //$basketItem['PRICE'];
	}
	//dump($priceOrder, false, true);
	//dump($id_order, false, true);
	//dump($arrProducts[$product_id]['PRODUCT_ID'], false, true);
	
	if (!empty($arrProducts)) {

		$arrSections = array();
		$element_res = CIBlockElement::GetList([], ['=ID' => array_keys($arrProducts), 'IBLOCK_ID' => IBLOCK_ID_CATALOG], false, false, [
			'ID',
			'NAME',
			'CODE',
			'IBLOCK_SECTION_ID',
			// 'PROPERTY_BRAND.NAME',
			'PROPERTY_CML2_ARTICLE',
		]);
		while($arr = $element_res->GetNext()) {
			$arrSections[$arr['~IBLOCK_SECTION_ID']] = $arr['~IBLOCK_SECTION_ID'];
			$arrProducts[$arr['ID']]['NAME'] = $arr['NAME'];
			$arrProducts[$arr['ID']]['CODE'] = $arr['CODE'];
			$arrProducts[$arr['ID']]['ARTICLE'] = $arr['PROPERTY_CML2_ARTICLE_VALUE'];
			$arrProducts[$arr['ID']]['SECTION_ID'] = $arr['~IBLOCK_SECTION_ID'];
		}
		if(count($arrSections)) {
			foreach($arrSections as $id_s) {
				$res = CIBlockSection::GetNavChain(IBLOCK_ID_CATALOG, $id_s, array('ID', 'NAME', 'IBLOCK_SECTION_ID'));
				$arrTree = array();
				while($arr = $res->Fetch()) {
					$arrTree[] = $arr['NAME'];
				}
				$arrSections[$id_s] = implode('/', $arrTree);
			}
		}

		$cnt = 0;
		foreach ($arrProducts as $id => $rows) {
			$cnt++;
			$arResult['ECOM_DATA'][] = array(
				'name' => htmlspecialchars_decode(strip_tags($rows['NAME'])),
				'id' => !empty($rows['ARTICLE'])?$rows['ARTICLE']:$rows['CODE'],
				'price' => (string)number_format($rows['PRICE'],2,'.', ''),
				'brand' => '',
				'category' => htmlspecialchars_decode(strip_tags($arrSections[$rows['SECTION_ID']])),
				'quantity' => $rows['QUANTITY'],
				'position' => $cnt,
			);

            $ga4_data['items'][] = array(
                'item_id' => !empty($rows['ARTICLE'])?$rows['ARTICLE']:$rows['CODE'],
                'item_name' => htmlspecialchars_decode(strip_tags($rows['NAME'])),
                'price' => (string)number_format($rows['PRICE'],2,'.', ''),
                'item_category' => htmlspecialchars_decode(strip_tags($arrSections[$rows['SECTION_ID']])),
                'quantity' => $rows['QUANTITY'],
            );
		}
	}
}


if( isset($arResult['ECOM_DATA']) && !empty($arResult['ECOM_DATA']) ):?>
    
}