本JavaScriptをコピーして、必要に応じ変更しお使いください。
行動分析URLとビーコンkeyは別途ご連絡いたします。
⚠️
動作を保証するものではございませんので、参考程度にご利用ください。
javascript
(function() { /**** 編集箇所 ****/ // 行動分析URL var URL = "<ドメイン>/beacon"; // 例:var URL ="example.com/beacon"; // クッキー名 var cookiename = "c"; // ビーコンAPI用key 管理画面>その他の設定画面で確認 var tracking_key = "・・・"; /****************/ // ビーコン送信時のタイムアウト var beacon_timeout = 5000; function isLocalStorageAvailable() { if (!('localStorage' in window)) return false; try { localStorage.test = ''; return true; } catch (e) { return false; } } function getCookie(key) { if (!key) return; var cookies = document.cookie.split(/; */); for (var i = 0; i < cookies.length; i++) { var entry = cookies[i]; var keyValue = entry.split('=', 2); var k = keyValue[0]; if (k != key) continue; return decodeURIComponent(keyValue[1]); } return null; } function setCookie(key, value, options) { if (!options) options = {}; var values = []; if (value === null || value === undefined) { value = ''; options.expires = -1; } values.push(key + "=" + encodeURIComponent(value)); // expires var expires = new Date(); if (typeof options.expires == 'number') { expires.setTime(expires.getTime() + options.expires * 24 * 60 * 60 * 1000); } else if (options.expires && options.expires.toUTCString) { expires = options.expires; } else { expires.setTime(expires.getTime() + 2 * 365 * 24 * 60 * 60 * 1000); } values.push("; expires=" + expires.toUTCString()); // path if (options.path) { values.push("; path=" + options.path); } // domain if (options.domain) { values.push("; domain=" + options.domain); } document.cookie = values.join(''); } function jsonp(options) { var head = document.head || document.getElementsByTagName("head")[0] || document.documentElement; var script = document.createElement("script"); script.async = ((options.async === undefined || options.async) ? "async" : "defer"); script.src = options.url + "?" + createQueryString(options.data); var tid = null; script.onload = script.onreadystatechange = function( _, isAbort ) { if (tid) { clearTimeout(tid); tid = null; } if ( isAbort || !script.readyState || /loaded|complete/.test( script.readyState ) ) { script.onload = script.onreadystatechange = script.onerror = null; if ( head && script.parentNode ) { head.removeChild( script ); } script = undefined; if ( options.success && !isAbort ) { options.success.call(this); } if (options.complete) options.complete.call(this); } }; script.onerror = function () { if (tid) { clearTimeout(tid); tid = null; } script.onload = script.onreadystatechange = script.onerror = null; if ( head && script.parentNode ) { head.removeChild( script ); } script = undefined; if (options.error) options.error.call(this); if (options.complete) options.complete.call(this); }; head.insertBefore( script, head.firstChild ); if (options.timeout) { tid = setTimeout(function() { if (!script) return; script.onload = script.onreadystatechange = script.onerror = null; if ( head && script.parentNode ) { head.removeChild( script ); } script = undefined; if (options.error) options.error.call(this); if (options.complete) options.complete.call(this); }, options.timeout); } } function createQueryString(param) { if (!param) return ""; if (typeof param == "string") { return param; } var res = []; Object.keys(param).forEach(function (k) { var isValueNull = param[k] === null || param[k] === undefined; var value = isValueNull ? "" : encodeURIComponent(param[k]); res.push(encodeURIComponent(k) + "=" + value); }); return res.join("&"); } function as_local_storage_c() { if (!('localStorage' in window)) return; if (!isLocalStorageAvailable()) return false; var c = localStorage.getItem(cookiename); if (c) return c; c = getCookie(cookiename); if (!c) c = (parseInt(Math.random()*100000000000, 10)).toString(); localStorage.setItem(cookiename, c); return c; } function getCParameter() { var cookie = as_local_storage_c() || getCookie(cookiename); if (cookie == null) { var expire = new Date(); expire.setTime(expire.getTime() + (1000 * 60 * 60 * 24 * 365 * 2)); c = (parseInt(Math.random()*100000000000, 10)).toString(); setCookie(cookiename, c, {path: '/', expires: expire}); cookie = c } return cookie; } window.as_beacon_search_default = function(q, item_ids){ var cookie = getCParameter(); var work_id = "1"; var params = {}; params["key"] = tracking_key; params["work_id"] = work_id; params["c"] = cookie; params["q"] = q; params["item_ids"] = item_ids; as_send_beacon_async(params); } // 詳細遷移リンククリック時 window.as_beacon_detail = function(item_id) { var e = (window.event)? window.event : arguments.callee.caller.arguments[0]; var self = e.target || e.srcElement; var url = getUrl(self); var work_id = "7"; var cookie = getCParameter(); var params = {}; params["key"] = tracking_key; params["work_id"] = work_id; params["c"] = cookie; params["item_id"] = item_id; var open = (e.metaKey || e.shiftKey || e.ctrlKey); if(open){ as_send_beacon_async(params, url); } else { as_send_beacon_click(params, url); return false; } } window.as_beacon_cart = function(item_id) { var cookie = getCParameter(); var work_id = "9"; var params = {}; params["key"] = tracking_key; params["work_id"] = work_id; params["c"] = cookie; params["item_id"] = item_id; as_send_beacon_async(params); } window.as_beacon_comp = function(item_ids, counts, prices) { var cookie = getCParameter(); var work_id = "10"; var stritem_ids = ""; var strcounts = ""; var strprices = ""; for ( var index = 0; index < item_ids.length; index++) { stritem_ids = stritem_ids + item_ids[index] + ","; strcounts = strcounts + counts[index] + ","; strprices = strprices + prices[index] + ","; } stritem_ids = stritem_ids.substring(0, stritem_ids.length - 1); strcounts = strcounts.substring(0, strcounts.length - 1); strprices = strprices.substring(0, strprices.length - 1); var params = {}; params["key"] = tracking_key; params["work_id"] = work_id; params["c"] = cookie; params["item_ids"] = stritem_ids; params["counts"] = strcounts; params["prices"] = strprices; as_send_beacon_async(params); } // 検索結果画面にあるカートボタンをクリック時 window.as_beacon_detail_and_cart = function(item_id, price) { var e = (window.event)? window.event : arguments.callee.caller.arguments[0]; var self = e.target || e.srcElement; var url = getUrl(self); if(!url) return; var cookie = getCParameter(); // 商品詳細 var first_params = {}; first_params["key"] = tracking_key; first_params["work_id"] = 7; first_params["c"] = cookie; first_params["item_id"] = item_id; // 購入完了 var second_params = {}; second_params["key"] = tracking_key; second_params["work_id"] = 9; second_params["c"] = cookie; second_params["item_id"] = item_id; var open = (e.metaKey || e.shiftKey || e.ctrlKey); if(open){ as_send_beacon_double(first_params, second_params, url); } else { as_send_beacon_double_transition(first_params, second_params, url); return false; } } // オートリレーション window.as_beacon_by_any_chance = function(q, item_id, id, url) { var e = (window.event)? window.event : arguments.callee.caller.arguments[0]; var self = e.target || e.srcElement; var url = getUrl(self); var cookie = getCParameter(); var params = {}; params["key"] = tracking_key; params["work_id"] = "1"; params["c"] = cookie; params["q"] = q; params["item_ids"] = item_id; as_send_beacon_sync(params); var params = {}; params["key"] = tracking_key; params["work_id"] = "12"; params["c"] = cookie; params["q"] = q; params["item_id"] = item_id; params["by_any_chance_item_id"] = id; as_send_beacon_sync(params); var params = {}; params["key"] = tracking_key; params["work_id"] = "7"; params["c"] = cookie; params["q"] = q; params["item_id"] = item_id; as_send_beacon_click(params, url); } window.as_send_beacon_async = function(params) { if (getCookie("ignore_as_cookie")) { return; } var protocol = location.protocol; if (protocol == undefined) { protocol = "https:"; } jsonp({ url: protocol + "//" + URL, data: params, async : true }); } window.as_send_beacon_sync = function(params) { if (getCookie("ignore_as_cookie")) { return; } var protocol = location.protocol; if (protocol == undefined) { protocol = "https:"; } jsonp({ url: protocol + "//" + URL, data: params, async : false }); } window.as_send_beacon_click = function(params, url) { if (getCookie("ignore_as_cookie")) { return; } var protocol = location.protocol; if (protocol == undefined) { protocol = "https:"; } jsonp({ url: protocol + "//" + URL, timeout: beacon_timeout, data: params, complete: function() { location.href = url; } }); } window.as_send_beacon_double = function(first_params, second_params) { if (getCookie("ignore_as_cookie")) { return; } var protocol = location.protocol; if (protocol == undefined) { protocol = "https:"; } jsonp({ url: protocol + "//" + URL, data: first_params, success: function() { setTimeout(function() { jsonp({ url: protocol + "//" + URL, data: second_params, }) }); } }); } window.as_send_beacon_double_transition = function(first_params, second_params, url) { if (getCookie("ignore_as_cookie")) { return; } var protocol = location.protocol; if (protocol == undefined) { protocol = "https:"; } jsonp({ url: protocol + "//" + URL, timeout: beacon_timeout, data: first_params, success: function() { jsonp({ url: protocol + "//" + URL, timeout: beacon_timeout, data: second_params, complete: function() { location.href = url; } }); }, error: function() { location.href = url; } }); } function getUrl(elem) { if(!elem) return null; if(elem.tagName == "a" || elem.tagName == "A") { return elem.href; } else { return getUrl(elem.parentElement); } } }());
Share