var HOST = 'https://track.jackmedia.io'; var eventURI = '/event/capture/' var mappingId; var variant = 'base'; var startSession; var sessionId; var urlP = new URLSearchParams(window.location.search); function configureMapping(mId, v) { mappingId = mId; var preExistingSession = urlP.get('sid'); if (preExistingSession) sessionId = preExistingSession; else sessionId = newSessionId(8); startSession = new Date().getTime(); variant = v?v:'base'; } function getEventSessionId() { return sessionId; } const getCookieValue = (name) => ( document.cookie.match('(^|;)\\s*' + name + '\\s*=\\s*([^;]+)')?.pop() || '' ) //looks at cookies value and make best guest as what the source of traffic is. function isTikTokTraffic() { var ttClickId, ttp; try { ttClickId = getCookieValue('ttclid'); if (ttClickId === null || ttClickId === undefined || ttClickId === '') ttClickId = urlP.get('ttclid'); if (ttClickId) return true; // tik tok param is used to match website visitor events with TikTok ads ttp = getCookieValue('_ttp'); if (ttp) return true; } catch (ex) { return false; } return false; } function postEvent(eventName, payload) { if (!mappingId) { console.log('Please run configureMapping with the relevant mapping id !'); return 'error'; } var fbp, fbc, fbClickId, adId, ttClickId, ttp; try { fbc = getCookieValue('_fbc'); fbp = getCookieValue('_fbp'); fbClickId = urlP.get('fbclid'); ttClickId = getCookieValue('ttclid'); if (ttClickId === null || ttClickId === undefined || ttClickId === '') ttClickId = urlP.get('ttclid'); // tik tok param is used to match website visitor events with TikTok ads ttp = getCookieValue('_ttp'); adId = urlP.get('adId'); } catch (ex) { } if (!payload) payload = {} payload.sid = sessionId; payload.start = startSession; //default to base variant if not provided if (!payload.variant) payload.variant = variant; if (fbc && fbc.length > 1) payload.fbc = fbc; if (fbClickId && fbClickId.length > 1) payload.fbClickId = fbClickId; if (ttClickId) payload.ttClickId = ttClickId; if (ttp) payload.ttp = ttp; if (fbp && fbp.length > 1) payload.fbp = fbp; if (adId && adId.length > 1) payload.adId = adId; payload.url = window.location.href; var post_url = HOST + eventURI + mappingId + '/' + eventName; if (encodeURI) post_url = encodeURI(post_url); $.ajax({ url: post_url, type: 'POST', data: JSON.stringify(payload), contentType: 'application/json; charset=utf-8', dataType: 'json', success: function (response) { return (response && response.id) ? response.id : 'error';; }, error: function (xhr) { return 'error'; } }); }; //post to our new D3 API function postToD3Distribution(campaignId, affiliateId, payload) { var post_url = `${HOST}/distributions/push/${campaignId}/${affiliateId}`; //console.log('post url', post_url); //console.log('payload', payload); //auto link the lead to the mapping; if (payload && mappingId) payload.mappingId = mappingId; if (payload && variant) payload.variant = variant; if (payload && sessionId) payload.sid = sessionId; $.ajax({ url: post_url, type: 'POST', data: JSON.stringify(payload), contentType: 'application/json; charset=utf-8', dataType: 'json', success: function (response) { var resp = (response && response.status) ? response.status.toLowerCase() : ''; if (resp == 'success' || resp == 'waitinginbound' ) { //if available - override thank you message with custom/dynamic message from server side. if (response.tyMessage && response.tyMessage.length > 5 && $('#thankyoumessage').length) $("#thankyoumessage").html(response.tyMessage); $("#submitting").addClass('d-none'); $("#thankyoumessage").removeClass('d-none'); setTimeout(function () { payload.leadId = response.id; postEvent('step-lead', payload ); }, 10); } else { $("#submitting").addClass('d-none'); $("#thankyoumessage").removeClass('d-none'); setTimeout(function () { //fire facebook custom lead events postEvent('rejection', { fatal: false, description : 'rejectedbyDash'}); }, 10); } }, error: function (xhr, exception) { $("#submitting").addClass('d-none'); $("#thankyoumessage").removeClass('d-none'); setTimeout(function () { if (xhr.status === 400 ) //400 invalid and 409 duplicate. postEvent('invalid', { fatal: false, status: xhr.status, description : 'Rejected as invalid lead'}); else if (xhr.status === 409) postEvent('duplicate', { fatal: false, status: xhr.status, description : 'Duplicate lead'}); else postEvent('exception', { fatal: true, status: xhr.status, exception: exception, response: xhr.responseText}); }, 10); } }); }; function postToD3(campaignId, affiliateId, payload) { var post_url = `${HOST}/leads/create/${campaignId}/${affiliateId}`; //console.log('post url', post_url); //console.log('payload', payload); //auto link the lead to the mapping; if (payload && mappingId) payload.mappingId = mappingId; if (payload && variant) payload.variant = variant; if (payload && sessionId) payload.sid = sessionId; $.ajax({ url: post_url, type: 'POST', data: JSON.stringify(payload), contentType: 'application/json; charset=utf-8', dataType: 'json', success: function (response) { var resp = (response && response.status) ? response.status.toLowerCase() : ''; if (resp == 'success' || resp == 'waitinginbound' ) { $("#submitting").addClass('d-none'); $("#thankyoumessage").removeClass('d-none'); setTimeout(function () { payload.leadId = response.id; postEvent('step-lead', payload ); }, 10); } else { $("#submitting").addClass('d-none'); $("#thankyoumessage").removeClass('d-none'); setTimeout(function () { //fire facebook custom lead events postEvent('rejection', { fatal: false, description : 'rejectedbyDash'}); }, 10); } }, error: function (xhr, exception) { $("#submitting").addClass('d-none'); $("#thankyoumessage").removeClass('d-none'); setTimeout(function () { if (xhr.status === 400 ) //400 invalid and 409 duplicate. postEvent('invalid', { fatal: false, status: xhr.status, description : 'Rejected as invalid lead'}); else if (xhr.status === 409) postEvent('duplicate', { fatal: false, status: xhr.status, description : 'Duplicate lead'}); else postEvent('exception', { fatal: true, status: xhr.status, exception: exception, response: xhr.responseText}); }, 10); } }); }; function newSessionId(length) { var result = []; var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; var charactersLength = characters.length; for ( var i = 0; i < length; i++ ) { result.push(characters.charAt(Math.floor(Math.random() * charactersLength))); } return result.join(''); }