UNPKG

dop-website-sdk

Version:

Wisetracker website sdk ( dop-website-sdk )

212 lines (206 loc) 6.53 kB
// transmitter.js import { SProperties } from "../dataObject/SProperties.js"; import axios from 'axios'; export class Transmitter { constructor(dataRcv, clickDataRcv) { this.sendBookingTimeKeyName = "sendBookingTime"; this.endPoint = dataRcv; this.adClkEndPoint = clickDataRcv; // let axios = require("axios"); vite not support require statment. this.ajax = axios.create({ baseURL: dataRcv, timeout: 3000, headers: { 'Content-type': 'application/json', 'authType': 'MhABL2kFMvrPFj2PukrVFQ==' } }); } /** * Start data sending task. * @public ***/ dataTransmittingOnBackground(command, target, immediate) { try { if (immediate) { this.doTransmitting(command, target); } else { let sendOk = this.hasBookedDataTransmittingTask(command); if (sendOk) { this.doTransmitting(command, target); } } } catch (e) { this.error(e); } } /** * Check scheduled transmitting * @private ***/ hasBookedDataTransmittingTask(command) { let needToSend = false; try { let tit = WDOT.transmittingIntervalTime; if (tit <= 0) { // immediate transmitting mode needToSend = true; } else { // interval transmitting mode let cIdentifyStr = window.RW_storage.getItem(window.RW_storage.CLIENT_IDENTIFY); if (cIdentifyStr != null) { let nowTime = new Date().getTime(); let properties = new SProperties(); properties.putAll(JSON.parse(cIdentifyStr)); if (typeof properties[this.sendBookingTimeKeyName] === "undefined") { properties.set(this.sendBookingTimeKeyName, nowTime); window.RW_storage.setItem(window.RW_storage.CLIENT_IDENTIFY, JSON.stringify(properties)); } else { let bookingTime = properties[this.sendBookingTimeKeyName]; if (nowTime > bookingTime + tit * window.sdk_timeutil.SECOND) { delete properties[this.sendBookingTimeKeyName]; window.RW_storage.setItem(window.RW_storage.CLIENT_IDENTIFY, JSON.stringify(properties)); needToSend = true; } } } } } catch (e) { this.error(e); } return needToSend; } /** * Send an event data to collector * @private ***/ doTransmitting(command, target) { setTimeout(function () { try { if (command === window.RW_Constants.COMMAND) { // 1. EVENT if (target === "ALL" || target === window.RW_storage.BASIC_EVENTS) { let unsentEvent = window.RW_storage.flushFifo(window.RW_storage.BASIC_EVENTS); if (unsentEvent != null && unsentEvent.length > 0) { let sendStr = JSON.stringify(unsentEvent); let hashNum = window.RW_transmitter.hashCodeOfSendData(sendStr); window.RW_transmitter.connectCollectorServer(command, sendStr, { headers: { "hash": hashNum } }, 0); } } // 2. CONVERSION if (target === "ALL" || target === window.RW_storage.BASIC_CONVERSION) { let unsentConversion = window.RW_storage.flushFifo(window.RW_storage.BASIC_CONVERSION); if (unsentConversion != null && unsentConversion.length > 0) { let sendStr = JSON.stringify(unsentConversion); let hashNum = window.RW_transmitter.hashCodeOfSendData(sendStr); window.RW_transmitter.connectCollectorServer(command, sendStr, { headers: { "hash": hashNum } }, 0); } } // 3. SENT_FAIL let unsentFail = window.RW_storage.flushFifo(window.RW_storage.BASIC_SENT_FAIL); if (unsentFail != null && unsentFail.length > 0) { let sendStr = JSON.stringify(unsentFail); let hashNum = window.RW_transmitter.hashCodeOfSendData(sendStr); window.RW_transmitter.connectCollectorServer(command, sendStr, { headers: { "hash": hashNum } }, 0); } } } catch (e) { this.error(e); } }, 0); } /** * Connect to server * @public ***/ connectCollectorServer(command, sendStr, header, retryCnt) { try { window.RW_transmitter.ajax.post(window.RW_transmitter.endPoint, sendStr, { ...header, credentials: true }).then(function (response) { window.RW_transmitter.log("SDK has been finished sending data to collector server. ( response code : " + response.status + " ) "); }).catch(function (error) { window.RW_transmitter.error(error); let sendArr = JSON.parse(sendStr); if (sendArr != null && sendArr.length > 0) { sendArr.forEach(function (element) { window.RW_storage.putSentFailDataToDotStorage(element); }); } }); } catch (e) { this.error(e); } } sendAdClickDataToCollectorServer(command, param, header) { try { window.RW_transmitter.ajax.get(window.RW_transmitter.adClkEndPoint, { params: { ...param }, headers: { // 요청 헤더 ...header } }).then(function (response) { window.RW_transmitter.log("SDK has been finished sending ad click data to collector server. ( response code : " + response.status + " ) "); }).catch(function (error) { window.RW_transmitter.error(error); }); } catch (e) { this.error(e); } } /** * * @public ***/ hashCodeOfSendData(str) { let char; let hash = 0; if (str.length == 0) return hash; for (let i = 0; i < str.length; i++) { char = str.charCodeAt(i); hash = (hash << 5) - hash + char; hash = hash & hash; // Convert to 32bit integer } return hash; } /** * print log into console of browser. * @private ***/ log(e) { try { if (process.env.NODE_ENV != "production") { if (typeof e === "string") { console.log(["[Transmitter,", 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) {} } }