try { window.ww_this_script = document.currentScript; } catch (e) { } jQuery(function () { var $ = jQuery; var is_miami = false; if (!SW_BS.isInit) { SW_BS.not_show_error_message = true; SW_BS.init(); } // console.log('client_code counter started'); var is_beta = (window.location.hostname.indexOf('beta.') === 0); if (ww_this_script && ww_this_script.src && ww_this_script.src.indexOf('wisewill.us') !== -1) { is_miami = true; base_url = miami_base_url; } var base_href = base_url + "/web/ccr.php"; var post_ajax = function (com, data, callback_function) { var query_object = { com: com }; if (data) query_object.data = JSON.stringify(data); $.post(base_href, query_object, function (data) { try { data = JSON.parse(data); } catch (e) { var e_mes = "ERROR GET AJAX: " + data; console.log(e_mes); // alert(e_mes); if (callback_function) callback_function(null); return; } if (callback_function) callback_function(data); }) .fail(function (xhr, status, error) { var e_mes = "ERROR GET AJAX: " + error; console.log(e_mes); // alert(e_mes); if (callback_function) callback_function(null); }); }; function getAllUrlParams(i) { var r = i ? i.split("?")[1] : window.location.search.slice(1), t = {}; if (r) for (var e = (r = r.split("#")[0]).split("&"), o = 0; o < e.length; o++) { var l = e[o].split("="), s = void 0, a = l[0].replace(/\[\d*\]/, function (i) { return s = i.slice(1, -1), ""; }), n = void 0 === l[1] || l[1]; a = a.toLowerCase(), n = typeof (n) === "string" ? n.toLowerCase() : n, t[a] ? ("string" === typeof t[a] && (t[a] = [t[a]]), void 0 === s ? t[a].push(n) : t[a][s] = n) : t[a] = n; } return t; } var cc_callback = function (client_code) { var messenger_a = $(".messenger_link, .messenger_link_tilda_is_telegram, .messenger_link_tilda_whatsapp_dubai"); var is_android_compatibility = SW_BS.browser.android; messenger_a.each(function () { // debugger; var t_el = $(this); var mt = ""; var com = ""; var pdf_url = false; var target = null; if (t_el.hasClass('is_telegram_miami')) { is_miami = true; com = "link"; // com = "js_redir"; mt = 'telegram_miami'; target = 'telegram'; pdf_url = t_el.data('pdf-url') || false; } else if (t_el.hasClass('is_whatsapp_usa') || t_el.hasClass('is_whatsapp_miami')) { is_miami = true; t_el.hide();//TODO: Временное решение }else if (t_el.hasClass('is_whatsapp_london') || (window.projectInfo && t_el.hasClass('is_whatsapp') && window.projectInfo.isLondon)) { com = "link"; //com = "js_redir"; mt = 'whatsapp_london'; target = 'whatsapp'; } else if (t_el.hasClass('is_whatsapp_dubai') || t_el.hasClass('messenger_link_tilda_whatsapp_dubai') || (window.projectInfo && t_el.hasClass('is_whatsapp') && window.projectInfo.isDubai)) { com = "link"; //com = "js_redir"; mt = 'whatsapp_dubai'; target = 'whatsapp'; } else if (t_el.hasClass('is_whatsapp') || t_el.hasClass('is_whatsapp_turkey')) { com = "link"; //com = "js_redir"; mt = 'whatsapp'; target = 'whatsapp'; pdf_url = t_el.data('pdf-url') || false; } else if (t_el.hasClass('is_fb_messenger')) { com = "link"; mt = 'fb_messenger'; } else if (t_el.hasClass('is_telegram') || t_el.hasClass('is_telegram_dubai') || t_el.hasClass('is_telegram_london') || t_el.hasClass('is_telegram_turkey') || t_el.hasClass('messenger_link_tilda_is_telegram')) { com = "link"; // com = "js_redir"; mt = 'telegram'; target = 'telegram'; pdf_url = t_el.data('pdf-url') || false; } if (is_miami){ base_url = miami_base_url; base_href = base_url + "/web/ccr.php"; } t_el.attr('data-target', target); t_el.addClass(com); t_el.data('mt', mt); if (pdf_url) pdf_url = encodeURIComponent(pdf_url); if (!is_android_compatibility) { var href = base_href + "?com=" + com + "&client_code=" + client_code + "&mt=" + mt; if (pdf_url) href = href + "&pdf_url=" + pdf_url; if (t_el.hasClass('messenger_link_tilda_whatsapp_dubai') || t_el.hasClass('messenger_link_tilda_is_telegram')) { t_el.find('a').attr('href', href); } else { t_el.attr('href', href); } } else { var data = { 'client_code': client_code, 'mt': mt, }; if (pdf_url) data['pdf_url'] = pdf_url; post_ajax("get_link", data, function (data) { if (!data) { console.log("incorrect href: " + data); return; } t_el.attr('href', data); }); } }); var is_c_blur = false; var c_blur_time = 0; var is_already_focus = false; var link_is_showed = false; var blur_time = 0; var last_mt = ""; var w = null; $(window).focus(function () { // console.log('focus'); setTimeout(function () { if (link_is_showed) { if (!is_c_blur && !is_already_focus && Date.now() - blur_time > 1000) { is_already_focus = true; var p_name = ""; switch (last_mt) { case "telegram": p_name = "Telegram Messenger"; break; case "fb_messenger": p_name = "Facebook Messenger"; break; } alert('Если вы не можете открыть чат, то у вас вероятно не установлено приложение ' + p_name); } else { is_c_blur = false; is_already_focus = false; link_is_showed = false; blur_time = 0; last_mt = ""; } } }, 1000); }); $(window).blur(function () { blur_time = Date.now(); // console.log('blur'); }); var already_addEventListener = false; var is_postMessage_init = false; function after_load_callback() { setTimeout(function () { if (!is_c_blur) { if (w) w.close(); if (w && !w.closed) setTimeout(arguments.callee); } }, 10000); setTimeout(function () { if (w) w.postMessage("init", "*"); if (!is_postMessage_init) setTimeout(arguments.callee, 100); }, 100); } messenger_a.click(function (ev) { var t_el = $(this); if (typeof Ya !== 'undefined') { var metrika = Ya.Metrika || Ya.Metrika2 || null; var target = t_el.attr('data-target') || null; if (metrika && target && !t_el.hasClass('no-target')) { var counters = metrika.counters(); for (var i = 0; i < counters.length; i++) { var counter = counters[i]; var metrikaCounterID = counter.id; ym(metrikaCounterID, 'reachGoal', target); if (get_params.debug_counters) console.warn('Goal named', target, 'was sent to YM', metrikaCounterID, 'in', new Date().toLocaleString()); } } } var href = ''; if (t_el.hasClass('messenger_link_tilda_whatsapp_dubai') || t_el.hasClass('messenger_link_tilda_is_telegram')) { href = t_el.find('a').attr('href'); } else { href = t_el.attr('href'); } if (!t_el.hasClass('js_redir')) { window.open(href); return false; } ev.preventDefault(); is_postMessage_init = false; c_blur_time = 0; last_mt = t_el.data('mt'); is_already_focus = false; link_is_showed = true; w = window.open(href); try { w.addEventListener('load', after_load_callback, false); if (w.document.readyState === 'complete') page_load_callback(); else if (SW_BS.browser.firefox) after_load_callback(); } catch (e) { after_load_callback(); } if (!already_addEventListener) { already_addEventListener = true; addEventListener('message', function (ev) { console.log(ev.data); switch (ev.data) { case "mb_init": is_postMessage_init = true; break; case "mb_close": w.close(); break; case "mb_blur": is_c_blur = true; c_blur_time = Date.now(); break; case "mb_beforeunload": if (Date.now() - c_blur_time < 1000) is_c_blur = false; break; } }, false); } return false; }); }; function get_cookie(key) { // debugger; var items = document.cookie.split('; '); for (var i = 0; i < items.length; i++) { var item_arr = items[i].split('='); if (item_arr[0] === key) return item_arr[1]; } return false; } var get_params = getAllUrlParams(); if (typeof get_params === "object") delete get_params.roistat; var client_code = $.cookie('client_code'); if (client_code) { cc_callback(client_code); return; } var max_retry = 20; var cur_retry = 0; var counters_on = {}; // Проверяем наличие подключённых скриптов аналитики на странице $("script").each(function () { var html = $(this).html(); var src = $(this).attr('src'); if (html.indexOf('/metrika/tag.js') !== -1) // Ya metric counter counters_on.ym = true; else if ((src && src.indexOf('/gtag/') !== -1) || html.indexOf('gtag(') !== -1) { // Google Analytics counters_on.ga = true; } else if (html.indexOf('roistat.com') !== -1) { // Roistat counters_on.roi = true; } }); if (get_params.debug_counters) console.warn('Enabled analytic counters: ', counters_on); // Проставляем ссылки на мессенджеры перед инициализацией счётчиков post_ajax("get_client_code", { 'domain': window.location.hostname + window.location.pathname, 'data': get_params }, function (data) { if (!data || !parseInt(data)) { console.log("incorrect client_code for messenger links: " + data); return; } if (get_params.debug_counters) console.warn('Received client code for links: ', data); cc_callback(data); }); var roistat_visit = null; window.onRoistatAllModulesLoaded = function () { window.roistat.registerOnVisitProcessedCallback(function () { roistat_visit = window.roistat.getVisit(); console.log("Установлен номер визита: " + roistat_visit); }); }; // Делаем несколько попыток найти нужные нам скрипты счётчиков в js коде setTimeout(function () { if (roistat_visit) get_params.lidroistat_visit = roistat_visit; try { get_params.yandex_metrika_id = Ya.Metrika2.counters()[0].id; } catch (e) { } if (!get_params.yandex_metrika_id) try { get_params.yandex_metrika_id = Ya.Metrika.counters()[0].id; } catch (e) { } try { get_params._ym_uid = get_cookie('_ym_uid'); } catch (e) { } try { get_params.google_analytics_id = ga.getAll()[0].get('trackingId'); } catch (e) { } try { get_params.ga_cookie = get_cookie('_ga'); } catch (e) { } try { get_params.client_UA = navigator.userAgent; } catch (e) { } var not_working_counters = {}; not_working_counters.ym = (counters_on.ym && typeof get_params.yandex_metrika_id === "undefined"); if (counters_on.ym) not_working_counters.ym = typeof get_params.yandex_metrika_id === "undefined"; if (counters_on.ga) not_working_counters.ga = typeof get_params.google_analytics_id === "undefined"; if (counters_on.roi) not_working_counters.roi = (typeof window.roistat === "undefined" || !parseInt(get_params.lidroistat_visit)); if (get_params.debug_counters) { console.warn('Trying to run not working counters: ', not_working_counters, '[true] means that counter isn\'t working.'); } // Проверка счётчиков, если не подключены, пытаемся снова несколько раз if ((not_working_counters.roi || not_working_counters.ga || not_working_counters.ym) && cur_retry < max_retry) { cur_retry++; console.log('client_code counter retry ' + cur_retry); setTimeout(arguments.callee, 500); return false; } if (get_params.debug_counters) console.warn('Sent goal params: ', get_params); function get_client_code() { post_ajax("get_client_code", { 'domain': window.location.hostname + window.location.pathname, 'data': get_params }, function (data) { if (!data || !parseInt(data)) { console.log("incorrect client_code: " + data); return; } if (get_params.debug_counters) console.warn('Received final client code: ', data); var expDate = new Date(); expDate.setTime(expDate.getTime() + (30 * 24 * 60 * 60 * 1000)); $.cookie('client_code', data, {path: '*', expires: expDate}); }); } if (get_params.debug_counters) window.ww_get_client_code = get_client_code; get_client_code(); setInterval(function () { if (!$.cookie('client_code')) { get_client_code(); } }, 60 * 60 * 1000) }, 0); });