1
0
mirror of synced 2024-11-30 09:16:06 +03:00
bitrix-module/intaro.retailcrm/classes/general/ua/RetailCrmUa.php

102 lines
4.1 KiB
PHP
Raw Normal View History

2017-09-04 11:36:04 +03:00
<?php
/**
* @category RetailCRM
* @package RetailCRM\Ua
* @author RetailCRM <integration@retailcrm.ru>
* @license MIT
* @link http://retailcrm.ru
* @see http://retailcrm.ru/docs
*/
/**
* Class RetailCrmUa
*
* @category RetailCRM
* @package RetailCRM\Ua
*/
2017-09-04 11:36:04 +03:00
class RetailCrmUa
{
public static $MODULE_ID = 'intaro.retailcrm';
public static $CRM_UA = 'ua';
public static $CRM_UA_KEYS = 'ua_keys';
public static function add()
{
$ua = COption::GetOptionString(self::$MODULE_ID, self::$CRM_UA, 0);
$uaKeys = unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_UA_KEYS, 0));
$request = \Bitrix\Main\Context::getCurrent()->getRequest();
2017-09-04 11:36:04 +03:00
if ($ua === 'Y' && !empty($uaKeys[SITE_ID]['ID']) && !empty($uaKeys[SITE_ID]['INDEX']) && $request->isAdminSection() !== true) {
2017-09-04 11:36:04 +03:00
global $APPLICATION;
$ua = "
<script type=\"text/javascript\">
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', '" . $uaKeys[SITE_ID]['ID'] . "', 'auto');
2020-12-15 12:39:38 +03:00
function getRetailCRMCookie(name) {
2017-09-04 11:36:04 +03:00
var matches = document.cookie.match(new RegExp(
'(?:^|; )' + name + '=([^;]*)'
));
return matches ? decodeURIComponent(matches[1]) : '';
}
2020-12-15 12:39:38 +03:00
ga('set', 'dimension" . $uaKeys[SITE_ID]['INDEX'] . "', getRetailCRMCookie('_ga'));
2017-09-04 11:36:04 +03:00
ga('send', 'pageview');
</script>";
/**
* В $_GET['ORDER_ID'] содержится номер заказа, а не его ID.
* Номер может совпадать с ID заказа, но это необязательное условие,
* то есть они могут отличаться.
*/
2017-09-04 11:36:04 +03:00
if (isset($_GET['ORDER_ID'])) {
CModule::IncludeModule("sale");
2018-02-27 15:29:43 +03:00
$order = \Bitrix\Sale\Order::loadByAccountNumber($_GET['ORDER_ID']);
if ($order instanceof \Bitrix\Sale\Order) {
2018-02-27 15:29:43 +03:00
$arOrder = array(
'ID' => $order->getId(),
'PRICE' => $order->getPrice(),
'DISCOUNT_VALUE' => $order->getField('DISCOUNT_VALUE')
);
$ua .= "<script type=\"text/javascript\">
2017-09-04 11:36:04 +03:00
ga('require', 'ecommerce', 'ecommerce.js');
ga('ecommerce:addTransaction', {
'id': $arOrder[ID],
'affiliation': '$_SERVER[SERVER_NAME]',
'revenue': $arOrder[PRICE],
'tax': $arOrder[DISCOUNT_VALUE]
});
";
2018-02-27 15:29:43 +03:00
$arBasketItems = CsaleBasket::GetList(
array('id' => 'asc'),
array('ORDER_ID' => $_GET['ORDER_ID']),
false,
false,
array('PRODUCT_ID', 'NAME', 'PRICE', 'QUANTITY', 'ORDER_ID', 'ID')
);
while ($arItem = $arBasketItems->fetch()) {
$ua .= "
ga('ecommerce:addItem', {
'id': $arOrder[ID],
'sku': '$arItem[PRODUCT_ID]',
'name': '$arItem[NAME]',
'price': $arItem[PRICE],
'quantity': $arItem[QUANTITY]
});
";
}
$ua .= "ga('ecommerce:send');";
2018-09-21 12:22:49 +03:00
$ua .= "</script>";
2017-09-04 11:36:04 +03:00
}
}
$APPLICATION->AddHeadString($ua);
}
}
}