// define adlinker function that will be called and defined as a global variable function adlinker_client_function() { let config = {}; config.window_url = new URL(window.location.href); //default config config.enable_url_param = false; //config.enable_url_param = true; config.stage = 'prod'; config.initialized = false; config.script_src = new URL(document.currentScript.src); config.page_key = 'al_pg_id'; config.cookie_key = 'al_br_id'; if(typeof Shopify != 'undefined'){config.platform = 'shopify'} // if(typeof woocommerce_params == 'undefined'){console.log('is woocommerce')} let data = {}; data.ua = navigator.userAgent; data.requested_url = config.window_url.href; if (!document.referrer.includes(config.window_url.hostname)) { data.referrer_url = document.referrer }; if (get_cookie(config.cookie_key)) { data.cookie = get_cookie(config.cookie_key) }; function configure(params = {}) { if (params.enable_url_param != undefined) { config.enable_url_param = params.enable_url_param; } if (params.stage != undefined) { config.stage = params.stage; } } // starting adlinker() function initialize() { fetch('https://api.ipify.org?format=json') .then(response => response.json()) .then(function (json) { data.ip = json.ip; //url.href = config.script_src; //config.origin = url.origin; if (typeof Shopify != 'undefined') { config.platform = 'shopify' } if (typeof woocommerce_params != 'undefined') { config.platform = 'woocommerce' } if (config.enable_url_param) { persist_url_params() }; if (config.platform == 'shopify') { shopify() }; if (config.platform == 'woocommerce') { woocommerce() }; if (window.location.href.includes('ketochow.xyz') || window.location.href.includes('adlinks.io')) { capture_emails() }; pageLoadTime = capture_page_load_time(); if (typeof pageLoadTime != 'undefined') { data.pageLoadTime = pageLoadTime } //console.log(config.platform); if (typeof config.platform == 'undefined') { console.log('no adlinks platform type detected') send_data({ event: { type: 'pageview' } }); }; config.initialized = true; // now initialized //send_data({event:{type:'pageview'}}); }); } function set_params() { // set params on current url var if (get_cookie(config.cookie_key)) { config.window_url.href = window.location.href; data.cookie = get_cookie(config.cookie_key); config.window_url.searchParams.set(config.page_key, data.cookie); // set browser id in page window.history.replaceState(null, null, config.window_url.href); // reset state } } // this decorates window.location (current url) function persist_url_params() { window.addEventListener('load', function () { set_params(); // call the function just defined to set params document.addEventListener('change', function () { // and call the function again if the DOM changes at all config.window_url.href = window.location.href; // set url var to current href if (!config.window_url.searchParams.has(config.page_key)) { //only reset url if page id not found in url set_params(); // set params again } }); }); } //utility function to grab a cookie value function get_cookie(cookie_name) { let regex_pattern = `(?:\\b|^|;)${cookie_name}=([^;]*)`; // let cart_regex = "\ " + cookie + "=[^;]*(?=;|$)"; //this regex matches specific cookie in cookie string //console.log(regex_pattern) let regex = new RegExp(regex_pattern) //console.log(regex) let match = document.cookie.match(regex) return (match ? match[1] : null); // if there's a match, grab the second one, else return null } function capture_page_load_time() { var performance = window.performance || window.mozPerformance || window.msPerformance || window.webkitPerformance || {}; var timing = performance.timing || {}; if (timing.loadEventEnd && timing.navigationStart) { var loadTime = timing.loadEventEnd - timing.navigationStart; // console.log('Page load time is ' + loadTime + ' milliseconds.'); return loadTime; } else { // console.log('Performance timing not supported.'); return undefined; } } function capture_emails() { document.addEventListener('focusout', function (event) { if (event.target.type === 'email' && isValidEmail(event.target.value)) { //console.log('Email inputted:', event.target.value); // send event "emailCaptured" data.email = event.target.value; send_data({ event: { type: 'emailcaptured' } }); } }); } function isValidEmail(email) { var pattern = /^[^@\s]+@[^@\s]+\.[^@\s]+$/; return pattern.test(email); } function shopify() { console.log('adlinks for shopify triggered'); function send_product_pageview(product_data) { send_data({ event: { type: 'pageview', product: { id: product_data.id, name: product_data.title, category: product_data.product_type, price: product_data.variants[0].price, currency: Shopify.currency.active } } }); } if (typeof meta != 'undefined') { if (meta.page.pageType == 'product') { fetch(config.window_url.origin + config.window_url.pathname + '.json') .then(response => response.json()) .then(data => send_product_pageview(data.product)) .catch(error => console.error(error)); } else { send_data({ event: { type: 'pageview' } }); } } else { send_data({ event: { type: 'pageview' } }); } let cart = get_cookie('cart'); if (cart) { data.shopify_cart = cart }; window.addEventListener('click', function () { var cartFinder = setInterval(function () { if (cart != get_cookie('cart')) { cart = get_cookie('cart'); if (cart) { data.shopify_cart = cart; send_data(); } //console.log(data.shopify_cart); //doing this until further notice clearInterval(cartFinder); } }, 100); setTimeout(() => clearInterval(cartFinder), 2000); }); if (Shopify.checkout) { data.shopify_purchase_id = Shopify.checkout.order_id.toString(); // if(Shopify.checkout.email){data.email = Shopify.checkout.email}; // if(Shopify.checkout.phone){data.phone = Shopify.checkout.phone}; // data.shopify_customer_id = Shopify.checkout.customer_id.toString(); } } //function specific to woocommerce function woocommerce() { console.log('adlinks for woocommerce triggered'); send_data({ event: { type: 'pageview' } }); if (config.window_url.href.includes('checkout')) { jQuery(document).ready(() => { first_name = jQuery('#billing_first_name'); first_name.change(() => { data.first_name = first_name.val(); send_data(); }) last_name = jQuery('#billing_last_name'); last_name.change(() => { data.last_name = last_name.val(); send_data(); }) billing_country = jQuery('#billing_country'); billing_country.ready(() => { data.country = billing_country.val(); send_data(); }) billing_country.change(() => { data.country = billing_country.val(); send_data(); }) city = jQuery('#billing_city'); city.change(() => { data.city = city.val(); send_data(); }) state = jQuery('#billing_state') state.ready(() => { data.state = state.val(); send_data(); }) state.change(() => { data.state = state.val(); send_data(); }) postcode = jQuery('#billing_postcode') postcode.change(() => { data.postcode = postcode.val(); send_data(); }) phone = jQuery('#billing_phone') phone.change(() => { data.phone = phone.val(); send_data(); }) email = jQuery('#billing_email') email.change(() => { data.email = email.val(); send_data(); }) }) } } // log function for debugging function log_vars() { // recheck until initialized is true let inter = setInterval(function () { if (config.initialized) { // once initialized, clear the interval clearInterval(inter); // log vars console.log('adlinker data:'); console.log(data); console.log('adlinker config:'); console.log(config); } }, 100); } // send event function // event argument should contain event data as needed - see schema above // user can contain user data following schema above, but not necessary function send_data(args = {}) { if (args.event === undefined) { args.event = { type: 'microdata' } } // if (args.email !== undefined){data.email = args.email} if (args.data !== undefined) { if (args.data.email !== undefined) { data.email = args.data.email } if (args.data.phone !== undefined) { data.phone = args.data.phone } if (args.data.first_name !== undefined) { data.first_name = args.data.first_name } if (args.data.last_name !== undefined) { data.last_name = args.data.last_name } if (args.data.city !== undefined) { data.city = args.data.city } if (args.data.state !== undefined) { data.state = args.data.state } if (args.data.postcode !== undefined) { data.postcode = args.data.postcode } if (args.data.country !== undefined) { data.country = args.data.country } } let endpoint = [config.script_src.origin, config.stage, 'js-api'].join('/'); var xhr = new XMLHttpRequest(); xhr.open("POST", endpoint); xhr.withCredentials = true; //xhr.setRequestHeader('content-type','application/json'); xhr.onreadystatechange = function () { if (this.readyState === XMLHttpRequest.DONE && this.status === 200) { if (config.enable_url_param) { set_params(); } } } function send() { let payload = {}; payload.data = data; payload.event = args.event; xhr.send(JSON.stringify(payload)); } if (!config.initialized) { let inter = setInterval(function () { if (config.initialized) { clearInterval(inter); send(); } }, 100); } else { send(); } } return { config: configure, init: initialize, send: send_data, log: log_vars, cookie: get_cookie } } //if(typeof adlinker == 'undefined'){ var adlinker = adlinker_client_function(); //} window.addEventListener('load', function(){ if (typeof adlinker_config != 'undefined') { adlinker.config(adlinker_config); adlinker.init(); } else { adlinker.init(); } })