Created
December 12, 2019 18:37
-
-
Save flayder/383773cd0f8a60bd175cf41f5e306614 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function func_gen_uuid() { | |
| return sprintf( "%04x%04x-%04x-%04x-%04x-%04x%04x%04x", | |
| mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), | |
| mt_rand( 0, 0xffff ), | |
| mt_rand( 0, 0x0fff ) | 0x4000, | |
| mt_rand( 0, 0x3fff ) | 0x8000, | |
| mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ) | |
| ); | |
| } | |
| function func_send_data_to_ga($params) { | |
| $curl = curl_init(); | |
| curl_setopt($curl,CURLOPT_USERAGENT, urlencode($_SERVER['HTTP_USER_AGENT'])); | |
| curl_setopt($curl, CURLOPT_HTTPHEADER,array('Content-type: application/x-www-form-urlencoded')); | |
| curl_setopt($curl, CURLOPT_URL, "https://www.google-analytics.com/collect"); | |
| curl_setopt($curl, CURLOPT_POST, 1); | |
| curl_setopt($curl, CURLOPT_POSTFIELDS, utf8_encode(http_build_query($params))); | |
| $ret = curl_exec($curl); | |
| CEventLog::Add(array( | |
| "SEVERITY" => "SECURITY", | |
| "AUDIT_TYPE_ID" => "Ответ от аналитики", | |
| "MODULE_ID" => "main", | |
| "ITEM_ID" => 123, | |
| "DESCRIPTION" => serialize($ret), | |
| )); | |
| curl_close($curl); | |
| } | |
| AddEventHandler("sale", "OnOrderAdd", "OnOrderAddHandler"); | |
| function OnOrderAddHandler($ID, $arFields) { | |
| global $APPLICATION; | |
| CModule::IncludeModule('sale'); | |
| CModule::IncludeModule('iblock'); | |
| $isShow = false; | |
| $arOrder = CSaleOrder::GetByID($ID); | |
| $user_id = func_gen_uuid(); | |
| $transaction_data = array( | |
| "v" => 1, | |
| "tid" => "UA-148030512-1", | |
| "cid" => $user_id, | |
| "t" => "event", | |
| "ti" => $ID, | |
| "ta" => 'Мосцветторг', | |
| "tr" => (float)$arOrder['PRICE'], | |
| //"ts" => $order_info["shipping_cost"], | |
| "tt" => (float)$arOrder['TAX_VALUE'], | |
| "cu" => "RUB" | |
| ); | |
| CEventLog::Add(array( | |
| "SEVERITY" => "SECURITY", | |
| "AUDIT_TYPE_ID" => "Новый заказ " . $ID, | |
| "MODULE_ID" => "main", | |
| "ITEM_ID" => 123, | |
| "DESCRIPTION" => serialize($transaction_data), | |
| )); | |
| // Отправляем параметры транзакции | |
| func_send_data_to_ga($transaction_data); | |
| // Перебираем все товары | |
| // 'transactionId': '".$ID."', | |
| // 'transactionAffiliation': 'Мосцветторг', | |
| // 'transactionTotal': ".floatval($arOrder['PRICE']).", | |
| // 'transactionTax': ".floatval($arOrder['TAX_VALUE']).", | |
| // 'transactionShipping': ".intval($arOrder['DELIVERY_ID']).", | |
| // 'transactionProducts': ["; | |
| // $script = "gtag('event', 'purchase', { | |
| // 'transaction_id': '".$ID."', | |
| // 'affiliation': 'Мосцветторг', | |
| // 'value': ".(float)$arOrder['PRICE'].", | |
| // 'currency': 'RUB', | |
| // 'tax': ".(float)$arOrder['TAX_VALUE'].", | |
| // 'shipping': ".intval($arOrder['DELIVERY_ID']).", | |
| // 'items': ["; | |
| foreach ($arFields["BASKET_ITEMS"] as $key => $arItems) | |
| { | |
| $isShow = true; | |
| $item_data = array( | |
| "v" => 1, | |
| "tid" => "UA-148030512-1", | |
| "cid" => $user_id, | |
| "t" => "item", | |
| "cu" => "RUB", | |
| "ti" => $ID, | |
| ); | |
| //$script .= "{"; | |
| $iblock_id = CIBlockElement::GetList(Array(), ["ID" => $arItems["PRODUCT_ID"]], false, false, [])->GetNext(); | |
| if($iblock_id) { | |
| $db_props = CIBlockElement::GetProperty($iblock_id["IBLOCK_ID"], $arItems["PRODUCT_ID"], "sort", "asc", Array("CODE"=>"ARTNUMBER")); | |
| if($ar_props = $db_props->Fetch()) { | |
| if(!empty($ar_props["VALUE"])) { | |
| $item_data["ic"] = $ar_props["VALUE"]; | |
| //$script .= "'id': '".$ar_props["VALUE"]."',"; | |
| } | |
| } | |
| } | |
| //$script .= "'name': '".$arItems["NAME"]."',"; | |
| $item_data["in"] = $arItems["NAME"]; | |
| $rsSections = CIBlockSection::GetList(array(), ["ID" => $iblock_id["IBLOCK_SECTION_ID"]]); | |
| if ($arSction = $rsSections->Fetch()) | |
| { | |
| //$script .= "'category': '".$arSction["NAME"]."',"; | |
| $item_data["iv"] = $arSction["NAME"]; | |
| } | |
| // $script .= " | |
| // 'price': '".(float)$arItems["PRICE"]."', | |
| // 'quantity': ".intval($arItems["QUANTITY"])." | |
| // },"; | |
| $item_data["ip"] = $arItems["PRICE"]; | |
| $item_data["iq"] = $arItems["QUANTITY"]; | |
| CEventLog::Add(array( | |
| "SEVERITY" => "SECURITY", | |
| "AUDIT_TYPE_ID" => "Товар ID ".$arItems["PRODUCT_ID"], | |
| "MODULE_ID" => "main", | |
| "ITEM_ID" => 123, | |
| "DESCRIPTION" => serialize($item_data), | |
| )); | |
| // Отправляем данные о товаре | |
| func_send_data_to_ga($item_data); | |
| } | |
| //$script .= "]});"; | |
| // if($isShow) { | |
| // $_SESSION['ANALYTIC'] = $script; | |
| // } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment