UNPKG

tui-code-snippet

Version:
69 lines (57 loc) 1.74 kB
/** * @fileoverview Send hostname on DOMContentLoaded. * @author NHN FE Development Lab <dl_javascript@nhn.com> */ 'use strict'; var isUndefined = require('../type/isUndefined'); var imagePing = require('./imagePing'); var ms7days = 7 * 24 * 60 * 60 * 1000; /** * Check if the date has passed 7 days * @param {number} date - milliseconds * @returns {boolean} * @private */ function isExpired(date) { var now = new Date().getTime(); return now - date > ms7days; } /** * Send hostname on DOMContentLoaded. * To prevent hostname set tui.usageStatistics to false. * @param {string} appName - application name * @param {string} trackingId - GA tracking ID * @ignore */ function sendHostname(appName, trackingId) { var url = 'https://www.google-analytics.com/collect'; var hostname = location.hostname; var hitType = 'event'; var eventCategory = 'use'; var applicationKeyForStorage = 'TOAST UI ' + appName + ' for ' + hostname + ': Statistics'; var date = window.localStorage.getItem(applicationKeyForStorage); // skip if the flag is defined and is set to false explicitly if (!isUndefined(window.tui) && window.tui.usageStatistics === false) { return; } // skip if not pass seven days old if (date && !isExpired(date)) { return; } window.localStorage.setItem(applicationKeyForStorage, new Date().getTime()); setTimeout(function() { if (document.readyState === 'interactive' || document.readyState === 'complete') { imagePing(url, { v: 1, t: hitType, tid: trackingId, cid: hostname, dp: hostname, dh: appName, el: appName, ec: eventCategory }); } }, 1000); } module.exports = sendHostname;