window.na = window.na || {};
window.na = {
config(naid, params = {}) {
(function() {
var first_child = document.head.firstElementChild;
var init_script = document.createElement('script');
init_script.src = 'https://analytics.datanugget.io/init?id=' + naid;
document.head.insertBefore(init_script, first_child);
})();
var vid = window.localStorage.getItem('na_vid');
var lvsid = window.localStorage.getItem('na_lvsid');
var vsid = window.sessionStorage.getItem('na_vsid');
var source = null;
var medium = null;
var campaign = null;
var session_switch = false;
window.addEventListener('beforeunload', () => {
window.mineNugget('disconnect', '', {});
});
if (!vid) {
window.localStorage.setItem('na_vid', 'fac41b57-d1ba-4563-a376-cf3dffcac4ed');
}
if (!vid || !vsid) {
window.localStorage.setItem('na_lvsid', 'aa84592d-ded7-4081-85d7-6b202b182ce7');
window.sessionStorage.setItem('na_vsid', 'aa84592d-ded7-4081-85d7-6b202b182ce7');
var search = new URLSearchParams(location.search.substring(1));
var hash = new URLSearchParams(location.search.substring(1));
if (search.get('utm_source')) {
source = search.get('utm_source');
} else if (hash.get('utm_source')) {
source = hash.get('utm_source');
} else if (document.referrer) {
if (/^http(s)?:\/\//i.test(document.referrer)) {
source = new URL(document.referrer).hostname;
} else {
source = new URL('https://' + document.referrer).hostname;
}
} else {
source = 'direct';
}
if (search.get('utm_medium')) {
medium = search.get('utm_medium');
} else if (hash.get('utm_medium')) {
medium = hash.get('utm_medium');
} else if (document.referrer) {
medium = 'referrer';
} else {
medium = 'none';
}
if (search.get('utm_campaign')) {
campaign = search.get('utm_campaign');
} else if (hash.get('utm_campaign')) {
campaign = hash.get('utm_campaign');
} else {
campaign = 'none';
}
if (source) {
window.localStorage.setItem('source', source);
}
if (medium) {
window.localStorage.setItem('medium', medium);
}
if (campaign) {
window.localStorage.setItem('campaign', campaign);
}
session_switch = true;
}
window.na.naid = naid;
window.na.vid = window.localStorage.getItem('na_vid') || '';
window.na.lvsid = window.localStorage.getItem('na_lvsid') || '';
window.na.vsid = window.sessionStorage.getItem('na_vsid') || '';
window.na.lid = params ? (params.lid || '') : '';
window.na.source = window.localStorage.getItem('source') || '';
window.na.medium = window.localStorage.getItem('medium') || '';
window.na.campaign = window.localStorage.getItem('campaign') || '';
if (session_switch) {
mineNugget('site_visit');
}
},
qs(args) {
var all = {};
if (args[0]) {
all.en = args[0];
} else {
return false;
}
if (args[1]) {
all.ec = args[1];
} else {
all.ec = '';
}
if (args[2]) {
all.pm = args[2];
} else {
all.pm = {};
}
var ua = window.navigator.userAgent;
var bn;
if (ua.match(/chrome|chromium|crios/i)) {
bn = 'chrome';
} else if (ua.match(/firefox|fxios/i)) {
bn = 'firefox';
} else if (ua.match(/safari/i)) {
bn = 'safari';
} else if (ua.match(/opr//i)) {
bn = 'opera';
} else if (ua.match(/edg/i)) {
bn = 'edge';
} else {
bn = 'not detected';
}
var vw = window.innerWidth;
var vh = window.innerHeight;
var vp = vw + 'x' + vh;
var sc = window.screen.colorDepth;
var sw = window.screen.width;
var sh = window.screen.height;
var sr = sw + 'x' + sh;
var cs = document.characterSet;
var t = document.title;
var url = window.location.href;
var time = new Date().getTime();
all.ua = ua;
all.bn = bn;
all.vp = vp;
all.sc = sc;
all.sr = sr;
all.cs = cs;
all.t = t;
all.url = url;
all.time = time;
all.naid = window.na.naid;
all.vid = window.na.vid;
all.lvsid = window.na.lvsid;
all.vsid = window.na.vsid;
all.lid = window.na.lid || '';
all.source = window.na.source || '';
all.medium = window.na.medium || '';
all.campaign = window.na.campaign || '';
return JSON.stringify(all);
},
record() {
var xhr = new XMLHttpRequest();
var url = 'https://analytics.datanugget.io/record';
var params = this.qs(arguments);
if (!params || arguments[0] === 'config') {
return;
}
xhr.open('POST', url, true);
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && /^20[0-9]$/.test(xhr.status)) {
var auto_script = document.createElement('script');
auto_script.id = 'datanugget_auto_script';
auto_script.innerText = xhr.response.replace(/(\s|\n|\r\n)/g, '');
document.body.appendChild(auto_script);
return true;
} else if (xhr.readyState == 4 && xhr.status !== 200) {
return false;
}
}
xhr.send(params);
}
}
function ObservableArray(items) {
var _self = this,
_array = [],
_handlers = {
itemadded: [],
itemremoved: [],
itemset: []
};
function defineIndexProperty(index) {
if (!(index in _self)) {
Object.defineProperty(_self, index, {
configurable: true,
enumerable: true,
get: function () {
return _array[index];
},
set: function (v) {
_array[index] = v;
raiseEvent({
type: 'itemset',
index: index,
item: v
});
}
});
}
}
function raiseEvent(event) {
_handlers[event.type].forEach(function (h) {
h.call(_self, event);
});
}
Object.defineProperty(_self, 'addEventListener', {
configurable: false,
enumerable: false,
writable: false,
value: function (eventName, handler) {
eventName = ('' + eventName).toLowerCase();
if (!(eventName in _handlers)) {
throw new Error('Invalid event name.');
}
if (typeof handler !== 'function') {
throw new Error('Invalid handler.');
}
_handlers[eventName].push(handler);
}
});
Object.defineProperty(_self, 'push', {
configurable: false,
enumerable: false,
writable: false,
value: function () {
var index;
for (var i = 0, ln = arguments.length; i < ln; i++) {
index = _array.length;
if (arguments[0][0] === 'config') {
window.na.config(arguments[0][1], (arguments[0][2] || {}));
} else {
window.na.record(arguments[0][0], arguments[0][1], arguments[0][2]);
}
_array.push(arguments[i]);
defineIndexProperty(index);
raiseEvent({
type: 'itemadded',
index: index,
item: arguments[i]
});
}
return _array.length;
}
});
Object.defineProperty(_self, 'length', {
configurable: false,
enumerable: false,
get: function () {
return _array.length;
},
set: function (value) {
var n = Number(value);
var length = _array.length;
if (n % 1 === 0 && n >= 0) {
if (n < length) {
_self.splice(n);
} else if (n > length) {
_self.push.apply(_self, new Array(n - length));
}
} else {
throw new RangeError('Invalid array length');
}
_array.length = n;
return value;
}
});
Object.getOwnPropertyNames(Array.prototype).forEach(function (name) {
if (!(name in _self)) {
Object.defineProperty(_self, name, {
configurable: false,
enumerable: false,
writable: false,
value: Array.prototype[name]
});
}
});
if (items instanceof Array) {
_self.push.apply(_self, items);
}
}
var temp = JSON.parse(JSON.stringify(window.nuggets));
window.nuggets = new ObservableArray([]);
for (var i = 0; i < temp.length; i++) {
window.nuggets.push(Object.values(temp[i]));
}
window.addEventListener('load', function() {
});
window.addEventListener('click', function(e) {
if (!/datanugget-cart-btn/.test(e.target.className)) {
return;
}
var category = window.iCategoryNo + '';
var name = window.product_name;
var id = window.iProductNo + '';
var basic_price = (typeof(window.product_sale_price) == 'undefined' ? window.product_price : window.product_sale_price);
var total_price = 0;
var total_quantity = 0;
var items = [];
var list = document.querySelectorAll('.option_product');
if (list.length > 0 && window.has_option === 'T') {
for (var i = 0; i < list.length; i++) {
var variant = list[i].querySelector('span').innerText;
var price = list[i].querySelector('.ec-front-product-item-price');
price = price ? parseInt(price.innerText.split('원')[0].replace(/[^0-9]/g, '')) : 0;
var quantity = list[i].querySelector('.quantity_opt');
quantity = quantity ? parseInt(quantity.value) : 0;
items.push({
name: name,
id: id,
price: (price / quantity) + '',
category: category,
category2: variant,
variant: variant,
quantity: quantity
});
total_quantity += quantity;
total_price += (price / quantity);
}
}
if (window.has_option === 'F') {
var quantity = parseInt(document.getElementById('quantity').value);
items.push({
name: name,
id: id,
price: basic_price,
category: category,
category2: variant,
variant: name,
quantity: quantity
});
total_quantity += quantity;
total_price += basic_price;
}
list = document.querySelectorAll('.add_product');
if (list.length > 0) {
for (i = 0; i < list.length; i++) {
var name = document.querySelector('.xans-product-option > tr > td > select') ? list[i].querySelector('.product').innerText.replace(/\n/g, ' ') : list[i].querySelector('.product').innerText.replace(/\n/g, ' ');
var id = list[i].querySelector('.add_product_option_box_price').getAttribute('product-no');
var price = parseInt(list[i].querySelector('.add_product_option_box_price').value);
var quantity = parseInt(list[i].querySelector('.quantity_opt').value);
items.push({
name: name,
id: id,
price: (price / quantity) + '',
quantity: quantity
});
}
}
var revenue = document.querySelector('.total strong').innerText.split(' ')[0].replace(/[^0-9]/g,'');
if (items.length > 0) {
mineNugget('add_to_cart', 'ecommerce', {
currency: window.currency || 'KRW',
value: revenue,
items: items
});
}
});