(function(undefined) { var rdy = false; var settings = { multiple: 0, param: '', events: [{"id":4,"event_type":1,"button_text":"\u041e\u0442\u043f\u0440\u0430\u0432\u0438\u0442\u044c","params":[{"pixel_event_id":4,"name":"country","selector":null}]},{"id":23,"event_type":2,"page_url_mask":"order","params":[{"pixel_event_id":23,"name":"value","selector":"fb_value"},{"pixel_event_id":23,"name":"currency","selector":null},{"pixel_event_id":23,"name":"ph","selector":"fb_phone"},{"pixel_event_id":23,"name":"country","selector":null},{"pixel_event_id":23,"name":"order_id","selector":"fb_lead_id"}]},{"id":190,"event_type":2,"page_url_mask":"products","params":[{"pixel_event_id":190,"name":"content_ids","selector":"product_ids"},{"pixel_event_id":190,"name":"content_type","selector":null},{"pixel_event_id":190,"name":"country","selector":null}]},{"id":211,"event_type":1,"button_text":"\u041e\u0444\u043e\u0440\u043c\u0438\u0442\u044c \u0437\u0430\u043a\u0430\u0437","page_url_mask":"order","params":[{"pixel_event_id":211,"name":"country","selector":null}]},{"id":212,"event_type":1,"button_text":"\u0417\u0430\u043a\u0430\u0437\u0430\u0442\u044c","params":[{"pixel_event_id":212,"name":"country","selector":null}]},{"id":213,"event_type":1,"button_text":"\u0417\u0430\u043a\u0430\u0437\u0430\u0442\u044c \u0447\u0435\u0440\u0435\u0437 Telegram","params":[{"pixel_event_id":213,"name":"country","selector":null}]}] }; var fbc = getCookieData('_fbc'); var qP = parseUrlParams(); if (fbc && qP['fbclid'] && fbc.indexOf(qP['fbclid']) == -1) { createFBC(qP['fbclid']); } else if (!fbc && qP['fbclid']) { createFBC(qP['fbclid']); } if (document.getElementsByTagName('body').length) rfn(); document.addEventListener("DOMContentLoaded", rfn,false); function rfn() { if (rdy) return; rdy = true if (settings.events && settings.events.length) { for (var i = 0; i < settings.events.length; i++) { addEventListener(settings.events[i]) } } } function closest(target, selector) { while (target) { if (target.matches && target.matches(selector)) return target; target = target.parentNode; } return null; } function addEventListener(eventObject) { if (eventObject.event_type === 1) { document.addEventListener("click", function(event) { if (eventObject.button_class) { if (!closest(event.target, '.' + eventObject.button_class)) return false; if (!eventObject.button_text) dispatchEvent(eventObject, prepareData(eventObject, event.target)); } if (event.target.innerText == eventObject.button_text) { dispatchEvent(eventObject, prepareData(eventObject, event.target)); } else { var children = event.target.childNodes; for (var i = 0; i < children.length; i++) { if (children[i].nodeType == Node.TEXT_NODE && children[i].textContent.trim() == eventObject.button_text) { dispatchEvent(eventObject, prepareData(eventObject, event.target)); } } } }, true); } else if (eventObject.event_type === 2) { if ( !eventObject.page_url_mask || eventObject.page_url_mask == "" || eventObject.page_url_mask == "*" || ( eventObject.page_url_mask && (new RegExp('.*' + eventObject.page_url_mask.replace(/([\\\.\+\?\[\^\]\$\(\)\{\}\=\!\<\>\|\:])/g, "\\$1").replace(/\*/g, ".*") + '.*', 'g')).test(document.location.href) ) ) { dispatchEvent(eventObject, prepareData(eventObject, document.body)); } } } function dispatchEvent(eventObject, data) { console.log(data); var d = JSON.stringify(data); obj = new Image(); obj.src = 'https://pixie.xcoder.pro/api/dispatch/1' + '/event/' + eventObject.id + ('?t=' + (new Date).getTime()) + (data && d != '{}' ? '&d=' + btoa(encodeURIComponent(d)) : '') } var pixie_new_event_id_timer = null; function pixie_new_event_id() { var el = document.getElementById('pixie_event_id'); if (el) { var event_id = el.value; if (event_id) { var parts = el.value.split('-'); if (pixie_new_event_id_timer) { clearTimeout(pixie_new_event_id_timer); } pixie_new_event_id_timer = setTimeout(function () { el.value = parts[0] + '-' + (new Date()).getTime() }, 2000); return event_id; } } } function prepareData(eventObject, el) { var maxParents = 50; var data = { url: document.location + '' }; if (settings.multiple) { var ids = getPixelIds(); if (ids.length) { data.ids = ids; } } var fbp = getCookieData('_fbp'); var fbc = getCookieData('_fbc'); if (fbp) { data.fbp = fbp; } else { data.fbp = createFBP(); } var event_id = pixie_new_event_id(); if (event_id) { data.event_id = event_id; } var qP = parseUrlParams(); if (fbc && qP['fbclid'] && fbc.indexOf(qP['fbclid']) == -1) { data.fbc = createFBC(qP['fbclid']); } else if (fbc) { data.fbc = fbc; } else if (qP['fbclid']) { data.fbc = createFBC(qP['fbclid']); } // fix if (data.fbc && data.fbc.indexOf('fc.') > -1) { data.fbc = data.fbc.replace('fc.', 'fb.'); } if (eventObject.params && eventObject.params.length) { while(el.nodeName != "HTML" && maxParents > 0) { for (var i = 0; i < eventObject.params.length; i++) { if (!data[eventObject.params[i]['name']]) { var e = el.querySelector('[name=' + eventObject.params[i]['selector'] + ']'); if (e && e.value) { data[eventObject.params[i]['name']] = e.value; } else { e = el.querySelector('.' + eventObject.params[i]['selector']); if (e && e.value) { data[eventObject.params[i]['name']] = e.value; } } } } el = el.parentNode; maxParents--; } for (var i = 0; i < eventObject.params.length; i++) { if (!data[eventObject.params[i]['name']]) { var e = document.getElementById(eventObject.params[i]['selector']); if (e) { data[eventObject.params[i]['name']] = e.value; } } } } return data; } function getCookieData(name) { var pairs = document.cookie.split("; "), count = pairs.length, parts; while (count--) { parts = pairs[count].split("="); if (parts[0] === name) return parts[1]; } return false; } function createFBP() { var fbp = 'fb.' + (document.location.host.split('.').length-1) + "." + (new Date).getTime() + '.' + (parseInt(Math.random()*1000000000)); document.cookie = "_fbp=" + fbp + '; domain=.' + document.location.host + '; path=/; expires=' + (new Date(Date.now() + 90 * 24 * 3600 * 1000)); return fbp; } function createFBC(fbclid) { var fbc = 'fb.' + (document.location.host.split('.').length-1) + "." + (new Date).getTime() + '.' + fbclid; document.cookie = "_fbc=" + fbc + '; domain=.' + document.location.host + '; path=/; expires=' + (new Date(Date.now() + 90 * 24 * 3600 * 1000)); return fbc; } function getPixelIds() { var ids = []; var queryParams = parseUrlParams(); if (settings.param && queryParams && queryParams[settings.param]) { return queryParams[settings.param].split(',').map(function(el) { return parseInt(el); }).filter(function(el) { return el > 0; }); } else if (window.fbq && window.fbq.queue) { for (var i = 0; i < window.fbq.queue.length; i++) { if (window.fbq.queue[i][0] && window.fbq.queue[i][0] && window.fbq.queue[i][0] == 'init') { ids.push(window.fbq.queue[i][1]); } } } return ids; } function parseUrlParams() { var parts = document.location.href.split('?'); if (!parts[1]) return {}; var query = parts[1].split('#')[0] var vars = query.split("&"); var queryString = {}; for (var i = 0; i < vars.length; i++) { var pair = vars[i].split("="); var key = decodeURIComponent(pair[0]); var value = decodeURIComponent(pair[1]); if (typeof queryString[key] === "undefined") { queryString[key] = decodeURIComponent(value); } else if (typeof queryString[key] === "string") { var arr = [queryString[key], decodeURIComponent(value)]; queryString[key] = arr; } else { queryString[key].push(decodeURIComponent(value)); } } return queryString; } })();