UNPKG

dop-website-sdk

Version:

Wisetracker website sdk ( dop-website-sdk )

302 lines (296 loc) 9.47 kB
// storage.js export class Storage { constructor() { // 공통 부분 this.CLIENT_IDENTIFY = "CID"; // Basic 그룹 this.BASIC_EVENTS = "DOT_EVENT"; this.BASIC_CONVERSION = "DOT_CONVERSION"; this.BASIC_SENT_FAIL = "DOT_SENT_FAIL"; // CAFE24 그룹 this.CAFE24_LOGIN_HISTORY = "DOT_CAFE24_LGNHIS"; try { if (this.windowLocalStorageAvailable()) { this.localStorage = window.localStorage; } else if (window.globalStorage) { // Firefox 2-3 use globalStorage // See https://developer.mozilla.org/en/dom/storage#globalStorage this.localStorage = window.globalStorage[window.location.hostname]; } else { this.log("localStorage is not available. Nothing we can do."); } if (typeof this.localStorage != "undefined") { // DOT data buffer this.initEventBufferForDOT(false); this.initConversionBufferForDOT(false); this.initSentFailBufferForDOT(false); } } catch (e) { this.error("localStorage initialize fail."); } } /*** * DOT functions * @public ****/ initEventBufferForDOT(doQuery) { let unsentData; try { // event unsent array let eventsFifo = this.getItem(this.BASIC_EVENTS); if (eventsFifo == null || typeof eventsFifo === "undefined") { this.setItem(this.BASIC_EVENTS, JSON.stringify(new Array())); unsentData = new Array(); } else { if (doQuery) { unsentData = JSON.parse(eventsFifo); } } } catch (e) { this.error(e); } return unsentData; } initConversionBufferForDOT(doQuery) { let unsentData; try { // event unsent array let eventsFifo = this.getItem(this.BASIC_CONVERSION); if (eventsFifo == null || typeof eventsFifo === "undefined") { this.setItem(this.BASIC_CONVERSION, JSON.stringify(new Array())); unsentData = new Array(); } else { if (doQuery) { unsentData = JSON.parse(eventsFifo); } } } catch (e) { this.error(e); } return unsentData; } initSentFailBufferForDOT(doQuery) { let unsentData; try { // event unsent array let eventsFifo = this.getItem(this.BASIC_SENT_FAIL); if (eventsFifo == null || typeof eventsFifo === "undefined") { this.setItem(this.BASIC_SENT_FAIL, JSON.stringify(new Array())); unsentData = new Array(); } else { if (doQuery) { unsentData = JSON.parse(eventsFifo); } } } catch (e) { this.error(e); } return unsentData; } putEventDataToDotStorage(data) { try { let _unsentData = this.initEventBufferForDOT(true); if (typeof data === 'object') { if (data != null) { _unsentData.push(data); this.setItem(this.BASIC_EVENTS, JSON.stringify(_unsentData)); } } } catch (e) { this.error(e); } } putConversionDataToDotStorage(data) { try { let _unsentData = this.initConversionBufferForDOT(true); if (typeof data === 'object') { if (data != null) { _unsentData.push(data); this.setItem(this.BASIC_CONVERSION, JSON.stringify(_unsentData)); } } } catch (e) { this.error(e); } } putSentFailDataToDotStorage(data) { try { let _unsentData = this.initSentFailBufferForDOT(true); if (typeof data === 'object') { if (data != null) { _unsentData.push(data); this.setItem(this.BASIC_SENT_FAIL, JSON.stringify(_unsentData)); } } } catch (e) { this.error(e); } } /** * Default CRD function * @public ***/ setItem(key, value) { try { if (typeof this.localStorage != "undefined") { this.localStorage.setItem([LOCALSTORAGE_PREFIX, key].join("_"), value); return true; } } catch (e) { this.error(e); } return false; } getItem(key) { try { if (typeof this.localStorage != "undefined") { return this.localStorage.getItem([LOCALSTORAGE_PREFIX, key].join("_")); } } catch (e) { this.error(e); } return null; } removeItem(key) { try { if (typeof this.localStorage != "undefined") { this.localStorage.removeItem([LOCALSTORAGE_PREFIX, key].join("_")); return true; } } catch (e) { this.error(e); } return false; } /** * Read all items from specific local storage data. * @public ***/ updateEventDataVsTime(_dataStr) { let _sendData = []; if (_dataStr != null) { _sendData = JSON.parse(_dataStr); try { // ############################################################## // 1. 저장된 데이터에서 바로 이전의 PAGES 데이터 index를 찾음. let _previousPageVisitTime = 0; let _cursorIndex = 0; let _now = new Date().getTime(); _sendData.forEach(function (rowData, index) { if (window.RW_Constants.DATA_GROUP_PAGE in rowData) { // 1. 이전의 Page visit time 을 계산해서 추가 시킴. if (_previousPageVisitTime > 0) { if (_sendData[_cursorIndex][window.RW_Constants.DATA_GROUP_PAGE].vs === 0) { _sendData[_cursorIndex][window.RW_Constants.DATA_GROUP_PAGE].vs = Math.floor((rowData[window.RW_Constants.DATA_GROUP_PAGE].vtTz - _previousPageVisitTime) / 1000); } } _previousPageVisitTime = rowData[window.RW_Constants.DATA_GROUP_PAGE].vtTz; _cursorIndex = index; // 2. 만약 현재 루프가 돌고 있는 페이지의 vs가 0 이고, 페이지 접속 시간이 30분을 초과하는 경우에, 강제로 30분으로 셋팅하여 보낼 수 있도록 처리. if (rowData[window.RW_Constants.DATA_GROUP_PAGE].vs === 0) { let _tvt = Math.floor((_now - rowData[window.RW_Constants.DATA_GROUP_PAGE].vtTz) / 1000); if (_tvt >= 1800) { rowData[window.RW_Constants.DATA_GROUP_PAGE].vs = 1800; } } } }); // ############################################################## } catch (e) {} } return _sendData; } flushFifo(key) { let _dataArray = []; try { if (typeof this.localStorage != "undefined") { let _dataStr = this.localStorage.getItem([LOCALSTORAGE_PREFIX, key].join("_")); if (_dataStr != null) { if (key === window.RW_storage.BASIC_EVENTS) { let _skipData = []; let _sendData = this.updateEventDataVsTime(_dataStr); if (_sendData != null && _sendData.length > 0) { let _sent = true; let _sendDataCount = _sendData.length; _sendData.forEach(function (rowData, index) { /* Page 데이터의 경우에, pv, vs 모두 0인 데이터는 전송하지 않기 */ if (window.RW_Constants.DATA_GROUP_PAGE in rowData) { if (rowData[window.RW_Constants.DATA_GROUP_PAGE].pv > 0 || rowData[window.RW_Constants.DATA_GROUP_PAGE].vs > 0) { _dataArray.push(rowData); } } else { _dataArray.push(rowData); } /* 마지막 페이지 데이터를 LocalStorage 에 담아두기 위한 로직. 체류시간 업데이트 목적용. */ if (index + 1 === _sendDataCount) { if (window.RW_Constants.DATA_GROUP_PAGE in rowData) { let __updatePage = { ...rowData }; __updatePage[window.RW_Constants.DATA_GROUP_PAGE] = { ...rowData[window.RW_Constants.DATA_GROUP_PAGE], pv: 0 }; _skipData.push(__updatePage); } } }); // _skipData 는 다시 event 로 저장하고, _dataArray는 전송을 한다. this.setItem(key, JSON.stringify(_skipData)); } } else { _dataArray = JSON.parse(_dataStr); if (_dataArray != null && _dataArray.length > 0) { this.setItem(key, JSON.stringify(new Array())); // FIFO reset. } } } } } catch (e) { this.error(e); } return _dataArray; } /** * print log into console of browser. * @private ***/ log(e) { try { if (process.env.NODE_ENV != "production") { if (typeof e === "string") { console.log(["[LocalStorage,", new Date().getTime(), "]", e].join(" ")); } else { this.error(e); } } } catch (e) {} } /** * print error log into console of browser. * @private ***/ error(e) { try { console.log(e); } catch (e) {} } /** * Local storage available check * @private ***/ windowLocalStorageAvailable() { let uid = new Date(); let result; try { let key = [LOCALSTORAGE_PREFIX, SDK_VERSION, uid].join("_"); window.localStorage.setItem(key, uid); result = window.localStorage.getItem(key) === String(uid); window.localStorage.removeItem(key); return result; } catch (e) { this.error(e); } return false; } }